SoYoung210 / soso-tip

🍯소소한 팁들과 정리, 버그 해결기를 모아두는 레포
24 stars 0 forks source link

react-testing-library/react-hooks with swr #52

Open SoYoung210 opened 3 years ago

SoYoung210 commented 3 years ago

cache clear안될때

import React, { ReactNode } from 'react';

import { SWRConfig } from 'swr';

/**
 * @description https://github.com/vercel/swr/pull/231#issuecomment-591614747
 */
const SWRNoCache = ({ children }: { children: ReactNode }) => (
  <SWRConfig value={{ dedupingInterval: 0 }}>{children}</SWRConfig>
);

export default SWRNoCache;

no cache wrapper를 추가하고,

const { result, waitForNextUpdate } = renderHook(useFoo, {
  wrapper: SWRNoCache,
}
);

renderHookwrapper로 넘겨준다.

그리고..

import { waitFor } from '@testing-library/react';
import { cache } from 'swr';

afterEach(async () => {
  await waitFor(() => cache.clear());
});

잊지말고 cache clear

Reference

간단한 비동기 액션인데 잘 안될때

react-test-renderer등 peerDependency설치했는지 살펴보자..