Skip to content

Commit

Permalink
Merge pull request #138 from Ceylar37/test-useDidUpdate
Browse files Browse the repository at this point in the history
test: add tests for useDidUpdate
  • Loading branch information
debabin authored Jun 6, 2024
2 parents 5f18757 + 50b840b commit 5e6e7db
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/hooks/useDidUpdate/useDidUpdate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { renderHook } from '@testing-library/react';

import { useDidUpdate } from './useDidUpdate';

it('Should use did update', () => {
const effect = vi.fn();
renderHook(() => useDidUpdate(effect, []));

expect(effect).not.toHaveBeenCalled();
});

it('Should call effect on subsequent updates when dependencies change', () => {
const effect = vi.fn();
const { rerender } = renderHook(({ deps }) => useDidUpdate(effect, deps), {
initialProps: { deps: [1] }
});

expect(effect).not.toHaveBeenCalled();

rerender({ deps: [1] });
expect(effect).not.toHaveBeenCalled();

rerender({ deps: [2] });
expect(effect).toHaveBeenCalledTimes(1);

rerender({ deps: [3] });
expect(effect).toHaveBeenCalledTimes(2);
});

it('Should call effect on subsequent updates when dependencies change', () => {
const effect = vi.fn();
const { rerender } = renderHook(() => useDidUpdate(effect));

expect(effect).not.toHaveBeenCalled();

rerender();
expect(effect).toHaveBeenCalledTimes(1);

rerender();
expect(effect).toHaveBeenCalledTimes(2);
});

0 comments on commit 5e6e7db

Please sign in to comment.