diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-12-01 17:59:30 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-12-01 18:06:46 +0100 |
| commit | 11e3ee75fcab0ab54b2bc1713a402c5cc3070c2d (patch) | |
| tree | 7cb478ac6b29f2b527eb3ec379b305b74dd4f0ba /src/utils/hooks/use-topic/use-topic.test.ts | |
| parent | dfdbf6cac1fe3719dc71e130129d28e04ba4e225 (diff) | |
refactor(pages): refine Topic pages
* add useTopic and useTopicsList hooks to refresh data
* add a table of contents
* add Cypress tests
Diffstat (limited to 'src/utils/hooks/use-topic/use-topic.test.ts')
| -rw-r--r-- | src/utils/hooks/use-topic/use-topic.test.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/utils/hooks/use-topic/use-topic.test.ts b/src/utils/hooks/use-topic/use-topic.test.ts new file mode 100644 index 0000000..e160a3e --- /dev/null +++ b/src/utils/hooks/use-topic/use-topic.test.ts @@ -0,0 +1,54 @@ +import { + afterEach, + beforeEach, + describe, + expect, + it, + jest, +} from '@jest/globals'; +import { renderHook, waitFor } from '@testing-library/react'; +import { wpTopicsFixture } from '../../../../tests/fixtures'; +import { ROUTES } from '../../constants'; +import { useTopic } from './use-topic'; + +describe('useTopic', () => { + beforeEach(() => { + /* Not sure why it is needed, but without it Jest was complaining with + * `Jest worker encountered 4 child process exceptions`... Maybe because of + * useSWR? */ + jest.useFakeTimers({ + doNotFake: ['queueMicrotask'], + }); + }); + + afterEach(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); + }); + + /* eslint-disable max-statements */ + it('fetch the requested topic', async () => { + const { result } = renderHook(() => useTopic(wpTopicsFixture[0].slug)); + + // Inaccurate assertions count because of waitFor... + //expect.assertions(8); + expect.hasAssertions(); + + expect(result.current.topic).toBeUndefined(); + expect(result.current.isError).toBe(false); + expect(result.current.isLoading).toBe(true); + expect(result.current.isValidating).toBe(true); + + jest.advanceTimersToNextTimer(); + + await waitFor(() => + expect(result.current.topic?.slug).toBe( + `${ROUTES.TOPICS}/${wpTopicsFixture[0].slug}` + ) + ); + expect(result.current.isError).toBe(false); + expect(result.current.isLoading).toBe(false); + expect(result.current.isValidating).toBe(false); + }); + /* eslint-enable max-statements */ +}); |
