Skip to content

Commit

Permalink
Merge pull request #11 from proposal-signals/reorganize-tests
Browse files Browse the repository at this point in the history
Reorganize and split tests
  • Loading branch information
littledan authored May 21, 2024
2 parents c01bdd3 + 64fe8d8 commit 30211ee
Show file tree
Hide file tree
Showing 17 changed files with 1,062 additions and 1,053 deletions.
1,052 changes: 0 additions & 1,052 deletions src/wrapper.spec.ts

This file was deleted.

74 changes: 74 additions & 0 deletions tests/Signal/computed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, expect, it } from "vitest";
import { Signal } from "../../src/wrapper.js";

describe("Computed", () => {
it("should work", () => {
const stateSignal = new Signal.State(1);

const computedSignal = new Signal.Computed(() => {
const f = stateSignal.get() * 2;
return f;
});

expect(computedSignal.get()).toEqual(2);

stateSignal.set(5);

expect(stateSignal.get()).toEqual(5);
expect(computedSignal.get()).toEqual(10);
});

describe("Comparison semantics", () => {
it("should track Computed by Object.is", () => {
const state = new Signal.State(1);
let value = 5;
let calls = 0;
const computed = new Signal.Computed(() => (state.get(), value));
const c2 = new Signal.Computed(() => (calls++, computed.get()));

expect(calls).toBe(0);
expect(c2.get()).toBe(5);
expect(calls).toBe(1);
state.set(2);
expect(c2.get()).toBe(5);
expect(calls).toBe(1);
value = NaN;
expect(c2.get()).toBe(5);
expect(calls).toBe(1);
state.set(3);
expect(c2.get()).toBe(NaN);
expect(calls).toBe(2);
state.set(4);
expect(c2.get()).toBe(NaN);
expect(calls).toBe(2);
});

it("applies custom equality in Computed", () => {
const s = new Signal.State(5);
let ecalls = 0;
const c1 = new Signal.Computed(() => (s.get(), 1), {
equals() {
ecalls++;
return false;
},
});
let calls = 0;
const c2 = new Signal.Computed(() => {
calls++;
return c1.get();
});

expect(calls).toBe(0);
expect(ecalls).toBe(0);

expect(c2.get()).toBe(1);
expect(ecalls).toBe(0);
expect(calls).toBe(1);

s.set(10);
expect(c2.get()).toBe(1);
expect(ecalls).toBe(1);
expect(calls).toBe(2);
});
});
});
57 changes: 57 additions & 0 deletions tests/Signal/state.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it } from "vitest";
import { Signal } from "../../src/wrapper.js";

describe("Signal.State", () => {
it("should work", () => {
const stateSignal = new Signal.State(0);
expect(stateSignal.get()).toEqual(0);

stateSignal.set(10);

expect(stateSignal.get()).toEqual(10);
});

describe("Comparison semantics", () => {
it("should cache State by Object.is", () => {
const state = new Signal.State(NaN);
let calls = 0;
const computed = new Signal.Computed(() => {
calls++;
return state.get();
});
expect(calls).toBe(0);
expect(computed.get()).toBe(NaN);
expect(calls).toBe(1);
state.set(NaN);
expect(computed.get()).toBe(NaN);
expect(calls).toBe(1);
});

it("applies custom equality in State", () => {
let ecalls = 0;
const state = new Signal.State(1, {
equals() {
ecalls++;
return false;
},
});
let calls = 0;
const computed = new Signal.Computed(() => {
calls++;
return state.get();
});

expect(calls).toBe(0);
expect(ecalls).toBe(0);

expect(computed.get()).toBe(1);
expect(ecalls).toBe(0);
expect(calls).toBe(1);

state.set(1);
expect(computed.get()).toBe(1);
expect(ecalls).toBe(1);
expect(calls).toBe(2);
});
});
});
14 changes: 14 additions & 0 deletions tests/Signal/subtle/currentComputed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { Signal } from "../../../src/wrapper.js";

describe("currentComputed", () => {
it("works", () => {
expect(Signal.subtle.currentComputed()).toBe(undefined);
let context;
let c = new Signal.Computed(
() => (context = Signal.subtle.currentComputed()),
);
c.get();
expect(c).toBe(context);
});
});
22 changes: 22 additions & 0 deletions tests/Signal/subtle/untrack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";
import { Signal } from "../../../src/wrapper.js";

