@@ -35,33 +35,38 @@ describe("signal", () => {
35
35
expect ( s . peek ( ) ) . equal ( 1 ) ;
36
36
} ) ;
37
37
38
- it ( "should not trigger a read " , ( ) => {
38
+ it ( "should get the updated value after a value change " , ( ) => {
39
39
const s = signal ( 1 ) ;
40
+ s . value = 2 ;
41
+ expect ( s . peek ( ) ) . equal ( 2 ) ;
42
+ } ) ;
40
43
44
+ it ( "should not make surrounding effect depend on the signal" , ( ) => {
45
+ const s = signal ( 1 ) ;
41
46
const spy = sinon . spy ( ( ) => {
42
- // When we trigger a read this would cause an infinite loop
43
47
s . peek ( ) ;
44
48
} ) ;
45
49
46
50
effect ( spy ) ;
51
+ expect ( spy ) . to . be . calledOnce ;
47
52
48
53
s . value = 2 ;
49
-
50
54
expect ( spy ) . to . be . calledOnce ;
51
55
} ) ;
52
56
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 ( ) ;
59
61
} ) ;
62
+ const d = computed ( spy ) ;
60
63
61
- dispose ( ) ;
62
- a . value = 2 ;
64
+ d . value ;
65
+ expect ( spy ) . to . be . calledOnce ;
63
66
64
- expect ( b . peek ( ) ) . to . equal ( 2 ) ;
67
+ s . value = 2 ;
68
+ d . value ;
69
+ expect ( spy ) . to . be . calledOnce ;
65
70
} ) ;
66
71
} ) ;
67
72
@@ -999,6 +1004,86 @@ describe("computed()", () => {
999
1004
expect ( spy ) . to . be . calledOnce ;
1000
1005
} ) ;
1001
1006
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
+
1002
1087
describe ( "garbage collection" , function ( ) {
1003
1088
// Skip GC tests if window.gc/global.gc is not defined.
1004
1089
before ( function ( ) {
0 commit comments