1
- import { RefStateEditor } from '../../src/core/component/state/editor'
1
+ import { RefStateEditor , StateEditor } from '../../src/core/component/state/editor'
2
2
3
3
describe ( 'editor: RefStateEditor' , ( ) => {
4
4
const editor = new RefStateEditor ( )
5
5
6
+ // Plain object
6
7
// eslint-disable-next-line test/consistent-test-it
7
8
test . each ( [
9
+ // Plain object.
8
10
// Add new key.
9
11
{ refValue : { foo : 'bar' } , newValue : { foo : 'bar' , bar : 'baz' } } ,
10
12
// Add new key and modify origin value.
@@ -21,4 +23,158 @@ describe('editor: RefStateEditor', () => {
21
23
editor . set ( refValue as any , newValue )
22
24
expect ( refValue ) . toEqual ( newValue )
23
25
} )
26
+
27
+ // Native set
28
+ it ( 'refStateEditor.set on native set' , ( ) => {
29
+ const refValue = new Set ( [ 'foo' , 'bar' ] )
30
+ const newValue = [ 'baz' , 'foo' , 'bar' ]
31
+ const targetValue = new Set ( [ 'baz' , 'foo' , 'bar' ] )
32
+ editor . set ( refValue as any , newValue )
33
+ expect ( refValue ) . toEqual ( targetValue )
34
+ } )
35
+
36
+ // Native map
37
+ // eslint-disable-next-line test/consistent-test-it
38
+ test . each ( [
39
+ // Add new key.
40
+ { refValue : new Map ( [ [ 'foo' , 'bar' ] ] ) , newValue : { foo : 'bar' , bar : 'baz' } , targetValue : new Map ( [ [ 'foo' , 'bar' ] , [ 'bar' , 'baz' ] ] ) } ,
41
+ // Add new key and modify origin value.
42
+ { refValue : new Map ( [ [ 'foo' , 'bar' ] ] ) , newValue : { foo : 'barr' , bar : 'baz' } , targetValue : new Map ( [ [ 'foo' , 'barr' ] , [ 'bar' , 'baz' ] ] ) } ,
43
+ // Add new key and modify origin value.
44
+ { refValue : new Map ( [ [ 'foo' , 'bar' ] ] ) , newValue : { foo : 'barr' , bar : 'baz' } , targetValue : new Map ( [ [ 'foo' , 'barr' ] , [ 'bar' , 'baz' ] ] ) } ,
45
+ // Modify origin value.
46
+ { refValue : new Map ( [ [ 'foo' , 'bar' ] ] ) , newValue : { foo : 'barr' } , targetValue : new Map ( [ [ 'foo' , 'barr' ] ] ) } ,
47
+ // Remove key.
48
+ { refValue : new Map ( [ [ 'foo' , 'bar' ] ] ) , newValue : { } , targetValue : new Map ( ) } ,
49
+ // Remove key and modify origin value.
50
+ { refValue : new Map ( [ [ 'foo' , 'bar' ] ] ) , newValue : { foo : 'barr' } , targetValue : new Map ( [ [ 'foo' , 'barr' ] ] ) } ,
51
+ // Remove key and add new key.
52
+ { refValue : new Map ( [ [ 'foo' , 'bar' ] ] ) , newValue : { bar : 'baz' } , targetValue : new Map ( [ [ 'bar' , 'baz' ] ] ) } ,
53
+ ] ) ( '%o can be modified to $newValue by RefStateEditor.set' , ( { refValue, newValue, targetValue } ) => {
54
+ editor . set ( refValue as any , newValue )
55
+ expect ( refValue ) . toEqual ( targetValue || newValue )
56
+ } )
57
+ } )
58
+
59
+ describe ( 'editor: StateEditor.set' , ( ) => {
60
+ const stateEditor = new StateEditor ( )
61
+
62
+ describe ( 'editComponentState: plain object' , ( ) => {
63
+ it ( 'modify value' , ( ) => {
64
+ const target = { foo : 'bar' }
65
+ const newValue = 'baz'
66
+ const state = { newKey : '' , type : '' , value : 'baz' }
67
+ const path = 'foo'
68
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
69
+ stateEditor . set ( target , path , newValue , defaultCallback )
70
+ expect ( target ) . toEqual ( { foo : 'baz' } )
71
+ } )
72
+
73
+ it ( 'add new value' , ( ) => {
74
+ const target = { foo : 'bar' }
75
+ const newValue = 'baz'
76
+ const state = { newKey : 'bar' , type : '' , value : 'baz' }
77
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
78
+ stateEditor . set ( target , '' , newValue , defaultCallback )
79
+ expect ( target ) . toEqual ( { foo : 'bar' , bar : 'baz' } )
80
+ } )
81
+
82
+ it ( 'remove value' , ( ) => {
83
+ const target = { foo : 'bar' , bar : 'baz' }
84
+ const state = { newKey : '' , type : '' , value : '' , remove : true }
85
+ const path = 'foo'
86
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
87
+ stateEditor . set ( target , path , '' , defaultCallback )
88
+ expect ( target ) . toEqual ( { bar : 'baz' } )
89
+ } )
90
+ } )
91
+
92
+ describe ( 'editComponentState: array' , ( ) => {
93
+ it ( 'modify value' , ( ) => {
94
+ const target = [ 'foo' , 'bar' ]
95
+ const state = { newKey : '' , type : '' , value : 'baz' }
96
+ const newValue = 'baz'
97
+ const path = '0'
98
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
99
+ stateEditor . set ( target , path , newValue , defaultCallback )
100
+ expect ( target ) . toEqual ( [ 'baz' , 'bar' ] )
101
+ } )
102
+
103
+ it ( 'add new value' , ( ) => {
104
+ const target = [ 'foo' , 'bar' ]
105
+ const newValue = 'baz'
106
+ const state = { newKey : '2' , type : '' , value : newValue }
107
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
108
+ stateEditor . set ( target , '2' , newValue , defaultCallback )
109
+ expect ( target ) . toEqual ( [ 'foo' , 'bar' , 'baz' ] )
110
+ } )
111
+
112
+ it ( 'remove value' , ( ) => {
113
+ const target = [ 'foo' , 'bar' , 'baz' ]
114
+ const state = { newKey : '' , type : '' , value : '' , remove : true }
115
+ const path = '0'
116
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
117
+ stateEditor . set ( target , path , '' , defaultCallback )
118
+ expect ( target ) . toEqual ( [ 'bar' , 'baz' ] )
119
+ } )
120
+ } )
121
+
122
+ describe ( 'editComponentState: set' , ( ) => {
123
+ it ( 'add new value' , ( ) => {
124
+ const target = new Set ( [ 'foo' , 'bar' ] )
125
+ const newValue = 'baz'
126
+ const state = { newKey : '2' , type : '' , value : newValue }
127
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
128
+ stateEditor . set ( target , '2' , newValue , defaultCallback )
129
+ expect ( target ) . toEqual ( new Set ( [ 'foo' , 'bar' , 'baz' ] ) )
130
+ } )
131
+
132
+ it ( 'remove value' , ( ) => {
133
+ const target = new Set ( [ 'foo' , 'bar' , 'baz' ] )
134
+ const state = { newKey : '' , type : '' , value : 'foo' , remove : true }
135
+ const path = '0'
136
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
137
+ stateEditor . set ( target , path , '' , defaultCallback )
138
+ expect ( target ) . toEqual ( new Set ( [ 'bar' , 'baz' ] ) )
139
+ } )
140
+
141
+ it ( 'remove object type member' , ( ) => {
142
+ const target = new Set ( [ 'foo' , { bar : 'baz' } ] )
143
+ const state = { newKey : '' , type : '' , value : { bar : 'baz' } , remove : true }
144
+ const path = '1'
145
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
146
+ stateEditor . set ( target , path , '' , defaultCallback )
147
+ expect ( target ) . toEqual ( new Set ( [ 'foo' ] ) )
148
+ } )
149
+ } )
150
+
151
+ describe ( 'editComponentState: map' , ( ) => {
152
+ it ( 'modify value' , ( ) => {
153
+ const target = new Map ( [ [ 'foo' , 'bar' ] ] )
154
+ const state = { newKey : '' , type : '' , value : 'baz' }
155
+ const newValue = 'baz'
156
+ const path = 'foo'
157
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
158
+ stateEditor . set ( target , path , newValue , defaultCallback )
159
+ expect ( target ) . toEqual ( new Map ( [ [ 'foo' , 'baz' ] ] ) )
160
+ } )
161
+
162
+ it ( 'add new value' , ( ) => {
163
+ const target = new Map ( [ [ 'foo' , 'bar' ] ] )
164
+ const newValue = 'baz'
165
+ const state = { newKey : 'bar' , type : '' , value : newValue }
166
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
167
+ stateEditor . set ( target , 'bar' , newValue , defaultCallback )
168
+ expect ( target ) . toEqual ( new Map ( [ [ 'foo' , 'bar' ] , [ 'bar' , 'baz' ] ] ) )
169
+ } )
170
+
171
+ it ( 'remove value' , ( ) => {
172
+ const target = new Map ( [ [ 'foo' , 'bar' ] , [ 'bar' , 'baz' ] ] )
173
+ const state = { newKey : '' , type : '' , value : '' , remove : true }
174
+ const path = 'foo'
175
+ const defaultCallback = stateEditor . createDefaultSetCallback ( state )
176
+ stateEditor . set ( target , path , '' , defaultCallback )
177
+ expect ( target ) . toEqual ( new Map ( [ [ 'bar' , 'baz' ] ] ) )
178
+ } )
179
+ } )
24
180
} )
0 commit comments