describe("Untrack", () => {
it("works", () => {
const state = new Signal.State(1);
const computed = new Signal.Computed(() =>
Signal.subtle.untrack(() => state.get()),
);
expect(computed.get()).toBe(1);
state.set(2);
expect(computed.get()).toBe(1);
});

it("works differently without untrack", () => {
const state = new Signal.State(1);
const computed = new Signal.Computed(() => state.get());
expect(computed.get()).toBe(1);
state.set(2);
expect(computed.get()).toBe(2);
});
});
163 changes: 163 additions & 0 deletions tests/Signal/subtle/watch-unwatch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { describe, expect, it } from "vitest";
import { Signal } from "../../../src/wrapper.js";

describe("watch and unwatch", () => {
it("handles multiple watchers well", () => {
const s = new Signal.State(1);
const s2 = new Signal.State(2);
let n = 0;
const w = new Signal.subtle.Watcher(() => n++);
w.watch(s, s2);

s.set(4);
expect(n).toBe(1);
expect(w.getPending()).toStrictEqual([]);

w.watch();
s2.set(8);
expect(n).toBe(2);

w.unwatch(s);
s.set(3);
expect(n).toBe(2);

w.watch();
s2.set(3);
expect(n).toBe(3);

w.watch();
s.set(2);
expect(n).toBe(3);
});
it("understands dynamic dependency sets", () => {
let w1 = 0,
u1 = 0,
w2 = 0,
u2 = 0,
n = 0,
d = 0;
let s1 = new Signal.State(1, {
[Signal.subtle.watched]() {
w1++;
},
[Signal.subtle.unwatched]() {
u1++;
},
});
let s2 = new Signal.State(2, {
[Signal.subtle.watched]() {
w2++;
},
[Signal.subtle.unwatched]() {
u2++;
},
});
let which: { get(): number } = s1;
let c = new Signal.Computed(() => (d++, which.get()));
let w = new Signal.subtle.Watcher(() => n++);

w.watch(c);
expect(w1 + w2 + u1 + u2 + n + d).toBe(0);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([c]);

expect(c.get()).toBe(1);
expect(w1).toBe(1);
expect(u1).toBe(0);
expect(w2).toBe(0);
expect(u2).toBe(0);
expect(n).toBe(0);
expect(Signal.subtle.hasSinks(s1)).toBe(true);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(1);

s1.set(3);
expect(w1).toBe(1);
expect(u1).toBe(0);
expect(w2).toBe(0);
expect(u2).toBe(0);
expect(n).toBe(1);
expect(Signal.subtle.hasSinks(s1)).toBe(true);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([c]);
expect(d).toBe(1);

expect(c.get()).toBe(3);
expect(w1).toBe(1);
expect(u1).toBe(0);
expect(w2).toBe(0);
expect(u2).toBe(0);
expect(n).toBe(1);
expect(Signal.subtle.hasSinks(s1)).toBe(true);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(2);

which = s2;
w.watch();
s1.set(4);
expect(w1).toBe(1);
expect(u1).toBe(0);
expect(w2).toBe(0);
expect(u2).toBe(0);
expect(n).toBe(2);
expect(Signal.subtle.hasSinks(s1)).toBe(true);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([c]);
expect(d).toBe(2);

expect(c.get()).toBe(2);
expect(w1).toBe(1);
expect(u1).toBe(1);
expect(w2).toBe(1);
expect(u2).toBe(0);
expect(n).toBe(2);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(true);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(3);

w.watch();
which = {
get() {
return 10;
},
};
s1.set(5);
expect(c.get()).toBe(2);
expect(w1).toBe(1);
expect(u1).toBe(1);
expect(w2).toBe(1);
expect(u2).toBe(0);
expect(n).toBe(2);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(true);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(3);

w.watch();
s2.set(0);
expect(w1).toBe(1);
expect(u1).toBe(1);
expect(w2).toBe(1);
expect(u2).toBe(0);
expect(n).toBe(3);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(true);
expect(w.getPending()).toStrictEqual([c]);
expect(d).toBe(3);

expect(c.get()).toBe(10);
expect(w1).toBe(1);
expect(u1).toBe(1);
expect(w2).toBe(1);
expect(u2).toBe(1);
expect(n).toBe(3);
expect(Signal.subtle.hasSinks(s1)).toBe(false);
expect(Signal.subtle.hasSinks(s2)).toBe(false);
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(4);
});
});
Loading

0 comments on commit 30211ee

Please sign in to comment.