@@ -1120,3 +1120,86 @@ test("803 - action.bound and action preserve type info", t => {
1120
1120
const bound2 = action . bound ( function ( ) { } ) as ( ( ) => void )
1121
1121
t . end ( )
1122
1122
} )
1123
+
1124
+ test ( "@computed.equals (TS)" , t => {
1125
+ const sameTime = ( a : Time , b : Time ) => {
1126
+ return ( a != null && b != null )
1127
+ ? a . hour === b . hour && a . minute === b . minute
1128
+ : a === b ;
1129
+ }
1130
+ class Time {
1131
+ constructor ( hour : number , minute : number ) {
1132
+ this . hour = hour ;
1133
+ this . minute = minute ;
1134
+ }
1135
+
1136
+ @observable public hour : number ;
1137
+ @observable public minute : number ;
1138
+
1139
+ @computed . equals ( sameTime ) public get time ( ) {
1140
+ return { hour : this . hour , minute : this . minute } ;
1141
+ }
1142
+ }
1143
+ const time = new Time ( 9 , 0 ) ;
1144
+
1145
+ const changes : Array < { hour : number , minute : number } > = [ ] ;
1146
+ const disposeAutorun = autorun ( ( ) => changes . push ( time . time ) ) ;
1147
+
1148
+ t . deepEqual ( changes , [ { hour : 9 , minute : 0 } ] ) ;
1149
+ time . hour = 9 ;
1150
+ t . deepEqual ( changes , [ { hour : 9 , minute : 0 } ] ) ;
1151
+ time . minute = 0 ;
1152
+ t . deepEqual ( changes , [ { hour : 9 , minute : 0 } ] ) ;
1153
+ time . hour = 10 ;
1154
+ t . deepEqual ( changes , [ { hour : 9 , minute : 0 } , { hour : 10 , minute : 0 } ] ) ;
1155
+ time . minute = 30 ;
1156
+ t . deepEqual ( changes , [ { hour : 9 , minute : 0 } , { hour : 10 , minute : 0 } , { hour : 10 , minute : 30 } ] ) ;
1157
+
1158
+ disposeAutorun ( ) ;
1159
+
1160
+ t . end ( ) ;
1161
+ } ) ;
1162
+
1163
+
1164
+ test ( "computed equals comparer" , t => {
1165
+ const comparisons : Array < { from : string , to : string } > = [ ] ;
1166
+ const comparer = ( from : string , to : string ) => {
1167
+ comparisons . push ( { from, to } ) ;
1168
+ return from === to ;
1169
+ } ;
1170
+
1171
+ const left = observable ( "A" ) ;
1172
+ const right = observable ( "B" ) ;
1173
+ const combinedToLowerCase = computed (
1174
+ ( ) => left . get ( ) . toLowerCase ( ) + right . get ( ) . toLowerCase ( ) ,
1175
+ { equals : comparer }
1176
+ ) ;
1177
+
1178
+ const values : Array < string > = [ ] ;
1179
+ const disposeAutorun = autorun ( ( ) => values . push ( combinedToLowerCase . get ( ) ) ) ;
1180
+
1181
+ // No comparison should be made on the first value
1182
+ t . deepEqual ( comparisons , [ ] ) ;
1183
+
1184
+ // First change will cause a comparison
1185
+ left . set ( "C" ) ;
1186
+ t . deepEqual ( comparisons , [ { from : "ab" , to : "cb" } ] ) ;
1187
+
1188
+ // Transition *to* CaughtException in the computed won't cause a comparison
1189
+ left . set ( null ) ;
1190
+ t . deepEqual ( comparisons , [ { from : "ab" , to : "cb" } ] ) ;
1191
+
1192
+ // Transition *from* CaughtException in the computed won't cause a comparison
1193
+ left . set ( "D" ) ;
1194
+ t . deepEqual ( comparisons , [ { from : "ab" , to : "cb" } ] ) ;
1195
+
1196
+ // Another value change will cause a comparison
1197
+ right . set ( "E" ) ;
1198
+ t . deepEqual ( comparisons , [ { from : "ab" , to : "cb" } , { from : "db" , to : "de" } ] ) ;
1199
+
1200
+ t . deepEqual ( values , [ "ab" , "cb" , "db" , "de" ] ) ;
1201
+
1202
+ disposeAutorun ( ) ;
1203
+
1204
+ t . end ( ) ;
1205
+ } ) ;
0 commit comments