Skip to content

Commit

Permalink
[Fix] React Testing library react-hooks의 console.error가 나타나는 문제를 수정하라
Browse files Browse the repository at this point in the history
- react-hooks console.error 수정
- react-test-renderer dependency 추가 (해당 버전에 맞는 peer dependency 사용)
  • Loading branch information
saseungmin committed May 4, 2022
1 parent c3c7ed5 commit 6bce4ec
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 71 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"@svgr/webpack": "^6.2.1",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.1.1",
"@testing-library/react-hooks": "^8.0.0",
"@types/facepaint": "^1.2.2",
"@types/jest": "^27.5.0",
"@types/jest-plugin-context": "^2.9.4",
Expand Down Expand Up @@ -122,6 +123,7 @@
"jest-plugin-context": "^2.9.0",
"lint-staged": "^12.1.2",
"node-mocks-http": "^1.11.0",
"react-test-renderer": "^18.1.0",
"start-server-and-test": "^1.14.0",
"typescript": "^4.6.4"
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/home/TagsBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import RecoilObserver from '@/test/RecoilObserver';
import TagsBar from './TagsBar';

jest.mock('nanoid', () => ({
nanoid: jest.fn().mockImplementation(() => '12345'),
nanoid: jest.fn().mockImplementation(() => Math.random()),
}));

describe('TagsBar', () => {
Expand Down
19 changes: 11 additions & 8 deletions src/components/write/ThumbnailUpload.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
act,
fireEvent, render, screen, waitFor,
act, fireEvent, render, screen, waitFor,
} from '@testing-library/react';

import useFetchUserProfile from '@/hooks/api/auth/useFetchUserProfile';
Expand Down Expand Up @@ -81,9 +80,11 @@ describe('ThumbnailUpload', () => {
});
});

await waitFor(async () => expect(mutate).toBeCalledWith({
userUid: FIXTURE_PROFILE.uid, thumbnail: file,
}));
await waitFor(
async () => expect(mutate).toBeCalledWith({
userUid: FIXTURE_PROFILE.uid, thumbnail: file,
}),
);
});
});
});
Expand All @@ -105,9 +106,11 @@ describe('ThumbnailUpload', () => {
});
});

await waitFor(async () => expect(mutate).toBeCalledWith({
userUid: FIXTURE_PROFILE.uid, thumbnail: file,
}));
await waitFor(
async () => expect(mutate).toBeCalledWith({
userUid: FIXTURE_PROFILE.uid, thumbnail: file,
}),
);

act(() => {
fireEvent.click(screen.getByTestId('close-icon'));
Expand Down
14 changes: 9 additions & 5 deletions src/hooks/api/alarm/useFetchAlarms.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import { getUserAlarm } from '@/services/api/alarm';
import wrapper from '@/test/ReactQueryWrapper';
Expand Down Expand Up @@ -28,19 +28,23 @@ describe('useFetchAlarms', () => {
given('alarms', () => null);

it('빈 배열을 반환해야만 한다', async () => {
const { result } = useFetchAlarmsHook();
const { result, waitFor } = useFetchAlarmsHook();

await waitFor(() => expect(result.current.data).toEqual([]));
await waitFor(() => result.current.isSuccess);

expect(result.current.data).toEqual([]);
});
});

context('useQuery반환값이 존재하는 경우', () => {
given('alarms', () => [ALARM_FIXTURE]);

it('alarms에 대한 정보를 반환해야만 한다', async () => {
const { result } = useFetchAlarmsHook();
const { result, waitFor } = useFetchAlarmsHook();

await waitFor(() => result.current.isSuccess);

await waitFor(() => expect(result.current.data).toEqual([ALARM_FIXTURE]));
expect(result.current.data).toEqual([ALARM_FIXTURE]);
});
});
});
14 changes: 9 additions & 5 deletions src/hooks/api/alarm/useFetchAlertAlarms.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import { getUserAlertAlarm } from '@/services/api/alarm';
import wrapper from '@/test/ReactQueryWrapper';
Expand Down Expand Up @@ -28,19 +28,23 @@ describe('useFetchAlertAlarms', () => {
given('alarms', () => null);

it('빈 배열을 반환해야만 한다', async () => {
const { result } = useFetchAlertAlarmsHook();
const { result, waitFor } = useFetchAlertAlarmsHook();

await waitFor(() => expect(result.current.data).toEqual([]));
await waitFor(() => result.current.isSuccess);

expect(result.current.data).toEqual([]);
});
});

