|
| 1 | +import { renderHook, act, waitFor } from '@testing-library/react'; |
| 2 | +import { describe, test, expect, vi, beforeEach } from 'vitest'; |
| 3 | +import { Provider } from 'react-redux'; |
| 4 | +import type { PropsWithChildren } from 'react'; |
| 5 | +import useListFolderPaginated from './useListFolderPaginated'; |
| 6 | +import { MailService } from '@/services/sdk/mail'; |
| 7 | +import { getMockedMails } from '@/test-utils/fixtures'; |
| 8 | +import { createTestStore } from '@/test-utils/createTestStore'; |
| 9 | +import { DEFAULT_FOLDER_LIMIT } from '@/constants'; |
| 10 | +import type { FolderType } from '@/types/mail'; |
| 11 | + |
| 12 | +const createWrapper = (store: ReturnType<typeof createTestStore>) => { |
| 13 | + return ({ children }: PropsWithChildren) => <Provider store={store}>{children}</Provider>; |
| 14 | +}; |
| 15 | + |
| 16 | +describe('List Folder Paginated - custom hook', () => { |
| 17 | + beforeEach(() => { |
| 18 | + vi.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + test('When a folder is opened, then the first batch of emails is loaded and more are available', async () => { |
| 22 | + const page1 = getMockedMails(DEFAULT_FOLDER_LIMIT); |
| 23 | + page1.total = DEFAULT_FOLDER_LIMIT * 3; |
| 24 | + vi.spyOn(MailService.instance, 'listFolder').mockResolvedValue(page1); |
| 25 | + |
| 26 | + const store = createTestStore(); |
| 27 | + const { result } = renderHook(() => useListFolderPaginated('inbox'), { |
| 28 | + wrapper: createWrapper(store), |
| 29 | + }); |
| 30 | + |
| 31 | + waitFor(() => { |
| 32 | + expect(result.current.isLoadingListFolder).toBe(true); |
| 33 | + |
| 34 | + expect(result.current.listFolder?.emails).toHaveLength(DEFAULT_FOLDER_LIMIT); |
| 35 | + expect(result.current.hasMore).toBeTruthy(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + test('When all emails in the folder have been loaded, then no more are available', async () => { |
| 40 | + const page = getMockedMails(DEFAULT_FOLDER_LIMIT); |
| 41 | + page.total = DEFAULT_FOLDER_LIMIT; |
| 42 | + vi.spyOn(MailService.instance, 'listFolder').mockResolvedValue(page); |
| 43 | + |
| 44 | + const store = createTestStore(); |
| 45 | + const { result } = renderHook(() => useListFolderPaginated('inbox'), { |
| 46 | + wrapper: createWrapper(store), |
| 47 | + }); |
| 48 | + |
| 49 | + expect(result.current.hasMore).toBeFalsy(); |
| 50 | + }); |
| 51 | + |
| 52 | + test('When the user scrolls to the end of the list, then the next batch of emails is loaded and appended', async () => { |
| 53 | + const page1 = getMockedMails(DEFAULT_FOLDER_LIMIT); |
| 54 | + const page2 = getMockedMails(DEFAULT_FOLDER_LIMIT); |
| 55 | + page1.total = DEFAULT_FOLDER_LIMIT * 2; |
| 56 | + page2.total = DEFAULT_FOLDER_LIMIT * 2; |
| 57 | + |
| 58 | + vi.spyOn(MailService.instance, 'listFolder').mockResolvedValueOnce(page1).mockResolvedValueOnce(page2); |
| 59 | + |
| 60 | + const store = createTestStore(); |
| 61 | + const { result } = renderHook(() => useListFolderPaginated('inbox'), { |
| 62 | + wrapper: createWrapper(store), |
| 63 | + }); |
| 64 | + |
| 65 | + act(() => { |
| 66 | + result.current.onLoadMore(); |
| 67 | + }); |
| 68 | + |
| 69 | + waitFor(() => { |
| 70 | + expect(result.current.listFolder?.emails).toHaveLength(DEFAULT_FOLDER_LIMIT * 2); |
| 71 | + expect(result.current.listFolder?.emails).toStrictEqual([...page1.emails, ...page2.emails]); |
| 72 | + expect(result.current.hasMore).toBeFalsy(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + test('When the user scrolls while emails are still loading, then no duplicate request is made', async () => { |
| 77 | + const page1 = getMockedMails(DEFAULT_FOLDER_LIMIT); |
| 78 | + page1.total = DEFAULT_FOLDER_LIMIT * 2; |
| 79 | + const listFolderSpy = vi.spyOn(MailService.instance, 'listFolder').mockResolvedValue(page1); |
| 80 | + |
| 81 | + const store = createTestStore(); |
| 82 | + const { result } = renderHook(() => useListFolderPaginated('inbox'), { |
| 83 | + wrapper: createWrapper(store), |
| 84 | + }); |
| 85 | + |
| 86 | + act(() => { |
| 87 | + result.current.onLoadMore(); |
| 88 | + }); |
| 89 | + |
| 90 | + expect(listFolderSpy).toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + test('When the user navigates to a different folder, then only the emails from the new folder are shown', async () => { |
| 94 | + const inboxEmails = getMockedMails(DEFAULT_FOLDER_LIMIT); |
| 95 | + const sentEmails = getMockedMails(DEFAULT_FOLDER_LIMIT); |
| 96 | + inboxEmails.total = DEFAULT_FOLDER_LIMIT; |
| 97 | + sentEmails.total = DEFAULT_FOLDER_LIMIT; |
| 98 | + |
| 99 | + vi.spyOn(MailService.instance, 'listFolder').mockResolvedValueOnce(inboxEmails).mockResolvedValueOnce(sentEmails); |
| 100 | + |
| 101 | + const store = createTestStore(); |
| 102 | + const { result, rerender } = renderHook(({ mailbox }) => useListFolderPaginated(mailbox), { |
| 103 | + initialProps: { mailbox: 'inbox' as FolderType }, |
| 104 | + wrapper: createWrapper(store), |
| 105 | + }); |
| 106 | + |
| 107 | + waitFor(() => { |
| 108 | + expect(result.current.listFolder?.emails).toStrictEqual(inboxEmails.emails); |
| 109 | + }); |
| 110 | + |
| 111 | + rerender({ mailbox: 'sent' }); |
| 112 | + |
| 113 | + waitFor(() => { |
| 114 | + expect(result.current.listFolder?.emails).toStrictEqual(sentEmails.emails); |
| 115 | + expect(result.current.listFolder?.emails).toHaveLength(DEFAULT_FOLDER_LIMIT); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments