Skip to content

Commit

Permalink
test: add test for useWatchEffect rejecting the lazy option
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed May 31, 2023
1 parent 2324272 commit 9881e52
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/__tests__/core.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import 'jest-performance-testing';
import { renderHook, act } from '@testing-library/react';

let consoleLog: jest.SpyInstance;
let consoleWarn: jest.SpyInstance;

beforeEach(() => {
consoleLog = jest.spyOn(console, 'log').mockImplementation(() => {});
consoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
});

afterEach(() => {
Expand Down Expand Up @@ -366,6 +368,38 @@ describe('useWatchEffect', () => {
// expect the hook to warn about using it inside a component that is not wrapped by makeReactive
expect(consoleLog).toHaveBeenCalled();
});
it('rejects lazy option', () => {
const counter = ref(1);
const effectFn = jest.fn();

const { unmount } = renderHook(() =>
useWatchEffect(
() => {
effectFn(counter.value);
},
// @ts-ignore
{ lazy: true }
)
);

expect(effectFn).toBeCalledTimes(1);

act(() => {
counter.value++;
});

expect(effectFn).toBeCalledTimes(2);

unmount();
act(() => {
counter.value++;
});

expect(effectFn).toBeCalledTimes(2);

// should warn about the lazy option
expect(consoleWarn).toBeCalled();
});
});

describe('watch', () => {
Expand Down

0 comments on commit 9881e52

Please sign in to comment.