context('useQuery반환값이 존재하는 경우', () => {
given('alarms', () => [ALARM_FIXTURE]);

it('alarms에 대한 정보를 반환해야만 한다', async () => {
const { result } = useFetchAlertAlarmsHook();
const { result, waitFor } = useFetchAlertAlarmsHook();

await waitFor(() => result.current.isSuccess);

await waitFor(() => expect(result.current.data).toEqual([ALARM_FIXTURE]));
expect(result.current.data).toEqual([ALARM_FIXTURE]);
});
});
});
20 changes: 12 additions & 8 deletions src/hooks/api/applicant/useFetchApplicants.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { renderHook, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import { getApplicants } from '@/services/api/applicants';
import wrapper from '@/test/InjectMockProviders';
import wrapper from '@/test/ReactQueryWrapper';

import FIXTURE_APPLICANT from '../../../../fixtures/applicant';

Expand All @@ -17,31 +17,35 @@ jest.mock('next/router', () => ({
}));

describe('useFetchApplicants', () => {
const useFetchApplicantsHook = () => renderHook(() => useFetchApplicants(), { wrapper });

beforeEach(() => {
jest.clearAllMocks();

(getApplicants as jest.Mock).mockImplementation(() => (given.applicants));
});

const useFetchApplicantsHook = () => renderHook(() => useFetchApplicants(), { wrapper });

context('useQuery반환값이 존재하지 않는 경우', () => {
given('applicants', () => null);

it('빈 배열을 반환해야만 한다', async () => {
const { result } = useFetchApplicantsHook();
const { result, waitFor } = useFetchApplicantsHook();

await waitFor(() => result.current.isSuccess);

await waitFor(() => expect(result.current.data).toEqual([]));
expect(result.current.data).toEqual([]);
});
});

context('useQuery반환값이 존재하는 경우', () => {
given('applicants', () => [FIXTURE_APPLICANT]);

it('applicants에 대한 정보를 반환해야만 한다', async () => {
const { result } = useFetchApplicantsHook();
const { result, waitFor } = useFetchApplicantsHook();

await waitFor(() => result.current.isSuccess);

await waitFor(() => expect(result.current.data).toEqual([FIXTURE_APPLICANT]));
expect(result.current.data).toEqual([FIXTURE_APPLICANT]);
});
});
});
14 changes: 9 additions & 5 deletions src/hooks/api/auth/useFetchUserProfile.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import { getUserProfile } from '@/services/api/auth';
import wrapper from '@/test/ReactQueryWrapper';
Expand Down Expand Up @@ -38,9 +38,11 @@ describe('useFetchUserProfile', () => {
given('isLoading', () => false);

it('user에 대한 profile 정보를 반환해야만 한다', async () => {
const { result } = useFetchUserProfileHook();
const { result, waitFor } = useFetchUserProfileHook();

await waitFor(() => expect(result.current.data).toEqual(FIXTURE_PROFILE));
await waitFor(() => result.current.isSuccess);

expect(result.current.data).toEqual(FIXTURE_PROFILE);
});
});

Expand All @@ -49,9 +51,11 @@ describe('useFetchUserProfile', () => {
given('isLoading', () => true);

it('user에 대한 profile 정보를 반환해야만 한다', async () => {
const { result } = useFetchUserProfileHook();
const { result, waitFor } = useFetchUserProfileHook();

await waitFor(() => result.current.isSuccess);

await waitFor(() => expect(result.current.data).toEqual(null));
expect(result.current.data).toEqual(null);
});
});
});
14 changes: 9 additions & 5 deletions src/hooks/api/auth/useGetUser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAuthUser } from '@react-query-firebase/auth';
import { renderHook, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import wrapper from '@/test/ReactQueryWrapper';

Expand All @@ -24,19 +24,23 @@ describe('useGetUser', () => {
given('user', () => null);

it('user에 대한 정보를 반환해야만 한다', async () => {
const { result } = useGetUserHook();
const { result, waitFor } = useGetUserHook();

await waitFor(() => expect(result.current.data).toEqual(null));
await waitFor(() => result.current.isSuccess);

expect(result.current.data).toEqual(null);
});
});

context('user.data가 존재하는 경우', () => {
given('user', () => FIXTURE_PROFILE);

it('user에 대한 정보를 반환해야만 한다', async () => {
const { result } = useGetUserHook();
const { result, waitFor } = useGetUserHook();

await waitFor(() => result.current.isSuccess);

await waitFor(() => expect(result.current.data).toEqual(FIXTURE_PROFILE));
expect(result.current.data).toEqual(FIXTURE_PROFILE);
});
});
});
14 changes: 9 additions & 5 deletions src/hooks/api/comment/useFetchComments.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import { getGroupComments } from '@/services/api/comment';
import wrapper from '@/test/InjectMockProviders';
Expand Down Expand Up @@ -29,19 +29,23 @@ describe('useFetchComments', () => {
given('comments', () => null);

it('빈 배열을 반환해야만 한다', async () => {
const { result } = useFetchCommentsHook();
const { result, waitFor } = useFetchCommentsHook();

await waitFor(() => expect(result.current.data).toEqual([]));
await waitFor(() => result.current.isSuccess);

expect(result.current.data).toEqual([]);
});
});

