|
| 1 | +import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 2 | +import Client from '.'; |
| 3 | + |
| 4 | +let notionClient: Client; |
| 5 | + |
| 6 | +beforeAll(() => { |
| 7 | + notionClient = new Client(); |
| 8 | +}); |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + vi.restoreAllMocks(); |
| 12 | +}); |
| 13 | + |
| 14 | +const TEST_ID = 'TEST_PAGE_ID'; |
| 15 | + |
| 16 | +// Mock response data |
| 17 | +const mockBlockResponse1 = { |
| 18 | + object: 'list', |
| 19 | + results: [ |
| 20 | + { |
| 21 | + object: 'block', |
| 22 | + id: 'block1', |
| 23 | + type: 'paragraph', |
| 24 | + paragraph: { text: [{ text: { content: 'Test content 1' } }] }, |
| 25 | + }, |
| 26 | + { |
| 27 | + object: 'block', |
| 28 | + id: 'block2', |
| 29 | + type: 'heading_1', |
| 30 | + heading_1: { text: [{ text: { content: 'Test heading' } }] }, |
| 31 | + }, |
| 32 | + ], |
| 33 | + next_cursor: 'cursor1', |
| 34 | + has_more: true, |
| 35 | +}; |
| 36 | + |
| 37 | +const mockBlockResponse2 = { |
| 38 | + object: 'list', |
| 39 | + results: [ |
| 40 | + { |
| 41 | + object: 'block', |
| 42 | + id: 'block3', |
| 43 | + type: 'numbered_list_item', |
| 44 | + numbered_list_item: { text: [{ text: { content: 'Test list item' } }] }, |
| 45 | + }, |
| 46 | + ], |
| 47 | + next_cursor: null, |
| 48 | + has_more: false, |
| 49 | +}; |
| 50 | + |
| 51 | +describe('getPageBlocks', () => { |
| 52 | + it('페이지의 모든 블록을 가져온다', async () => { |
| 53 | + const listMock = vi |
| 54 | + .fn() |
| 55 | + .mockResolvedValueOnce(mockBlockResponse1) |
| 56 | + .mockResolvedValueOnce(mockBlockResponse2); |
| 57 | + |
| 58 | + notionClient.blocks.children.list = listMock; |
| 59 | + |
| 60 | + const blocks = await notionClient.getPageBlocks(TEST_ID); |
| 61 | + expect(blocks).toHaveLength(3); // Total blocks from both responses |
| 62 | + expect(blocks[0]!.id).toBe('block1'); |
| 63 | + expect(blocks[1]!.id).toBe('block2'); |
| 64 | + expect(blocks[2]!.id).toBe('block3'); |
| 65 | + |
| 66 | + // Verify pagination handling |
| 67 | + expect(listMock).toHaveBeenCalledTimes(2); |
| 68 | + expect(listMock).toHaveBeenNthCalledWith(1, { |
| 69 | + block_id: TEST_ID, |
| 70 | + start_cursor: undefined, |
| 71 | + }); |
| 72 | + expect(listMock).toHaveBeenNthCalledWith(2, { |
| 73 | + block_id: TEST_ID, |
| 74 | + start_cursor: 'cursor1', |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('API 호출 실패시 빈 배열을 반환한다', async () => { |
| 79 | + notionClient.blocks.children.list = vi |
| 80 | + .fn() |
| 81 | + .mockRejectedValue(new Error('API Error')); |
| 82 | + |
| 83 | + const blocks = await notionClient.getPageBlocks(TEST_ID); |
| 84 | + |
| 85 | + expect(blocks).toEqual([]); |
| 86 | + expect(notionClient.blocks.children.list).toHaveBeenCalledWith({ |
| 87 | + block_id: TEST_ID, |
| 88 | + start_cursor: undefined, |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments