mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
Update usePrevious tests
This commit is contained in:
parent
f058ff9739
commit
4ae76748cd
@ -1,20 +1,23 @@
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import usePrevious from '../usePrevious';
|
||||
|
||||
describe('usePrevious', () => {
|
||||
it('should be defined', () => {
|
||||
expect(usePrevious).toBeDefined();
|
||||
});
|
||||
const setUp = () => renderHook(({ state }) => usePrevious(state), { initialProps: { state: 0 } });
|
||||
|
||||
const hook = renderHook(props => usePrevious(props), { initialProps: 0 });
|
||||
it('should return undefined on initial render', () => {
|
||||
const { result } = setUp();
|
||||
|
||||
it('should return undefined on initial render', () => {
|
||||
expect(hook.result.current).toBe(undefined);
|
||||
});
|
||||
|
||||
it('should return previous state after update', () => {
|
||||
hook.rerender(1);
|
||||
hook.rerender(2);
|
||||
expect(hook.result.current).toBe(1);
|
||||
});
|
||||
expect(result.current).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should always return previous state after each update', () => {
|
||||
const { result, rerender } = setUp();
|
||||
|
||||
rerender({ state: 2 });
|
||||
expect(result.current).toBe(0);
|
||||
|
||||
rerender({ state: 4 });
|
||||
expect(result.current).toBe(2);
|
||||
|
||||
rerender({ state: 6 });
|
||||
expect(result.current).toBe(4);
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user