aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.test.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-12-06 18:20:54 +0100
committerArmand Philippot <git@armandphilippot.com>2023-12-07 19:12:11 +0100
commitb8eb008dd5927fb736e56699637f5f8549965eae (patch)
tree648274babea3d3d09ed3e0f5f1fef013f94158fb /src/utils/hooks/use-github-repo-meta/use-github-repo-meta.test.ts
parent802285872a2c57e7a5e130f32a2b45497d7687f1 (diff)
refactor(hooks): replace useGithubApi with useGithubRepoMeta
* use GraphQL API instead of REST (the inconvenient however is that we now need an authorization token...) * move fetcher in services * add tests * mock response using MSW
Diffstat (limited to 'src/utils/hooks/use-github-repo-meta/use-github-repo-meta.test.ts')
-rw-r--r--src/utils/hooks/use-github-repo-meta/use-github-repo-meta.test.ts59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.test.ts b/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.test.ts
new file mode 100644
index 0000000..d796f69
--- /dev/null
+++ b/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.test.ts
@@ -0,0 +1,59 @@
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ jest,
+} from '@jest/globals';
+import { renderHook, waitFor } from '@testing-library/react';
+import { githubRepos } from '../../../../tests/fixtures';
+import { useGithubRepoMeta } from './use-github-repo-meta';
+
+describe('useGithubRepoMeta', () => {
+ 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('fetches the requested repository', async () => {
+ const { result } = renderHook(() =>
+ useGithubRepoMeta({
+ name: githubRepos[0].name,
+ owner: githubRepos[0].owner,
+ })
+ );
+
+ // Inaccurate assertions count because of waitFor...
+ //expect.assertions(11);
+ expect.hasAssertions();
+
+ expect(result.current.meta).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.meta).toBeDefined());
+ expect(result.current.meta?.createdAt).toBe(githubRepos[0].createdAt);
+ expect(result.current.meta?.stargazerCount).toBe(
+ githubRepos[0].stargazerCount
+ );
+ expect(result.current.meta?.updatedAt).toBe(githubRepos[0].updatedAt);
+ expect(result.current.isError).toBe(false);
+ expect(result.current.isLoading).toBe(false);
+ expect(result.current.isValidating).toBe(false);
+ });
+ /* eslint-enable max-statements */
+});