Skip to content

Commit

Permalink
test: test for invalid sources in watch
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed May 31, 2023
1 parent 4f14ef7 commit 4eb97a2
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/__tests__/core.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,60 @@ describe('watch', () => {

expect(effectFn).toBeCalledTimes(1);
});
it('warns about invalid source', () => {
const obj = { nested: { a: 1 } };
const effectFn = jest.fn();

const runner = watch(obj, (...args) => {
effectFn(...args);
});

expect(effectFn).toBeCalledTimes(0);

obj.nested.a++;

expect(effectFn).toBeCalledTimes(0);

runner();
obj.nested.a++;

expect(effectFn).toBeCalledTimes(0);

expect(consoleWarn).toBeCalled();
});
it('warns about invalid source in array', () => {
const counter = ref(1);
const obj = { a: 1, nested: { b: 2 } };
const obj2 = reactive({ a: 1, nested: { b: 2 } });
const effectFn = jest.fn();

const runner = watch([counter, obj, () => obj2.a], (...args) => {
effectFn(...args);
});

expect(effectFn).toBeCalledTimes(0);

counter.value++;

expect(effectFn).toBeCalledTimes(1);
expect(effectFn).toBeCalledWith([2, obj, 1], [1, obj, 1]);

obj.nested.b++;

expect(effectFn).toBeCalledTimes(1);

obj2.a++;

expect(effectFn).toBeCalledTimes(2);
expect(effectFn).toBeCalledWith([2, obj, 2], [2, obj, 1]);

runner();
obj.nested.b++;

expect(effectFn).toBeCalledTimes(2);

expect(consoleWarn).toBeCalled();
});
});

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

0 comments on commit 4eb97a2

Please sign in to comment.