context('useQuery반환값이 존재하는 경우', () => {
given('comments', () => [FIXTURE_COMMENT]);

it('comments에 대한 정보를 반환해야만 한다', async () => {
const { result } = useFetchCommentsHook();
const { result, waitFor } = useFetchCommentsHook();

await waitFor(() => result.current.isSuccess);

await waitFor(() => expect(result.current.data).toEqual([FIXTURE_COMMENT]));
expect(result.current.data).toEqual([FIXTURE_COMMENT]);
});
});
});
8 changes: 5 additions & 3 deletions src/hooks/api/group/useFetchGroup.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import { getGroupDetail } from '@/services/api/group';
import wrapper from '@/test/ReactQueryWrapper';
Expand Down Expand Up @@ -26,9 +26,11 @@ describe('useFetchGroup', () => {
});

it('group에 대한 정보를 반환해야만 한다', async () => {
const { result } = useFetchGroupHook();
const { result, waitFor } = useFetchGroupHook();

await waitFor(() => result.current.isSuccess);

expect(getGroupDetail).toBeCalledWith('groupId');
await waitFor(() => expect(result.current.data).toEqual(FIXTURE_GROUP));
expect(result.current.data).toEqual(FIXTURE_GROUP);
});
});
14 changes: 9 additions & 5 deletions src/hooks/api/group/useFetchGroups.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import { getFilteredGroups } from '@/services/api/group';
import wrapper from '@/test/InjectMockProviders';
Expand All @@ -22,19 +22,23 @@ describe('useFetchGroups', () => {
given('groups', () => null);

it('빈 배열을 반환해야만 한다', async () => {
const { result } = useFetchGroupsHook();
const { result, waitFor } = useFetchGroupsHook();

await waitFor(() => expect(result.current.data).toEqual([]));
await waitFor(() => result.current.isSuccess);

expect(result.current.data).toEqual([]);
});
});

context('useQuery반환값이 존재하는 경우', () => {
given('groups', () => [FIXTURE_GROUP]);

it('groups에 대한 정보를 반환해야만 한다', async () => {
const { result } = useFetchGroupsHook();
const { result, waitFor } = useFetchGroupsHook();

await waitFor(() => result.current.isSuccess);

await waitFor(() => expect(result.current.data).toEqual([FIXTURE_GROUP]));
expect(result.current.data).toEqual([FIXTURE_GROUP]);
});
});
});
13 changes: 8 additions & 5 deletions src/hooks/api/group/useFetchUserAppliedGroups.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import { getUserAppliedGroups } from '@/services/api/applicants';
import wrapper from '@/test/ReactQueryWrapper';
Expand All @@ -24,19 +24,22 @@ describe('useFetchUserAppliedGroups', () => {
given('groups', () => null);

it('빈 배열을 반환해야만 한다', async () => {
const { result } = useFetchUserAppliedGroupsHook();
const { result, waitFor } = useFetchUserAppliedGroupsHook();

await waitFor(() => expect(result.current.data).toEqual([]));
await waitFor(() => result.current.isSuccess);

expect(result.current.data).toEqual([]);
});
});

context('useQuery 반환값이 존재하는 경우', () => {
given('groups', () => [FIXTURE_GROUP]);

it('groups에 대한 정보를 반환해야만 한다', async () => {
const { result } = useFetchUserAppliedGroupsHook();
const { result, waitFor } = useFetchUserAppliedGroupsHook();
await waitFor(() => result.current.isSuccess);

await waitFor(() => expect(result.current.data).toEqual([FIXTURE_GROUP]));
expect(result.current.data).toEqual([FIXTURE_GROUP]);
});
});
});
Loading

0 comments on commit 6bce4ec

Please sign in to comment.