Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#353 유틸함수 테스트 보강 #356

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions frontend/src/shared/hooks/useDebounceEffect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { renderHook, act } from '@testing-library/react';
import useDebounceEffect from './useDebounceEffect';

jest.useFakeTimers();

describe('useDebounceEffect 테스트. debounce 제한 딜레이를 0.5초로 둘 경우', () => {
Copy link
Collaborator

@Creative-Lee Creative-Lee Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debounce 제한 딜레이를 0.5초로 둘 경우 이 부분은 아래 테스트 명으로 들어가야 될 것 같아요!

Copy link
Collaborator

@Creative-Lee Creative-Lee Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

의도 따로 확인했습니다 ㅎㅎ

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 아래 경우에도 통과하고 있는데, 테스트를 어떻게 짜야할지 고민해봐야 할 것 같아요

  test('0.499초 지났을 때, 한 번도 실행되지 않는다.', () => {
    const fn = jest.fn();
    const { rerender } = renderHook(({ deps, delay }) => useDebounceEffect(fn, deps, delay), {
      initialProps: { deps: 'dependency1', delay: 500 },
    });

    expect(fn).not.toBeCalled();

    rerender({ deps: 'dependency2', delay: 5000 });

    act(() => {
      jest.advanceTimersByTime(4000);
    });

    rerender({ deps: 'dependency4', delay: 5000 });

    // 0ms
    expect(fn).not.toBeCalled();

    act(() => {
      jest.advanceTimersByTime(1000);
    });

    // 1000ms
    // fn이 실행되지 않아야 하는데 실행됨
    expect(fn).not.toBeCalled();
  });

test('0.499초 지났을 때, 한 번도 실행되지 않는다.', () => {
const fn = jest.fn();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 useFetch 테스트와 마찬가지로 beforeEach를 활용하면 좋을 것 같아요!

const { rerender } = renderHook(({ deps, delay }) => useDebounceEffect(fn, deps, delay), {
initialProps: { deps: 'dependency1', delay: 500 },
});

expect(fn).not.toBeCalled();

rerender({ deps: 'dependency2', delay: 500 });
rerender({ deps: 'dependency3', delay: 500 });
rerender({ deps: 'dependency4', delay: 500 });

act(() => {
jest.advanceTimersByTime(499);
});

expect(fn).not.toBeCalled();
});

test('0.5초 지났을 때, 한 번만 실행된다.', () => {
const fn = jest.fn();
const { rerender } = renderHook(({ deps, delay }) => useDebounceEffect(fn, deps, delay), {
initialProps: { deps: 'dependency1', delay: 500 },
});

expect(fn).not.toBeCalled();

rerender({ deps: 'dependency2', delay: 500 });
rerender({ deps: 'dependency3', delay: 500 });
rerender({ deps: 'dependency4', delay: 500 });

act(() => {
jest.advanceTimersByTime(500);
});

expect(fn).toBeCalledTimes(1);
});
});
38 changes: 38 additions & 0 deletions frontend/src/shared/hooks/useFetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { renderHook, waitFor } from '@testing-library/react';
import useFetch from './useFetch';

describe('useFetch 테스트', () => {
let fetcher: jest.Mock;

beforeEach(() => {
fetcher = jest.fn();
});

test('fetch가 성공했을 경우, isLoading 은 false, data는 mockData, error은 null이다.', async () => {
const mockData = { key: 'value' };
fetcher.mockResolvedValueOnce(mockData);
const { result } = renderHook(() => useFetch(fetcher, true));

expect(result.current.isLoading).toBe(true);

waitFor(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 여기는 await가 안붙고 아래 실패케이스에는 붙는데 어떤 차이인가요?

expect(result.current.isLoading).toBe(false);
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBe(null);
});
});

test('fetch가 실패했을 경우, isLoading 은 false, datas는 null, error은 mockError다.', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*data
오타발견!

const mockError = { message: 'An error occurred' };
fetcher.mockRejectedValueOnce(mockError);
const { result } = renderHook(() => useFetch(fetcher, true));

expect(result.current.isLoading).toBe(true);

await waitFor(() => {
expect(result.current.isLoading).toBe(false);
expect(result.current.data).toBe(null);
expect(result.current.error).toEqual(mockError);
});
});
});
46 changes: 46 additions & 0 deletions frontend/src/shared/hooks/useMutation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { renderHook, act, waitFor } from '@testing-library/react';
import { useMutation } from './useMutation';

describe('useMutation 테스트', () => {
let mutateFn: jest.Mock;

beforeEach(() => {
mutateFn = jest.fn();
});

test('fetch가 성공했을 경우, isLoading 은 false, data는 mockData, error은 null이다.', async () => {
const mockData = { key: 'value' };
mutateFn.mockResolvedValueOnce(mockData);
const { result } = renderHook(() => useMutation(mutateFn));

act(() => {
result.current.mutateData();
});

expect(result.current.isLoading).toBe(true);

await waitFor(() => {
expect(result.current.isLoading).toBe(false);
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBe(null);
});
});

test('fetch가 실패했을 경우, isLoading 은 false, datas는 null, error은 mockError다.', async () => {
const mockError = { message: 'An error occurred' };
mutateFn.mockRejectedValueOnce(mockError);
const { result } = renderHook(() => useMutation(mutateFn));

act(() => {
result.current.mutateData();
});

expect(result.current.isLoading).toBe(true);

await waitFor(() => {
expect(result.current.isLoading).toBe(false);
expect(result.current.data).toBe(null);
expect(result.current.error).toEqual(mockError);
});
});
});
23 changes: 23 additions & 0 deletions frontend/src/shared/utils/formatOrdinals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import formatOrdinals from '@/shared/utils/formatOrdinals';

describe('formatOrdinals', () => {
test('숫자 1은 1st를 반환한다.', () => {
expect(formatOrdinals(1)).toBe('1st');
});

test('숫자 2은 1nd를 반환한다.', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트명에 오류가 있네요~! ㅎㅎ

expect(formatOrdinals(2)).toBe('2nd');
});

test('숫자 3은 1rd를 반환한다.', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도여~

expect(formatOrdinals(3)).toBe('3rd');
});

test('숫자 4은 4th를 반환한다.', () => {
expect(formatOrdinals(4)).toBe('4th');
});

test('숫자 11은 11th를 반환한다.', () => {
expect(formatOrdinals(11)).toBe('11th');
});
});