Просмотр исходного кода

refactor: simplify mock response and page creation in tests

Shun Miyazawa 2 дней назад
Родитель
Сommit
5498816cb9
1 измененных файлов с 58 добавлено и 77 удалено
  1. 58 77
      apps/app/src/server/routes/apiv3/page/respond-with-single-page.spec.ts

+ 58 - 77
apps/app/src/server/routes/apiv3/page/respond-with-single-page.spec.ts

@@ -1,5 +1,6 @@
 import type {
 import type {
   IDataWithMeta,
   IDataWithMeta,
+  IPageInfo,
   IPageInfoExt,
   IPageInfoExt,
   IPageNotFoundInfo,
   IPageNotFoundInfo,
 } from '@growi/core';
 } from '@growi/core';
@@ -22,56 +23,62 @@ vi.mock('~/utils/logger', () => ({
 
 
 import { respondWithSinglePage } from './respond-with-single-page';
 import { respondWithSinglePage } from './respond-with-single-page';
 
 
-interface MockRes {
-  apiv3: ReturnType<typeof vi.fn>;
-  apiv3Err: ReturnType<typeof vi.fn>;
+// Express Response has extensive required properties that aren't used in these tests
+function createMockRes(): ApiV3Response {
+  return {
+    apiv3: vi.fn().mockReturnValue(undefined),
+    apiv3Err: vi.fn().mockReturnValue(undefined),
+  } as unknown as ApiV3Response;
 }
 }
 
 
-interface MockPage {
-  path: string;
-  initLatestRevisionField: ReturnType<typeof vi.fn>;
-  populateDataToShowRevision: ReturnType<typeof vi.fn>;
+// HydratedDocument adds Mongoose internals that aren't relevant to these tests
+function createMockPage(path = '/normal-page'): HydratedDocument<PageDocument> {
+  const page = {
+    path,
+    initLatestRevisionField: vi.fn(),
+    populateDataToShowRevision: vi.fn(),
+  };
+  page.populateDataToShowRevision.mockResolvedValue(page);
+  return page as unknown as HydratedDocument<PageDocument>;
+}
+
+function createPageInfo(overrides: Partial<IPageInfo> = {}): IPageInfo {
+  return {
+    isNotFound: false,
+    isV5Compatible: true,
+    isEmpty: false,
+    isMovable: true,
+    isDeletable: true,
+    isAbleToDeleteCompletely: true,
+    isRevertible: false,
+    bookmarkCount: 0,
+    ...overrides,
+  };
 }
 }
 
 
 describe('respondWithSinglePage', () => {
 describe('respondWithSinglePage', () => {
-  let mockRes: MockRes;
-  let mockPage: MockPage;
+  let mockRes: ApiV3Response;
+  let mockPage: HydratedDocument<PageDocument>;
 
 
   beforeEach(() => {
   beforeEach(() => {
-    mockRes = {
-      apiv3: vi.fn().mockReturnValue(undefined),
-      apiv3Err: vi.fn().mockReturnValue(undefined),
-    };
-
-    mockPage = {
-      path: '/normal-page',
-      initLatestRevisionField: vi.fn(),
-      populateDataToShowRevision: vi.fn(),
-    };
-
-    // Make populateDataToShowRevision return the same object (modified in place)
-    mockPage.populateDataToShowRevision.mockImplementation(() =>
-      Promise.resolve(mockPage),
-    );
+    mockRes = createMockRes();
+    mockPage = createMockPage();
   });
   });
 
 
   describe('success case', () => {
   describe('success case', () => {
     it('should return success response with page and meta when page exists', async () => {
     it('should return success response with page and meta when page exists', async () => {
       // Arrange
       // Arrange
-      const mockMeta = { isNotFound: false } as IPageInfoExt;
+      const mockMeta = createPageInfo();
       const pageWithMeta: IDataWithMeta<
       const pageWithMeta: IDataWithMeta<
         HydratedDocument<PageDocument>,
         HydratedDocument<PageDocument>,
         IPageInfoExt
         IPageInfoExt
       > = {
       > = {
-        data: mockPage as unknown as HydratedDocument<PageDocument>,
+        data: mockPage,
         meta: mockMeta,
         meta: mockMeta,
       };
       };
 
 
       // Act
       // Act
-      await respondWithSinglePage(
-        mockRes as unknown as ApiV3Response,
-        pageWithMeta,
-      );
+      await respondWithSinglePage(mockRes, pageWithMeta);
 
 
       // Assert
       // Assert
       expect(mockRes.apiv3).toHaveBeenCalledWith(
       expect(mockRes.apiv3).toHaveBeenCalledWith(
@@ -88,21 +95,17 @@ describe('respondWithSinglePage', () => {
     it('should initialize revision field when revisionId is provided', async () => {
     it('should initialize revision field when revisionId is provided', async () => {
       // Arrange
       // Arrange
       const revisionId = '507f1f77bcf86cd799439011';
       const revisionId = '507f1f77bcf86cd799439011';
-      const mockMeta = { isNotFound: false } as IPageInfoExt;
+      const mockMeta = createPageInfo();
       const pageWithMeta: IDataWithMeta<
       const pageWithMeta: IDataWithMeta<
         HydratedDocument<PageDocument>,
         HydratedDocument<PageDocument>,
         IPageInfoExt
         IPageInfoExt
       > = {
       > = {
-        data: mockPage as unknown as HydratedDocument<PageDocument>,
+        data: mockPage,
         meta: mockMeta,
         meta: mockMeta,
       };
       };
 
 
       // Act
       // Act
-      await respondWithSinglePage(
-        mockRes as unknown as ApiV3Response,
-        pageWithMeta,
-        { revisionId },
-      );
+      await respondWithSinglePage(mockRes, pageWithMeta, { revisionId });
 
 
       // Assert
       // Assert
       expect(mockPage.initLatestRevisionField).toHaveBeenCalledWith(revisionId);
       expect(mockPage.initLatestRevisionField).toHaveBeenCalledWith(revisionId);
@@ -112,20 +115,17 @@ describe('respondWithSinglePage', () => {
   describe('forbidden case', () => {
   describe('forbidden case', () => {
     it('should return 403 when page meta has isForbidden=true', async () => {
     it('should return 403 when page meta has isForbidden=true', async () => {
       // Arrange
       // Arrange
-      const mockMeta = {
+      const mockMeta: IPageNotFoundInfo = {
         isNotFound: true,
         isNotFound: true,
         isForbidden: true,
         isForbidden: true,
-      } as IPageNotFoundInfo;
+      };
       const pageWithMeta: IDataWithMeta<null, IPageNotFoundInfo> = {
       const pageWithMeta: IDataWithMeta<null, IPageNotFoundInfo> = {
         data: null,
         data: null,
         meta: mockMeta,
         meta: mockMeta,
       };
       };
 
 
       // Act
       // Act
-      await respondWithSinglePage(
-        mockRes as unknown as ApiV3Response,
-        pageWithMeta,
-      );
+      await respondWithSinglePage(mockRes, pageWithMeta);
 
 
       // Assert
       // Assert
       expect(mockRes.apiv3Err).toHaveBeenCalledWith(
       expect(mockRes.apiv3Err).toHaveBeenCalledWith(
@@ -140,28 +140,20 @@ describe('respondWithSinglePage', () => {
 
 
     it('should return 403 when disableUserPages=true and page is a user page', async () => {
     it('should return 403 when disableUserPages=true and page is a user page', async () => {
       // Arrange
       // Arrange
-      const userPageMock = {
-        path: '/user/john',
-        initLatestRevisionField: vi.fn(),
-        populateDataToShowRevision: vi.fn(),
-      };
-      const mockMeta = { isNotFound: false } as IPageInfoExt;
+      const userPage = createMockPage('/user/john');
+      const mockMeta = createPageInfo();
       const pageWithMeta: IDataWithMeta<
       const pageWithMeta: IDataWithMeta<
         HydratedDocument<PageDocument>,
         HydratedDocument<PageDocument>,
         IPageInfoExt
         IPageInfoExt
       > = {
       > = {
-        data: userPageMock as unknown as HydratedDocument<PageDocument>,
+        data: userPage,
         meta: mockMeta,
         meta: mockMeta,
       };
       };
 
 
       // Act
       // Act
-      await respondWithSinglePage(
-        mockRes as unknown as ApiV3Response,
-        pageWithMeta,
-        {
-          disableUserPages: true,
-        },
-      );
+      await respondWithSinglePage(mockRes, pageWithMeta, {
+        disableUserPages: true,
+      });
 
 
       // Assert
       // Assert
       expect(mockRes.apiv3Err).toHaveBeenCalledWith(
       expect(mockRes.apiv3Err).toHaveBeenCalledWith(
@@ -176,28 +168,20 @@ describe('respondWithSinglePage', () => {
 
 
     it('should return 403 when disableUserPages=true and page is a user top page', async () => {
     it('should return 403 when disableUserPages=true and page is a user top page', async () => {
       // Arrange
       // Arrange
-      const userTopPageMock = {
-        path: '/user',
-        initLatestRevisionField: vi.fn(),
-        populateDataToShowRevision: vi.fn(),
-      };
-      const mockMeta = { isNotFound: false } as IPageInfoExt;
+      const userTopPage = createMockPage('/user');
+      const mockMeta = createPageInfo();
       const pageWithMeta: IDataWithMeta<
       const pageWithMeta: IDataWithMeta<
         HydratedDocument<PageDocument>,
         HydratedDocument<PageDocument>,
         IPageInfoExt
         IPageInfoExt
       > = {
       > = {
-        data: userTopPageMock as unknown as HydratedDocument<PageDocument>,
+        data: userTopPage,
         meta: mockMeta,
         meta: mockMeta,
       };
       };
 
 
       // Act
       // Act
-      await respondWithSinglePage(
-        mockRes as unknown as ApiV3Response,
-        pageWithMeta,
-        {
-          disableUserPages: true,
-        },
-      );
+      await respondWithSinglePage(mockRes, pageWithMeta, {
+        disableUserPages: true,
+      });
 
 
       // Assert
       // Assert
       expect(mockRes.apiv3Err).toHaveBeenCalledWith(
       expect(mockRes.apiv3Err).toHaveBeenCalledWith(
@@ -213,20 +197,17 @@ describe('respondWithSinglePage', () => {
   describe('not-found case', () => {
   describe('not-found case', () => {
     it('should return 404 when page meta has isForbidden=false (not-found only)', async () => {
     it('should return 404 when page meta has isForbidden=false (not-found only)', async () => {
       // Arrange
       // Arrange
-      const mockMeta = {
+      const mockMeta: IPageNotFoundInfo = {
         isNotFound: true,
         isNotFound: true,
         isForbidden: false,
         isForbidden: false,
-      } as IPageNotFoundInfo;
+      };
       const pageWithMeta: IDataWithMeta<null, IPageNotFoundInfo> = {
       const pageWithMeta: IDataWithMeta<null, IPageNotFoundInfo> = {
         data: null,
         data: null,
         meta: mockMeta,
         meta: mockMeta,
       };
       };
 
 
       // Act
       // Act
-      await respondWithSinglePage(
-        mockRes as unknown as ApiV3Response,
-        pageWithMeta,
-      );
+      await respondWithSinglePage(mockRes, pageWithMeta);
 
 
       // Assert
       // Assert
       expect(mockRes.apiv3Err).toHaveBeenCalledWith(
       expect(mockRes.apiv3Err).toHaveBeenCalledWith(