how to reset atom in unit test?
See original GitHub issueI know I can follow the doc here: https://docs.pmnd.rs/jotai/guides/resettable in my code to achieve this. But I’m writing unit tests in my project, which it appears that the atom value would be persisted somehow, but I think all the atom values used in different unit tests should not be the same.
my hook is defined below:
export const stepAtom = atom(0);
export function useOnBoardingService() {
const { width } = useWindowDimensions();
const [currentIndex, updateStep] = useAtom(stepAtom);
const slidePrev = useCallback(() => {
if (currentIndex !== 0) {
const step = currentIndex - 1;
updateStep(step);
}
}, [currentIndex, updateStep]);
const slideNext = useCallback(() => {
if (currentIndex !== 3) {
const step = currentIndex + 1;
updateStep(step);
}
}, [currentIndex, updateStep]);
return {
slidePrev,
slideNext,
};
}
and my unit test is below:
import { renderHook, act } from '@testing-library/react-hooks';
import { useAtomValue } from 'jotai/utils';
import { useOnBoardingService, stepAtom } from './useOnBoardingService';
test('should useOnBoardingService', () => {
const { result } = renderHook(() => useOnBoardingService());
expect(typeof result.current.slidePrev).toBe('function');
});
test('should step be 1', () => {
const { result } = renderHook(() => useOnBoardingService());
const { result: stepResult } = renderHook(() => useAtomValue(stepAtom));
expect(stepResult.current).toBe(0);
act(() => {
result.current.slideNext();
});
expect(stepResult.current).toBe(1);
});
test('should step be 1 too', () => {
const { result } = renderHook(() => useOnBoardingService());
const { result: stepResult } = renderHook(() => useAtomValue(stepAtom));
// I want this to be 0 but it actually is 1 because atom value changed after the above test executed
expect(stepResult.current).toBe(0);
act(() => {
result.current.slideNext();
});
// actual value would be 2
expect(stepResult.current).toBe(1);
});
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to reset unit test? - angularjs - Stack Overflow
Since you are appending the element to the body, you need to manually remove it too. Add this line to end of each...
Read more >Testing - Recoil
Testing Recoil state inside of a React component. ... useRecoilValue and useEffect , to observe an atom / selector 's changes and execute...
Read more >Clean Unit Tests with Mockito - Reflectoring
Normally, you don't need to reset your mocks, just create new mocks for each test method. We better create simple and small test...
Read more >atomWithReset - Jotai
Creates an atom that could be reset to its initialValue with useResetAtom hook. It works exactly the same way as primitive atom would,...
Read more >Reset Unit Tests - Khan Academy Help Center
Why can't we reset the unit tests/quizzes after messing up? I've been at this for hours and cannot get 100% because it makes...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
atomWithReset is not primarily for testing. Provider is. jotai is based on react context and to isolate state within memory, we need to use
<Provider>
.According to https://react-hooks-testing-library.com/usage/advanced-hooks#context
This may help.
I’d personally do that even with useState. It’s more about how you organize your code in React in general. The library is rather unopinionated about the code structure. Happy to see someone come up with best practices.