Skip to content

Commit d930658

Browse files
authored
Merge pull request #210 from preactjs/more-tests
Add tests for Computed.peek()
2 parents 9ca60eb + 458a6e3 commit d930658

File tree

1 file changed

+97
-12
lines changed

1 file changed

+97
-12
lines changed

packages/core/test/signal.test.tsx

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,38 @@ describe("signal", () => {
3535
expect(s.peek()).equal(1);
3636
});
3737

38-
it("should not trigger a read", () => {
38+
it("should get the updated value after a value change", () => {
3939
const s = signal(1);
40+
s.value = 2;
41+
expect(s.peek()).equal(2);
42+
});
4043

44+
it("should not make surrounding effect depend on the signal", () => {
45+
const s = signal(1);
4146
const spy = sinon.spy(() => {
42-
// When we trigger a read this would cause an infinite loop
4347
s.peek();
4448
});
4549

4650
effect(spy);
51+
expect(spy).to.be.calledOnce;
4752

4853
s.value = 2;
49-
5054
expect(spy).to.be.calledOnce;
5155
});
5256

53-
it("should refresh value if stale", () => {
54-
const a = signal(1);
55-
const b = computed(() => a.value);
56-
57-
const dispose = effect(() => {
58-
b.value;
57+
it("should not make surrounding computed depend on the signal", () => {
58+
const s = signal(1);
59+
const spy = sinon.spy(() => {
60+
s.peek();
5961
});
62+
const d = computed(spy);
6063

61-
dispose();
62-
a.value = 2;
64+
d.value;
65+
expect(spy).to.be.calledOnce;
6366

64-
expect(b.peek()).to.equal(2);
67+
s.value = 2;
68+
d.value;
69+
expect(spy).to.be.calledOnce;
6570
});
6671
});
6772

@@ -999,6 +1004,86 @@ describe("computed()", () => {
9991004
expect(spy).to.be.calledOnce;
10001005
});
10011006

1007+
describe(".peek()", () => {
1008+
it("should get value", () => {
1009+
const s = signal(1);
1010+
const c = computed(() => s.value);
1011+
expect(c.peek()).equal(1);
1012+
});
1013+
1014+
it("should refresh value if stale", () => {
1015+
const a = signal(1);
1016+
const b = computed(() => a.value);
1017+
expect(b.peek()).to.equal(1);
1018+
1019+
a.value = 2;
1020+
expect(b.peek()).to.equal(2);
1021+
});
1022+
1023+
it("should not make surrounding effect depend on the computed", () => {
1024+
const s = signal(1);
1025+
const c = computed(() => s.value);
1026+
const spy = sinon.spy(() => {
1027+
c.peek();
1028+
});
1029+
1030+
effect(spy);
1031+
expect(spy).to.be.calledOnce;
1032+
1033+
s.value = 2;
1034+
expect(spy).to.be.calledOnce;
1035+
});
1036+
1037+
it("should not make surrounding computed depend on the computed", () => {
1038+
const s = signal(1);
1039+
const c = computed(() => s.value);
1040+
1041+
const spy = sinon.spy(() => {
1042+
c.peek();
1043+
});
1044+
1045+
const d = computed(spy);
1046+
d.value;
1047+
expect(spy).to.be.calledOnce;
1048+
1049+
s.value = 2;
1050+
d.value;
1051+
expect(spy).to.be.calledOnce;
1052+
});
1053+
1054+
it("should not make surrounding effect depend on the peeked computed's dependencies", () => {
1055+
const a = signal(1);
1056+
const b = computed(() => a.value);
1057+
const spy = sinon.spy();
1058+
effect(() => {
1059+
spy();
1060+
b.peek();
1061+
});
1062+
expect(spy).to.be.calledOnce;
1063+
spy.resetHistory();
1064+
1065+
a.value = 1;
1066+
expect(spy).not.to.be.called;
1067+
});
1068+
1069+
it("should not subscribe a surrounding computed depend on peeked computed's dependencies", () => {
1070+
const a = signal(1);
1071+
const b = computed(() => a.value);
1072+
const spy = sinon.spy();
1073+
const d = computed(() => {
1074+
spy();
1075+
b.peek();
1076+
});
1077+
d.value;
1078+
expect(spy).to.be.calledOnce;
1079+
spy.resetHistory();
1080+
1081+
a.value = 1;
1082+
d.value;
1083+
expect(spy).not.to.be.called;
1084+
});
1085+
});
1086+
10021087
describe("garbage collection", function () {
10031088
// Skip GC tests if window.gc/global.gc is not defined.
10041089
before(function () {

0 commit comments

Comments
 (0)