import {render, screen} from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import {describe, expect, it, vi} from 'vitest'; import FeedMore from './FeedMore'; function stubReplies(...replies) { const fetch = vi.fn(); replies.forEach((data) => fetch.mockResolvedValueOnce({ok: true, status: 200, json: async () => data})); vi.stubGlobal('fetch', fetch); return fetch; } describe('FeedMore', () => { it('shows the button', () => { render(); expect(screen.getByRole('button', {name: 'Show earlier days'})).toBeInTheDocument(); }); it('fetches the chunk it was given', async () => { const fetch = stubReplies({html: '

Monday

', next: null}); render(); await userEvent.click(screen.getByRole('button')); expect(fetch).toHaveBeenCalledWith('/feed?page=2', expect.anything()); }); it('adds the chunk below', async () => { stubReplies({html: '

Monday

', next: null}); render(); await userEvent.click(screen.getByRole('button')); expect(await screen.findByText('Monday')).toBeInTheDocument(); }); it('fetches the next chunk the reply points to', async () => { const fetch = stubReplies({html: '

Monday

', next: '/feed?page=3'}, {html: '

Sunday

', next: null}); render(); await userEvent.click(screen.getByRole('button')); await screen.findByText('Monday'); await userEvent.click(screen.getByRole('button')); expect(fetch).toHaveBeenLastCalledWith('/feed?page=3', expect.anything()); }); describe('when there is no more', () => { it('removes the button', async () => { stubReplies({html: '

Monday

', next: null}); render(); await userEvent.click(screen.getByRole('button')); await screen.findByText('Monday'); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); }); });