1
1
import type { Draft } from 'immer' ;
2
2
import * as sinon from 'sinon' ;
3
3
4
- import { BaseController } from './BaseControllerV2' ;
4
+ import { BaseController , getAnonymizedState , getPersistentState } from './BaseControllerV2' ;
5
5
6
6
interface MockControllerState {
7
7
count : number ;
8
8
}
9
9
10
+ const mockControllerSchema = {
11
+ count : {
12
+ persist : true ,
13
+ anonymous : true ,
14
+ } ,
15
+ } ;
16
+
10
17
class MockController extends BaseController < MockControllerState > {
11
18
update ( callback : ( state : Draft < MockControllerState > ) => void | MockControllerState ) {
12
19
super . update ( callback ) ;
@@ -19,21 +26,27 @@ class MockController extends BaseController<MockControllerState> {
19
26
20
27
describe ( 'BaseController' , ( ) => {
21
28
it ( 'should set initial state' , ( ) => {
22
- const controller = new MockController ( { count : 0 } ) ;
29
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
23
30
24
31
expect ( controller . state ) . toEqual ( { count : 0 } ) ;
25
32
} ) ;
26
33
34
+ it ( 'should set initial schema' , ( ) => {
35
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
36
+
37
+ expect ( controller . schema ) . toEqual ( mockControllerSchema ) ;
38
+ } ) ;
39
+
27
40
it ( 'should not allow mutating state directly' , ( ) => {
28
- const controller = new MockController ( { count : 0 } ) ;
41
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
29
42
30
43
expect ( ( ) => {
31
44
controller . state = { count : 1 } ;
32
45
} ) . toThrow ( ) ;
33
46
} ) ;
34
47
35
48
it ( 'should allow updating state by modifying draft' , ( ) => {
36
- const controller = new MockController ( { count : 0 } ) ;
49
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
37
50
38
51
controller . update ( ( draft ) => {
39
52
draft . count += 1 ;
@@ -43,7 +56,7 @@ describe('BaseController', () => {
43
56
} ) ;
44
57
45
58
it ( 'should allow updating state by return a value' , ( ) => {
46
- const controller = new MockController ( { count : 0 } ) ;
59
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
47
60
48
61
controller . update ( ( ) => {
49
62
return { count : 1 } ;
@@ -53,7 +66,7 @@ describe('BaseController', () => {
53
66
} ) ;
54
67
55
68
it ( 'should throw an error if update callback modifies draft and returns value' , ( ) => {
56
- const controller = new MockController ( { count : 0 } ) ;
69
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
57
70
58
71
expect ( ( ) => {
59
72
controller . update ( ( draft ) => {
@@ -64,7 +77,7 @@ describe('BaseController', () => {
64
77
} ) ;
65
78
66
79
it ( 'should inform subscribers of state changes' , ( ) => {
67
- const controller = new MockController ( { count : 0 } ) ;
80
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
68
81
const listener1 = sinon . stub ( ) ;
69
82
const listener2 = sinon . stub ( ) ;
70
83
@@ -81,7 +94,7 @@ describe('BaseController', () => {
81
94
} ) ;
82
95
83
96
it ( 'should inform a subscriber of each state change once even after multiple subscriptions' , ( ) => {
84
- const controller = new MockController ( { count : 0 } ) ;
97
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
85
98
const listener1 = sinon . stub ( ) ;
86
99
87
100
controller . subscribe ( listener1 ) ;
@@ -95,7 +108,7 @@ describe('BaseController', () => {
95
108
} ) ;
96
109
97
110
it ( 'should no longer inform a subscriber about state changes after unsubscribing' , ( ) => {
98
- const controller = new MockController ( { count : 0 } ) ;
111
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
99
112
const listener1 = sinon . stub ( ) ;
100
113
101
114
controller . subscribe ( listener1 ) ;
@@ -108,7 +121,7 @@ describe('BaseController', () => {
108
121
} ) ;
109
122
110
123
it ( 'should no longer inform a subscriber about state changes after unsubscribing once, even if they subscribed many times' , ( ) => {
111
- const controller = new MockController ( { count : 0 } ) ;
124
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
112
125
const listener1 = sinon . stub ( ) ;
113
126
114
127
controller . subscribe ( listener1 ) ;
@@ -122,7 +135,7 @@ describe('BaseController', () => {
122
135
} ) ;
123
136
124
137
it ( 'should allow unsubscribing listeners who were never subscribed' , ( ) => {
125
- const controller = new MockController ( { count : 0 } ) ;
138
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
126
139
const listener1 = sinon . stub ( ) ;
127
140
128
141
expect ( ( ) => {
@@ -131,7 +144,7 @@ describe('BaseController', () => {
131
144
} ) ;
132
145
133
146
it ( 'should no longer update subscribers after being destroyed' , ( ) => {
134
- const controller = new MockController ( { count : 0 } ) ;
147
+ const controller = new MockController ( { count : 0 } , mockControllerSchema ) ;
135
148
const listener1 = sinon . stub ( ) ;
136
149
const listener2 = sinon . stub ( ) ;
137
150
@@ -146,3 +159,105 @@ describe('BaseController', () => {
146
159
expect ( listener2 . callCount ) . toEqual ( 0 ) ;
147
160
} ) ;
148
161
} ) ;
162
+
163
+ describe ( 'getAnonymizedState' , ( ) => {
164
+ it ( 'should return empty state' , ( ) => {
165
+ expect ( getAnonymizedState ( { } , { } ) ) . toEqual ( { } ) ;
166
+ } ) ;
167
+
168
+ it ( 'should return empty state when no properties are anonymized' , ( ) => {
169
+ const anonymizedState = getAnonymizedState ( { count : 1 } , { count : { anonymous : false , persist : false } } ) ;
170
+ expect ( anonymizedState ) . toEqual ( { } ) ;
171
+ } ) ;
172
+
173
+ it ( 'should return state that is already anonymized' , ( ) => {
174
+ const anonymizedState = getAnonymizedState (
175
+ {
176
+ password : 'secret password' ,
177
+ privateKey : '123' ,
178
+ network : 'mainnet' ,
179
+ tokens : [ 'DAI' , 'USDC' ] ,
180
+ } ,
181
+ {
182
+ password : {
183
+ anonymous : false ,
184
+ persist : false ,
185
+ } ,
186
+ privateKey : {
187
+ anonymous : false ,
188
+ persist : false ,
189
+ } ,
190
+ network : {
191
+ anonymous : true ,
192
+ persist : false ,
193
+ } ,
194
+ tokens : {
195
+ anonymous : true ,
196
+ persist : false ,
197
+ } ,
198
+ } ,
199
+ ) ;
200
+ expect ( anonymizedState ) . toEqual ( { network : 'mainnet' , tokens : [ 'DAI' , 'USDC' ] } ) ;
201
+ } ) ;
202
+
203
+ it ( 'should use anonymizing function to anonymize state' , ( ) => {
204
+ const anonymizeTransactionHash = ( hash : string ) => {
205
+ return hash . split ( '' ) . reverse ( ) . join ( '' ) ;
206
+ } ;
207
+
208
+ const anonymizedState = getAnonymizedState (
209
+ {
210
+ transactionHash : '0x1234' ,
211
+ } ,
212
+ {
213
+ transactionHash : {
214
+ anonymous : anonymizeTransactionHash ,
215
+ persist : false ,
216
+ } ,
217
+ } ,
218
+ ) ;
219
+
220
+ expect ( anonymizedState ) . toEqual ( { transactionHash : '4321x0' } ) ;
221
+ } ) ;
222
+ } ) ;
223
+
224
+ describe ( 'getPersistentState' , ( ) => {
225
+ it ( 'should return empty state' , ( ) => {
226
+ expect ( getPersistentState ( { } , { } ) ) . toEqual ( { } ) ;
227
+ } ) ;
228
+
229
+ it ( 'should return empty state when no properties are persistent' , ( ) => {
230
+ const persistentState = getPersistentState ( { count : 1 } , { count : { anonymous : false , persist : false } } ) ;
231
+ expect ( persistentState ) . toEqual ( { } ) ;
232
+ } ) ;
233
+
234
+ it ( 'should return persistent state' , ( ) => {
235
+ const persistentState = getPersistentState (
236
+ {
237
+ password : 'secret password' ,
238
+ privateKey : '123' ,
239
+ network : 'mainnet' ,
240
+ tokens : [ 'DAI' , 'USDC' ] ,
241
+ } ,
242
+ {
243
+ password : {
244
+ anonymous : false ,
245
+ persist : true ,
246
+ } ,
247
+ privateKey : {
248
+ anonymous : false ,
249
+ persist : true ,
250
+ } ,
251
+ network : {
252
+ anonymous : false ,
253
+ persist : false ,
254
+ } ,
255
+ tokens : {
256
+ anonymous : false ,
257
+ persist : false ,
258
+ } ,
259
+ } ,
260
+ ) ;
261
+ expect ( persistentState ) . toEqual ( { password : 'secret password' , privateKey : '123' } ) ;
262
+ } ) ;
263
+ } ) ;
0 commit comments