Futa Arai 7 месяцев назад
Родитель
Сommit
9c929ad077
1 измененных файлов с 26 добавлено и 84 удалено
  1. 26 84
      packages/remark-lsx/src/client/stores/lsx/lsx.spec.ts

+ 26 - 84
packages/remark-lsx/src/client/stores/lsx/lsx.spec.ts

@@ -6,16 +6,16 @@ import lsxMiddleware from '../../../server';
 
 import { useSWRxLsx } from './lsx';
 
-// Mock the generateBaseQuery function that requires Mongoose
+// Mock the generateBaseQuery function
 vi.mock('../../../server/routes/list-pages/generate-base-query', () => ({
   generateBaseQuery: vi.fn().mockResolvedValue({
     query: {
       skip: vi.fn().mockReturnThis(),
       limit: vi.fn().mockReturnThis(),
       sort: vi.fn().mockReturnThis(),
-      and: vi.fn().mockReturnThis(), // Add and method for depth/filter conditions
-      clone: vi.fn().mockReturnThis(), // Add clone method
-      count: vi.fn().mockResolvedValue(10), // Add count method
+      and: vi.fn().mockReturnThis(),
+      clone: vi.fn().mockReturnThis(),
+      count: vi.fn().mockResolvedValue(10),
       exec: vi.fn().mockResolvedValue([]),
     },
     addConditionToListOnlyDescendants: vi.fn().mockReturnThis(),
@@ -30,13 +30,13 @@ vi.mock('mongoose', () => ({
       skip: vi.fn().mockReturnThis(),
       limit: vi.fn().mockReturnThis(),
       sort: vi.fn().mockReturnThis(),
-      and: vi.fn().mockReturnThis(), // Add and method
-      clone: vi.fn().mockReturnThis(), // Add clone method
-      count: vi.fn().mockResolvedValue(10), // Add count method
+      and: vi.fn().mockReturnThis(),
+      clone: vi.fn().mockReturnThis(),
+      count: vi.fn().mockResolvedValue(10),
       exec: vi.fn().mockResolvedValue([]),
     }),
     countDocuments: vi.fn().mockResolvedValue(0),
-    aggregate: vi.fn().mockResolvedValue([{ count: 5 }]), // Add aggregate method for getToppageViewersCount
+    aggregate: vi.fn().mockResolvedValue([{ count: 5 }]),
   }),
 }));
 
@@ -47,6 +47,15 @@ describe('useSWRxLsx integration tests', () => {
   let server: Server;
   let app: express.Application;
 
+  // Helper function to setup axios spy
+  const setupAxiosSpy = () => {
+    const originalAxios = axios.create();
+    return vi.spyOn(axios, 'get').mockImplementation((url, config) => {
+      const fullUrl = url.startsWith('/_api') ? `${TEST_SERVER_URL}${url}` : url;
+      return originalAxios.get(fullUrl, config);
+    });
+  };
+
   beforeAll(async () => {
     // Create minimal Express app with just the LSX route
     app = express();
@@ -67,7 +76,6 @@ describe('useSWRxLsx integration tests', () => {
       next();
     });
 
-
     // Mock minimal GROWI-like structure for the middleware
     const mockCrowi = {
       require: () => () => (req: any, res: any, next: any) => next(),
@@ -98,48 +106,24 @@ describe('useSWRxLsx integration tests', () => {
   });
 
   it('should make actual server request and receive 2xx response for basic lsx request', async () => {
-    // Configure axios to use the test server
-    const originalAxios = axios.create();
-
-    // Mock axios.get to point to our test server
-    const axiosGetSpy = vi
-      .spyOn(axios, 'get')
-      .mockImplementation((url, config) => {
-        const fullUrl = url.startsWith('/_api')
-          ? `${TEST_SERVER_URL}${url}`
-          : url;
-        return originalAxios.get(fullUrl, config);
-      });
+    const axiosGetSpy = setupAxiosSpy();
 
     const { result } = renderHook(() =>
       useSWRxLsx('/test-page', { depth: '1' }, false),
     );
 
-    // Wait for either data or error to be defined
-    await waitFor(
-      () => {
-        expect(
-          result.current.data !== undefined ||
-            result.current.error !== undefined,
-        ).toBe(true);
-      },
-      { timeout: 5000 },
-    );
+    await waitFor(() => expect(result.current.data).toBeDefined(), { timeout: 5000 });
 
-    // Verify axios.get was called
     expect(axiosGetSpy).toHaveBeenCalledWith(
       '/_api/lsx',
       expect.objectContaining({
         params: expect.objectContaining({
           pagePath: '/test-page',
-          options: expect.objectContaining({
-            depth: '1',
-          }),
+          options: expect.objectContaining({ depth: '1' }),
         }),
       }),
     );
 
-    // Verify we got a successful 2xx response with data
     expect(result.current.data).toBeDefined();
     expect(result.current.error).toBeUndefined();
 
@@ -147,37 +131,14 @@ describe('useSWRxLsx integration tests', () => {
   });
 
   it('should handle different lsx options correctly', async () => {
-    const originalAxios = axios.create();
-
-    const axiosGetSpy = vi
-      .spyOn(axios, 'get')
-      .mockImplementation((url, config) => {
-        const fullUrl = url.startsWith('/_api')
-          ? `${TEST_SERVER_URL}${url}`
-          : url;
-        return originalAxios.get(fullUrl, config);
-      });
+    const axiosGetSpy = setupAxiosSpy();
 
     const { result } = renderHook(() =>
-      useSWRxLsx(
-        '/parent-page',
-        {
-          depth: '2',
-          filter: 'test',
-          sort: 'createdAt',
-        },
-        false,
-      ),
+      useSWRxLsx('/parent-page', { depth: '2', filter: 'test', sort: 'createdAt' }, false),
     );
 
-    await waitFor(
-      () => {
-        expect(result.current.data).toBeDefined();
-      },
-      { timeout: 5000 },
-    );
+    await waitFor(() => expect(result.current.data).toBeDefined(), { timeout: 5000 });
 
-    // Verify the request was made with correct parameters
     expect(axiosGetSpy).toHaveBeenCalledWith(
       '/_api/lsx',
       expect.objectContaining({
@@ -192,7 +153,6 @@ describe('useSWRxLsx integration tests', () => {
       }),
     );
 
-    // Verify we got a successful 2xx response with data
     expect(result.current.data).toBeDefined();
     expect(result.current.error).toBeUndefined();
 
@@ -200,30 +160,12 @@ describe('useSWRxLsx integration tests', () => {
   });
 
   it('should handle server validation errors properly', async () => {
-    const originalAxios = axios.create();
+    const axiosGetSpy = setupAxiosSpy();
 
-    const axiosGetSpy = vi
-      .spyOn(axios, 'get')
-      .mockImplementation((url, config) => {
-        const fullUrl = url.startsWith('/_api')
-          ? `${TEST_SERVER_URL}${url}`
-          : url;
-        return originalAxios.get(fullUrl, config);
-      });
+    const { result } = renderHook(() => useSWRxLsx('', {}, false));
 
-    const { result } = renderHook(() =>
-      // Missing required pagePath parameter should cause validation error
-      useSWRxLsx('', {}, false),
-    );
-
-    await waitFor(
-      () => {
-        expect(result.current.error).toBeDefined();
-      },
-      { timeout: 5000 },
-    );
+    await waitFor(() => expect(result.current.error).toBeDefined(), { timeout: 5000 });
 
-    // Should receive an error from the server
     expect(result.current.error).toBeDefined();
     expect(result.current.data).toBeUndefined();