|
|
@@ -1,92 +1,36 @@
|
|
|
-// import type { IPage, IUser } from '@growi/core';
|
|
|
-// import mongoose from 'mongoose';
|
|
|
+import mongoose from 'mongoose';
|
|
|
import { vi } from 'vitest';
|
|
|
|
|
|
-import { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
|
|
|
-
|
|
|
-// import PageModel, { PageModel } from '../../models/page';
|
|
|
-// import { UserModel } from '../../models/user';
|
|
|
-
|
|
|
import { deleteCompletelyUserHomeBySystem } from './delete-completely-user-home-by-system';
|
|
|
|
|
|
import PageService from '.';
|
|
|
|
|
|
-describe('delete-completely-user-home-by-system test', () => {
|
|
|
-
|
|
|
- let User;
|
|
|
- let Page;
|
|
|
-
|
|
|
- const testUser01Name = 'testUser01';
|
|
|
-
|
|
|
- const rootPagePath = '/';
|
|
|
- const userPagePath = '/user';
|
|
|
- const testUser01HomepagePath = `${userPagePath}/${testUser01Name}`;
|
|
|
- const testUser01HomeSubpagePath = `${userPagePath}/${testUser01Name}/subpage`;
|
|
|
-
|
|
|
- // beforeAll(async() => {
|
|
|
- // User = mongoose.model<IUser>('User');
|
|
|
- // Page = mongoose.model<IPage, PageModel>('Page');
|
|
|
-
|
|
|
- // const rootPageId = new mongoose.Types.ObjectId();
|
|
|
- // const userPageId = new mongoose.Types.ObjectId();
|
|
|
- // const testUser01HomepageId = new mongoose.Types.ObjectId();
|
|
|
-
|
|
|
- // await User.insertMany([
|
|
|
- // {
|
|
|
- // name: testUser01Name,
|
|
|
- // username: testUser01Name,
|
|
|
- // email: `${testUser01Name}@example.com`,
|
|
|
- // },
|
|
|
- // ]);
|
|
|
+// TODO: use actual user model after ~/server/models/user.js becomes importable in vitest
|
|
|
+// ref: https://github.com/vitest-dev/vitest/issues/846
|
|
|
+const userSchema = new mongoose.Schema({
|
|
|
+ name: { type: String },
|
|
|
+ username: { type: String, required: true, unique: true },
|
|
|
+ email: { type: String, unique: true, sparse: true },
|
|
|
+}, {
|
|
|
+ timestamps: true,
|
|
|
+});
|
|
|
+const User = mongoose.model('User', userSchema);
|
|
|
|
|
|
- // await PageModel.insertMany([
|
|
|
- // {
|
|
|
- // _id: rootPageId,
|
|
|
- // path: rootPagePath,
|
|
|
- // grant: Page.GRANT_PUBLIC,
|
|
|
- // isEmpty: true,
|
|
|
- // parent: null,
|
|
|
- // },
|
|
|
- // {
|
|
|
- // _id: userPageId,
|
|
|
- // path: userPagePath,
|
|
|
- // grant: Page.GRANT_PUBLIC,
|
|
|
- // isEmpty: true,
|
|
|
- // parent: rootPageId,
|
|
|
- // },
|
|
|
- // {
|
|
|
- // _id: testUser01HomepageId,
|
|
|
- // path: testUser01HomepagePath,
|
|
|
- // grant: Page.GRANT_PUBLIC,
|
|
|
- // isEmpty: true,
|
|
|
- // parent: userPageId,
|
|
|
- // },
|
|
|
- // {
|
|
|
- // _id: new mongoose.Types.ObjectId(),
|
|
|
- // path: testUser01HomeSubpagePath,
|
|
|
- // grant: Page.GRANT_PUBLIC,
|
|
|
- // isEmpty: true,
|
|
|
- // parent: testUser01HomepageId,
|
|
|
- // },
|
|
|
- // ]);
|
|
|
- // });
|
|
|
+describe('delete-completely-user-home-by-system test', () => {
|
|
|
+ const userId1 = new mongoose.Types.ObjectId();
|
|
|
|
|
|
- // afterEach(async() => {
|
|
|
- // await User.deleteMany({});
|
|
|
- // await Page.deleteMany({});
|
|
|
- // });
|
|
|
+ beforeAll(async() => {
|
|
|
+ await User.create({
|
|
|
+ _id: userId1, name: 'user1', username: 'user1', email: 'user1@example.com',
|
|
|
+ });
|
|
|
+ });
|
|
|
|
|
|
describe('deleteCompletelyUserHomeBySystem()', () => {
|
|
|
- const mockUpdateDescendantCountOfAncestors = vi.fn().mockImplementation(
|
|
|
- (pageId: ObjectIdLike, inc: number, shouldIncludeTarget: boolean): Promise<void> => Promise.resolve(),
|
|
|
- );
|
|
|
- const mockDeleteCompletelyOperation = vi.fn().mockImplementation((pageIds: any, pagePaths: any) => Promise.resolve());
|
|
|
- const mockPageEvent = {
|
|
|
- emit: vi.fn().mockImplementation((event: string, ...args: any[]): void => {}),
|
|
|
- };
|
|
|
- const mockDeleteMultipleCompletely = vi.fn().mockImplementation(
|
|
|
- (pages: any, user: any, options?: any): Promise<void> => Promise.resolve(),
|
|
|
- );
|
|
|
+ // setup
|
|
|
+ const mockUpdateDescendantCountOfAncestors = vi.fn().mockImplementation(() => Promise.resolve());
|
|
|
+ const mockDeleteCompletelyOperation = vi.fn().mockImplementation(() => Promise.resolve());
|
|
|
+ const mockPageEvent = { emit: vi.fn().mockImplementation(() => {}) };
|
|
|
+ const mockDeleteMultipleCompletely = vi.fn().mockImplementation(() => Promise.resolve());
|
|
|
|
|
|
const mockPageService = {
|
|
|
updateDescendantCountOfAncestors: mockUpdateDescendantCountOfAncestors,
|
|
|
@@ -97,7 +41,7 @@ describe('delete-completely-user-home-by-system test', () => {
|
|
|
|
|
|
it('should call page service functions', async() => {
|
|
|
// when
|
|
|
- await deleteCompletelyUserHomeBySystem(testUser01HomepagePath, mockPageService);
|
|
|
+ await deleteCompletelyUserHomeBySystem('/user/user1', mockPageService);
|
|
|
|
|
|
// then
|
|
|
expect(mockUpdateDescendantCountOfAncestors).toHaveBeenCalled();
|
|
|
@@ -109,7 +53,7 @@ describe('delete-completely-user-home-by-system test', () => {
|
|
|
it('should throw error if userHomepage is not exists', async() => {
|
|
|
// when
|
|
|
const throwError = async() => {
|
|
|
- await deleteCompletelyUserHomeBySystem('/user/not_exist_user', mockPageService);
|
|
|
+ await deleteCompletelyUserHomeBySystem('/user/not_exists_user', mockPageService);
|
|
|
};
|
|
|
|
|
|
// then
|