|
| 1 | +import NotionDocumentService from './service'; |
| 2 | +import { IWebRequestService } from '@/service/common/webRequest'; |
| 3 | +import { ICookieService } from '@/service/common/cookie'; |
| 4 | +import { NotionUserContent } from './types'; |
| 5 | +import Container from 'typedi'; |
| 6 | + |
| 7 | +// Mock services |
| 8 | +jest.mock('@/service/common/webRequest'); |
| 9 | +jest.mock('@/service/common/cookie'); |
| 10 | + |
| 11 | +describe('NotionDocumentService', () => { |
| 12 | + let notionService: NotionDocumentService; |
| 13 | + let mockWebRequestService: jest.Mocked<IWebRequestService>; |
| 14 | + let mockCookieService: jest.Mocked<ICookieService>; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + // Create new instances of mock services for each test |
| 18 | + mockWebRequestService = { |
| 19 | + startChangeHeader: jest.fn(), |
| 20 | + end: jest.fn(), |
| 21 | + changeUrl: jest.fn(url => Promise.resolve(url)), // Ensure changeUrl returns a Promise<string> |
| 22 | + } as any; // Using 'as any' to simplify mock structure for this example |
| 23 | + mockCookieService = { |
| 24 | + getAll: jest.fn().mockResolvedValue([]), // Mock getAll to return empty array or relevant cookie structure |
| 25 | + set: jest.fn(), |
| 26 | + remove: jest.fn(), |
| 27 | + get: jest.fn(), |
| 28 | + } as any; // Using 'as any' to simplify mock structure |
| 29 | + |
| 30 | + // Mock Container.get for each service |
| 31 | + Container.get = jest.fn().mockImplementation((token: any) => { |
| 32 | + if (token === IWebRequestService) { |
| 33 | + return mockWebRequestService; |
| 34 | + } |
| 35 | + if (token === ICookieService) { |
| 36 | + return mockCookieService; |
| 37 | + } |
| 38 | + throw new Error(`Unknown token: ${String(token)}`); |
| 39 | + }) as any; |
| 40 | + |
| 41 | + notionService = new NotionDocumentService(); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + jest.clearAllMocks(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('getRepositories', () => { |
| 49 | + it('should return an empty array when recordMap.space is undefined', async () => { |
| 50 | + // Mock getUserContent to return a userContent object where recordMap.space is undefined |
| 51 | + const mockUserContent: Partial<NotionUserContent> = { |
| 52 | + recordMap: { |
| 53 | + // space is intentionally undefined |
| 54 | + block: {}, |
| 55 | + notion_user: {}, |
| 56 | + collection: {}, |
| 57 | + collection_view: {}, |
| 58 | + comment: {}, |
| 59 | + discussion: {}, |
| 60 | + follow: {}, |
| 61 | + space_view: {}, |
| 62 | + user_root: {}, |
| 63 | + user_settings: {}, |
| 64 | + team: {}, |
| 65 | + team_role: {}, |
| 66 | + collection_block_column_order: {}, |
| 67 | + collection_block_column_format: {}, |
| 68 | + } as any, // Using 'as any' to bypass strict type checks for partial mock |
| 69 | + }; |
| 70 | + |
| 71 | + jest.spyOn(notionService as any, 'getUserContent').mockResolvedValue(mockUserContent as NotionUserContent); |
| 72 | + |
| 73 | + const repositories = await notionService.getRepositories(); |
| 74 | + |
| 75 | + expect(repositories).toEqual([]); |
| 76 | + expect((notionService as any).getUserContent).toHaveBeenCalledTimes(1); |
| 77 | + }); |
| 78 | + |
| 79 | + // Add other test cases for getRepositories if needed |
| 80 | + }); |
| 81 | + |
| 82 | + // Add tests for other methods of NotionDocumentService |
| 83 | +}); |
0 commit comments