From 11e3ee75fcab0ab54b2bc1713a402c5cc3070c2d Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 1 Dec 2023 17:59:30 +0100 Subject: refactor(pages): refine Topic pages * add useTopic and useTopicsList hooks to refresh data * add a table of contents * add Cypress tests --- src/utils/hooks/use-topics-list/index.ts | 1 + .../hooks/use-topics-list/use-topics-list.test.ts | 48 ++++++++++++++++++++++ src/utils/hooks/use-topics-list/use-topics-list.ts | 46 +++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 src/utils/hooks/use-topics-list/index.ts create mode 100644 src/utils/hooks/use-topics-list/use-topics-list.test.ts create mode 100644 src/utils/hooks/use-topics-list/use-topics-list.ts (limited to 'src/utils/hooks/use-topics-list') diff --git a/src/utils/hooks/use-topics-list/index.ts b/src/utils/hooks/use-topics-list/index.ts new file mode 100644 index 0000000..c08400f --- /dev/null +++ b/src/utils/hooks/use-topics-list/index.ts @@ -0,0 +1 @@ +export * from './use-topics-list'; diff --git a/src/utils/hooks/use-topics-list/use-topics-list.test.ts b/src/utils/hooks/use-topics-list/use-topics-list.test.ts new file mode 100644 index 0000000..c8fa607 --- /dev/null +++ b/src/utils/hooks/use-topics-list/use-topics-list.test.ts @@ -0,0 +1,48 @@ +import { + afterEach, + beforeEach, + describe, + expect, + it, + jest, +} from '@jest/globals'; +import { renderHook, waitFor } from '@testing-library/react'; +import { useTopicsList } from './use-topics-list'; + +describe('useTopicsList', () => { + 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 topics list', async () => { + const { result } = renderHook(() => useTopicsList()); + + // Inaccurate assertions count because of waitFor... + //expect.assertions(8); + expect.hasAssertions(); + + expect(result.current.topics).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.topics).toBeDefined()); + expect(result.current.isError).toBe(false); + expect(result.current.isLoading).toBe(false); + expect(result.current.isValidating).toBe(false); + }); + /* eslint-enable max-statements */ +}); diff --git a/src/utils/hooks/use-topics-list/use-topics-list.ts b/src/utils/hooks/use-topics-list/use-topics-list.ts new file mode 100644 index 0000000..7860486 --- /dev/null +++ b/src/utils/hooks/use-topics-list/use-topics-list.ts @@ -0,0 +1,46 @@ +import useSWR from 'swr'; +import { + type FetchTopicsListInput, + fetchTopicsList, +} from '../../../services/graphql'; +import type { GraphQLConnection, Maybe, WPTopicPreview } from '../../../types'; + +export type UseTopicsListReturn< + T extends Maybe>, +> = { + isError: boolean; + isLoading: boolean; + isValidating: boolean; + topics: T extends undefined + ? Maybe> + : GraphQLConnection; +}; + +export type UseTopicsListConfig< + T extends Maybe>, +> = { + input?: FetchTopicsListInput; + fallback?: T; +}; + +export const useTopicsList = < + T extends Maybe>, +>( + config?: UseTopicsListConfig +): UseTopicsListReturn => { + const { fallback, input } = config ?? {}; + const { data, error, isLoading, isValidating } = useSWR( + input ?? {}, + fetchTopicsList, + { fallbackData: fallback } + ); + + if (error) console.error(error); + + return { + isError: !!error, + isLoading, + isValidating, + topics: data, + } as UseTopicsListReturn; +}; -- cgit v1.2.3