Skip to content

Commit 0066523

Browse files
committed
up
1 parent f0d9a98 commit 0066523

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

tests/derived.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,22 @@ describe("derived", () => {
2727

2828
test("derived updates when dependencies change", async () => {
2929
const state = reactive({ a: 1, b: 2 });
30-
const d = derived(() => state.a * state.b);
31-
const spy = jest.fn();
32-
effect(() => spy(d()));
33-
expectSpy(spy, 1, [2]);
30+
31+
const spyDerived = jest.fn(() => state.a * state.b);
32+
const d = derived(spyDerived);
33+
const spyEffect = jest.fn(() => d());
34+
effect(spyEffect);
35+
36+
expectSpy(spyEffect, 1, []);
37+
expectSpy(spyDerived, 1, [], 2);
3438
state.a = 3;
3539
await waitScheduler();
36-
expectSpy(spy, 2, [6]);
40+
expectSpy(spyEffect, 2, []);
41+
expectSpy(spyDerived, 2, [], 6);
3742
state.b = 4;
3843
await waitScheduler();
39-
expectSpy(spy, 3, [12]);
44+
expectSpy(spyEffect, 3, []);
45+
expectSpy(spyDerived, 3, [], 12);
4046
});
4147

4248
test("derived does not update when unrelated property changes", async () => {

tests/helpers.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,16 @@ export async function editInput(input: HTMLInputElement | HTMLTextAreaElement, v
219219
return nextTick();
220220
}
221221

222-
export function expectSpy(spy: jest.Mock, callTime: number, args: any[]): void {
222+
const noReturnValue = Symbol();
223+
export function expectSpy(
224+
spy: jest.Mock,
225+
callTime: number,
226+
args: any[],
227+
returnValue: any = noReturnValue
228+
): void {
223229
expect(spy).toHaveBeenCalledTimes(callTime);
224230
expect(spy).lastCalledWith(...args);
231+
!noReturnValue && expect(spy).toHaveReturnedWith(returnValue);
225232
}
226233

227234
afterEach(() => {

0 commit comments

Comments
 (0)