|
| 1 | +import { render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 2 | +import { it, vi, expect, describe, beforeEach } from 'vitest'; |
| 3 | + |
| 4 | +import type { NoteWithHabit } from '@models'; |
| 5 | +import { useBoundStore } from '@stores'; |
| 6 | + |
| 7 | +import NotesList from './NotesList'; |
| 8 | + |
| 9 | +const listAllNotes = vi.hoisted(() => { |
| 10 | + return vi.fn(); |
| 11 | +}); |
| 12 | +const rollbarError = vi.hoisted(() => { |
| 13 | + return vi.fn(); |
| 14 | +}); |
| 15 | + |
| 16 | +vi.mock('@services', () => { |
| 17 | + return { listAllNotes }; |
| 18 | +}); |
| 19 | +vi.mock('@rollbar/react', () => { |
| 20 | + return { |
| 21 | + useRollbar: () => { |
| 22 | + return { error: rollbarError }; |
| 23 | + }, |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +vi.stubGlobal( |
| 28 | + 'ResizeObserver', |
| 29 | + class { |
| 30 | + disconnect = vi.fn(); |
| 31 | + observe = vi.fn(); |
| 32 | + unobserve = vi.fn(); |
| 33 | + } |
| 34 | +); |
| 35 | + |
| 36 | +const makeNote = (id: string, createdAt: string): NoteWithHabit => { |
| 37 | + return { |
| 38 | + content: `Content ${id}`, |
| 39 | + createdAt, |
| 40 | + id, |
| 41 | + periodDate: '2026-07-10', |
| 42 | + periodKind: 'day', |
| 43 | + updatedAt: null, |
| 44 | + userId: 'user-id', |
| 45 | + }; |
| 46 | +}; |
| 47 | + |
| 48 | +describe('NotesList', () => { |
| 49 | + beforeEach(() => { |
| 50 | + listAllNotes.mockReset(); |
| 51 | + rollbarError.mockReset(); |
| 52 | + useBoundStore.getState().noteActions.clearNotes(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('renders calendar notes from the store before the first page resolves', async () => { |
| 56 | + let resolvePage: ((notes: NoteWithHabit[]) => void) | undefined; |
| 57 | + listAllNotes.mockReturnValue( |
| 58 | + new Promise((resolve) => { |
| 59 | + resolvePage = resolve; |
| 60 | + }) |
| 61 | + ); |
| 62 | + const cachedNote = makeNote('cached', '2026-07-10T10:00:00.000Z'); |
| 63 | + |
| 64 | + useBoundStore.setState({ notes: { [cachedNote.id]: cachedNote } }); |
| 65 | + render(<NotesList />); |
| 66 | + |
| 67 | + expect(screen.getByText('Content cached')).toBeInTheDocument(); |
| 68 | + expect(listAllNotes).toHaveBeenCalledWith({ limit: 20, page: 0 }); |
| 69 | + |
| 70 | + resolvePage?.([]); |
| 71 | + |
| 72 | + await waitFor(() => { |
| 73 | + expect(useBoundStore.getState().notesListIsLoading).toBe(false); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('populates the Zustand store when the page is opened directly', async () => { |
| 78 | + const fetchedNote = makeNote('fetched', '2026-07-10T10:00:00.000Z'); |
| 79 | + listAllNotes.mockResolvedValue([fetchedNote]); |
| 80 | + |
| 81 | + render(<NotesList />); |
| 82 | + |
| 83 | + expect(await screen.findByText('Content fetched')).toBeInTheDocument(); |
| 84 | + expect(useBoundStore.getState().notes[fetchedNote.id]).toEqual(fetchedNote); |
| 85 | + }); |
| 86 | + |
| 87 | + it('fetches and caches subsequent pages while scrolling', async () => { |
| 88 | + const firstPage = Array.from({ length: 20 }, (_, index) => { |
| 89 | + return makeNote( |
| 90 | + `page-0-${index}`, |
| 91 | + `2026-07-10T10:${String(index).padStart(2, '0')}:00.000Z` |
| 92 | + ); |
| 93 | + }); |
| 94 | + const nextPageNote = makeNote('page-1-0', '2026-07-09T10:00:00.000Z'); |
| 95 | + listAllNotes |
| 96 | + .mockResolvedValueOnce(firstPage) |
| 97 | + .mockResolvedValueOnce([nextPageNote]); |
| 98 | + |
| 99 | + const { container } = render(<NotesList />); |
| 100 | + |
| 101 | + expect(await screen.findByText('Content page-0-0')).toBeInTheDocument(); |
| 102 | + |
| 103 | + const scrollContainer = container.querySelector('.overflow-y-auto'); |
| 104 | + |
| 105 | + expect(scrollContainer).not.toBeNull(); |
| 106 | + Object.defineProperties(scrollContainer!, { |
| 107 | + clientHeight: { configurable: true, value: 100 }, |
| 108 | + scrollHeight: { configurable: true, value: 100 }, |
| 109 | + scrollTop: { configurable: true, value: 0 }, |
| 110 | + }); |
| 111 | + fireEvent.scroll(scrollContainer!); |
| 112 | + |
| 113 | + expect(await screen.findByText('Content page-1-0')).toBeInTheDocument(); |
| 114 | + expect(listAllNotes).toHaveBeenLastCalledWith({ limit: 20, page: 1 }); |
| 115 | + expect(useBoundStore.getState().notes[nextPageNote.id]).toEqual( |
| 116 | + nextPageNote |
| 117 | + ); |
| 118 | + }); |
| 119 | +}); |
0 commit comments