Skip to content

Commit

Permalink
✅ improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaraku committed Jul 12, 2024
1 parent 1b8a069 commit a4a962d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
24 changes: 21 additions & 3 deletions src/modules/user/hooks/useUser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { vi, describe, it, expect, afterEach } from 'vitest';
import { renderHook, waitFor, cleanup } from '@testing-library/preact';
import { TestQueryClientProvider } from '@/modules/test';
import { useUser } from './useUser';
import { getUser } from '../api';

vi.mock('../api', () => ({
getUser: vi.fn(
() => Promise.resolve({ username: 'dummy', name: 'Dummy' }),
),
getUser: vi.fn(),
}) satisfies Partial<typeof import('../api')>);

afterEach(() => {
Expand All @@ -15,6 +14,8 @@ afterEach(() => {

describe('useUser()', () => {
it('should be able to fetch user', async () => {
vi.mocked(getUser).mockResolvedValueOnce({ username: 'dummy', name: 'Dummy' });

const r = renderHook(() => useUser('dummy'), {
wrapper: ({ children }) => (
<TestQueryClientProvider>
Expand All @@ -28,4 +29,21 @@ describe('useUser()', () => {
expect(r.result.current.user).toMatchObject({ username: 'dummy', name: 'Dummy' });
});
});

it('should return undefined on error', async () => {
vi.mocked(getUser).mockRejectedValueOnce(new Error('Failed to fetch.'));

const r = renderHook(() => useUser('dummy'), {
wrapper: ({ children }) => (
<TestQueryClientProvider>
{children}
</TestQueryClientProvider>
),
});

await waitFor(() => {
expect(r.result.current.user).not.toBe(null);
expect(r.result.current.user).toBe(undefined);
});
});
});
28 changes: 23 additions & 5 deletions src/modules/user/hooks/useUsers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { vi, describe, it, expect, afterEach } from 'vitest';
import { renderHook, waitFor, cleanup } from '@testing-library/preact';
import { TestQueryClientProvider } from '@/modules/test';
import { useUsers } from './useUsers';
import { getUsers } from '../api';

vi.mock('../api', () => ({
getUsers: vi.fn(
() => Promise.resolve([
{ username: 'dummy', name: 'Dummy' },
]),
),
getUsers: vi.fn(),
}) satisfies Partial<typeof import('../api')>);

afterEach(() => {
Expand All @@ -17,6 +14,10 @@ afterEach(() => {

describe('useUsers()', () => {
it('should be able to fetch users', async () => {
vi.mocked(getUsers).mockResolvedValueOnce([
{ username: 'dummy', name: 'Dummy' },
]);

const r = renderHook(() => useUsers(), {
wrapper: ({ children }) => (
<TestQueryClientProvider>
Expand All @@ -32,4 +33,21 @@ describe('useUsers()', () => {
]);
});
});

it('should return undefined on error', async () => {
vi.mocked(getUsers).mockRejectedValueOnce(new Error('Failed to fetch.'));

const r = renderHook(() => useUsers(), {
wrapper: ({ children }) => (
<TestQueryClientProvider>
{children}
</TestQueryClientProvider>
),
});

await waitFor(() => {
expect(r.result.current.users).not.toBe(null);
expect(r.result.current.users).toBe(undefined);
});
});
});

0 comments on commit a4a962d

Please sign in to comment.