@webqit/observer 2.0.7 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +3 -3
- package/LICENSE +20 -20
- package/README.md +416 -202
- package/dist/main.js +1 -1
- package/dist/main.js.map +3 -3
- package/package.json +68 -68
- package/src/actors.js +180 -176
- package/src/core/Descriptor.js +22 -22
- package/src/core/ListenerRegistration.js +61 -57
- package/src/core/ListenerRegistry.js +73 -70
- package/src/core/Registration.js +34 -34
- package/src/core/Registry.js +92 -92
- package/src/core/TrapsRegistration.js +34 -34
- package/src/core/TrapsRegistry.js +50 -50
- package/src/index.js +9 -9
- package/src/main.js +585 -561
- package/src/targets.browser.js +8 -8
- package/src/util.js +9 -7
- package/test/reactions.test.js +351 -352
- package/webpack.config.cjs +5 -5
package/test/reactions.test.js
CHANGED
|
@@ -1,353 +1,352 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @imports
|
|
4
|
-
*/
|
|
5
|
-
import { expect } from 'chai';
|
|
6
|
-
import Observer from '../src/index.js';
|
|
7
|
-
import TrapsRegistry from '../src/core/TrapsRegistry.js';
|
|
8
|
-
import ListenerRegistry from '../src/core/ListenerRegistry.js';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
describe( `Test: .observe() + .set()`, function() {
|
|
12
|
-
|
|
13
|
-
describe( `Observe all changes.`, function() {
|
|
14
|
-
|
|
15
|
-
it( `Should recieve an event of one change on setting one prop.`, function() {
|
|
16
|
-
let obj = {}, _changes = [];
|
|
17
|
-
// -----
|
|
18
|
-
Observer.observe( obj, changes => {
|
|
19
|
-
_changes.push( ...changes );
|
|
20
|
-
} );
|
|
21
|
-
// -----
|
|
22
|
-
Observer.set( obj, {
|
|
23
|
-
key1: 'value1',
|
|
24
|
-
} );
|
|
25
|
-
Observer.set( obj, {
|
|
26
|
-
key1: 'value1',
|
|
27
|
-
}, { diff: true } );
|
|
28
|
-
// -----
|
|
29
|
-
expect( _changes ).to.be.an( 'array' ).with.length( 1 );
|
|
30
|
-
} );
|
|
31
|
-
|
|
32
|
-
it( `Should recieve an event of two changes on batch-setting two props.`, function() {
|
|
33
|
-
let obj = {}, _changes;
|
|
34
|
-
// -----
|
|
35
|
-
Observer.observe( obj, changes => {
|
|
36
|
-
_changes = changes;
|
|
37
|
-
} );
|
|
38
|
-
// -----
|
|
39
|
-
Observer.set( obj, {
|
|
40
|
-
key1: 'value1',
|
|
41
|
-
key2: 'value2',
|
|
42
|
-
} );
|
|
43
|
-
// -----
|
|
44
|
-
expect( _changes ).to.be.an( 'array' ).with.length( 2 );
|
|
45
|
-
} );
|
|
46
|
-
|
|
47
|
-
} );
|
|
48
|
-
|
|
49
|
-
describe( `Observe batched changes.`, function() {
|
|
50
|
-
|
|
51
|
-
it( `Should recieve multiple event of different mutation types.`, function() {
|
|
52
|
-
let obj = {}, _changes = [];
|
|
53
|
-
// -----
|
|
54
|
-
Observer.observe( obj, changes => {
|
|
55
|
-
_changes.push( ...changes );
|
|
56
|
-
} );
|
|
57
|
-
// -----
|
|
58
|
-
Observer.batch( obj, () => {
|
|
59
|
-
// This call to observe will not recieve events from the batch
|
|
60
|
-
Observer.observe( obj, changes => {
|
|
61
|
-
_changes.push( ...changes );
|
|
62
|
-
} );
|
|
63
|
-
Observer.set( obj, {
|
|
64
|
-
key1: 'value1',
|
|
65
|
-
} );
|
|
66
|
-
Observer.deleteProperty( obj, 'key1' );
|
|
67
|
-
} );
|
|
68
|
-
// -----
|
|
69
|
-
expect( _changes ).to.be.an( 'array' ).with.length( 2 );
|
|
70
|
-
} );
|
|
71
|
-
} );
|
|
72
|
-
|
|
73
|
-
describe( `Observe with namespaces.`, function() {
|
|
74
|
-
|
|
75
|
-
let obj = {},
|
|
76
|
-
ListenerRegistryCustomAddMethodCalled = false,
|
|
77
|
-
TrapsRegistryCustomAddMethodCalled = false,
|
|
78
|
-
_changesRecieved = [];
|
|
79
|
-
class ListenerRegistry2 extends ListenerRegistry {
|
|
80
|
-
// Catch when fireables added.
|
|
81
|
-
addRegistration( ...args ) {
|
|
82
|
-
ListenerRegistryCustomAddMethodCalled = true;
|
|
83
|
-
return super.addRegistration( ...args );
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
class TrapsRegistry2 extends TrapsRegistry {
|
|
87
|
-
// Catch when fireables added.
|
|
88
|
-
addRegistration( ...args ) {
|
|
89
|
-
TrapsRegistryCustomAddMethodCalled = true;
|
|
90
|
-
return super.addRegistration( ...args );
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
ListenerRegistry.namespace( 'ns1', ListenerRegistry2 );
|
|
94
|
-
TrapsRegistry.namespace( 'ns1', TrapsRegistry2 );
|
|
95
|
-
|
|
96
|
-
it( `Should assert that methods of an Observers namespace class are called.`, function() {
|
|
97
|
-
Observer.observe( obj, changes => {
|
|
98
|
-
_changesRecieved.push( changes );
|
|
99
|
-
}, {
|
|
100
|
-
namespace: 'ns1',
|
|
101
|
-
} );
|
|
102
|
-
expect( ListenerRegistryCustomAddMethodCalled ).to.be.true;
|
|
103
|
-
} );
|
|
104
|
-
|
|
105
|
-
it( `Should that "events" off the namespace dont't leak.`, function() {
|
|
106
|
-
_changesRecieved = [];
|
|
107
|
-
ListenerRegistryCustomAddMethodCalled = false;
|
|
108
|
-
TrapsRegistryCustomAddMethodCalled = false;
|
|
109
|
-
Observer.observe( obj, () => {} );
|
|
110
|
-
Observer.set( obj, {
|
|
111
|
-
key1: 'value1',
|
|
112
|
-
key2: 'value2',
|
|
113
|
-
} );
|
|
114
|
-
expect( _changesRecieved ).to.be.an( 'array' ).with.lengthOf( 0 );
|
|
115
|
-
expect( ListenerRegistryCustomAddMethodCalled ).to.be.false;
|
|
116
|
-
expect( TrapsRegistryCustomAddMethodCalled ).to.be.false;
|
|
117
|
-
} );
|
|
118
|
-
|
|
119
|
-
it( `Should assert that methods of an Interceptors namespace class are called.`, function() {
|
|
120
|
-
TrapsRegistryCustomAddMethodCalled = false;
|
|
121
|
-
Observer.intercept( obj, 'set', ( e, recieved, next ) => {
|
|
122
|
-
return next();
|
|
123
|
-
}, {
|
|
124
|
-
namespace: 'ns1',
|
|
125
|
-
} );
|
|
126
|
-
expect( TrapsRegistryCustomAddMethodCalled ).to.be.true;
|
|
127
|
-
} );
|
|
128
|
-
|
|
129
|
-
it( `Should assert that interceptor was called.`, function() {
|
|
130
|
-
let handlerWasCalled;
|
|
131
|
-
Observer.intercept( obj, {
|
|
132
|
-
set: ( e, recieved, next ) => {
|
|
133
|
-
handlerWasCalled = e.value;
|
|
134
|
-
e.value = 'new val';
|
|
135
|
-
return next();
|
|
136
|
-
},
|
|
137
|
-
} );
|
|
138
|
-
Observer.set( obj, 'someProp', 'some val' );
|
|
139
|
-
expect( handlerWasCalled ).to.be.eq( 'some val' );
|
|
140
|
-
expect( obj.someProp ).to.be.eq( 'new val' );
|
|
141
|
-
} );
|
|
142
|
-
|
|
143
|
-
it( `Should assert that "custom events" in the namespace fire.`, function() {
|
|
144
|
-
_changesRecieved = [];
|
|
145
|
-
ListenerRegistry2.getInstance( obj ).emit( [ {
|
|
146
|
-
key: 'costum-name', // required
|
|
147
|
-
type: 'costum-type', // required
|
|
148
|
-
} ] );
|
|
149
|
-
expect( _changesRecieved[ 0 ][ 0 ] ).to.be.an( 'object' ).that.includes( { key: 'costum-name', type: 'costum-type', } );
|
|
150
|
-
} );
|
|
151
|
-
|
|
152
|
-
it( `Should that "set" events in the namespace are recieved.`, function() {
|
|
153
|
-
_changesRecieved = [];
|
|
154
|
-
Observer.set( obj, {
|
|
155
|
-
key1: 'value1',
|
|
156
|
-
key2: 'value2',
|
|
157
|
-
}, {
|
|
158
|
-
namespace: 'ns1',
|
|
159
|
-
} );
|
|
160
|
-
expect( _changesRecieved[ 0 ] ).to.be.an( 'array' ).with.length( 2 );
|
|
161
|
-
} );
|
|
162
|
-
|
|
163
|
-
} );
|
|
164
|
-
|
|
165
|
-
describe( `Observe paths.`, function() {
|
|
166
|
-
|
|
167
|
-
it( `Observe a one-level path of an object.`, function() {
|
|
168
|
-
let obj = {}, _change;
|
|
169
|
-
// -----
|
|
170
|
-
Observer.observe( obj, 'key1', change => {
|
|
171
|
-
_change = change;
|
|
172
|
-
} );
|
|
173
|
-
// -----
|
|
174
|
-
Observer.set( obj, {
|
|
175
|
-
key1: 'value1',
|
|
176
|
-
} );
|
|
177
|
-
// -----
|
|
178
|
-
expect( _change ).to.be.an( 'object' ).that.includes( { key: 'key1', type: 'set', } );
|
|
179
|
-
} );
|
|
180
|
-
|
|
181
|
-
it( `Observe a two-level path of an object. (Using Observer.observe() with preflight option.)`, function() {
|
|
182
|
-
let obj = {}, _changes = [];
|
|
183
|
-
// -----
|
|
184
|
-
Observer.deep( obj, [ 'key1', 'sub.key1' ], Observer.observe, change => {
|
|
185
|
-
_changes.push( change );
|
|
186
|
-
}, { preflight: true } );
|
|
187
|
-
// -----
|
|
188
|
-
Observer.set( obj, {
|
|
189
|
-
key1: {},
|
|
190
|
-
key2: {},
|
|
191
|
-
} );
|
|
192
|
-
Observer.set( obj.key1, {
|
|
193
|
-
'sub.key1': {},
|
|
194
|
-
subkey1: {},
|
|
195
|
-
} );
|
|
196
|
-
// -----
|
|
197
|
-
expect( _changes ).to.have.lengthOf( 2 );
|
|
198
|
-
expect( _changes[ 0 ] ).to.be.an( 'object' ).that.deep.includes( { key: 'sub.key1', path: [ 'key1', 'sub.key1' ], type: 'get', value: undefined, } );
|
|
199
|
-
expect( _changes[ 1 ] ).to.be.an( 'object' ).that.deep.includes( { key: 'sub.key1', path: [ 'key1', 'sub.key1' ], type: 'set', value: {}, } );
|
|
200
|
-
} );
|
|
201
|
-
|
|
202
|
-
it( `Observe a two-level path of an object. (Using Observer.get() with "live" option.)`, function() {
|
|
203
|
-
let obj = {}, _changes = [];
|
|
204
|
-
// -----
|
|
205
|
-
Observer.deep( obj, [ 'key1', 'sub.key1' ], Observer.get, change => {
|
|
206
|
-
_changes.push( change );
|
|
207
|
-
}, { live: true } );
|
|
208
|
-
// -----
|
|
209
|
-
Observer.set( obj, {
|
|
210
|
-
key1: {},
|
|
211
|
-
key2: {},
|
|
212
|
-
} );
|
|
213
|
-
Observer.set( obj.key1, {
|
|
214
|
-
'sub.key1': {},
|
|
215
|
-
subkey1: {},
|
|
216
|
-
} );
|
|
217
|
-
// -----
|
|
218
|
-
expect( _changes ).to.have.lengthOf(
|
|
219
|
-
expect( _changes[
|
|
220
|
-
expect( _changes[
|
|
221
|
-
} );
|
|
222
|
-
|
|
223
|
-
it( `Observe path 0 of an array.`, function() {
|
|
224
|
-
let arr = [], _changes = [];
|
|
225
|
-
// -----
|
|
226
|
-
Observer.observe( arr, 0, change => {
|
|
227
|
-
_changes.push( change );
|
|
228
|
-
}
|
|
229
|
-
// -----
|
|
230
|
-
Observer.set( arr, 0, {} );
|
|
231
|
-
// -----
|
|
232
|
-
expect( _changes ).to.have.lengthOf(
|
|
233
|
-
expect( _changes[ 0 ] ).to.be.an( 'object' ).that.deep.includes( { key: 0, type: '
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
Observer.set( arr
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
expect( _changes ).to.
|
|
248
|
-
expect( _changes[
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
expect( _changes ).to.
|
|
269
|
-
expect( _changes[
|
|
270
|
-
expect( _changes[ 1 ] ).to.be.an( '
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
expect(
|
|
292
|
-
expect(
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
_obj.
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
expect(
|
|
312
|
-
expect( _obj === obj ).to.be.
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
Observer.
|
|
328
|
-
|
|
329
|
-
//
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
Observer.
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
//
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @imports
|
|
4
|
+
*/
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
import Observer from '../src/index.js';
|
|
7
|
+
import TrapsRegistry from '../src/core/TrapsRegistry.js';
|
|
8
|
+
import ListenerRegistry from '../src/core/ListenerRegistry.js';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
describe( `Test: .observe() + .set()`, function() {
|
|
12
|
+
|
|
13
|
+
describe( `Observe all changes.`, function() {
|
|
14
|
+
|
|
15
|
+
it( `Should recieve an event of one change on setting one prop.`, function() {
|
|
16
|
+
let obj = {}, _changes = [];
|
|
17
|
+
// -----
|
|
18
|
+
Observer.observe( obj, changes => {
|
|
19
|
+
_changes.push( ...changes );
|
|
20
|
+
} );
|
|
21
|
+
// -----
|
|
22
|
+
Observer.set( obj, {
|
|
23
|
+
key1: 'value1',
|
|
24
|
+
} );
|
|
25
|
+
Observer.set( obj, {
|
|
26
|
+
key1: 'value1',
|
|
27
|
+
}, { diff: true } );
|
|
28
|
+
// -----
|
|
29
|
+
expect( _changes ).to.be.an( 'array' ).with.length( 1 );
|
|
30
|
+
} );
|
|
31
|
+
|
|
32
|
+
it( `Should recieve an event of two changes on batch-setting two props.`, function() {
|
|
33
|
+
let obj = {}, _changes;
|
|
34
|
+
// -----
|
|
35
|
+
Observer.observe( obj, changes => {
|
|
36
|
+
_changes = changes;
|
|
37
|
+
} );
|
|
38
|
+
// -----
|
|
39
|
+
Observer.set( obj, {
|
|
40
|
+
key1: 'value1',
|
|
41
|
+
key2: 'value2',
|
|
42
|
+
} );
|
|
43
|
+
// -----
|
|
44
|
+
expect( _changes ).to.be.an( 'array' ).with.length( 2 );
|
|
45
|
+
} );
|
|
46
|
+
|
|
47
|
+
} );
|
|
48
|
+
|
|
49
|
+
describe( `Observe batched changes.`, function() {
|
|
50
|
+
|
|
51
|
+
it( `Should recieve multiple event of different mutation types.`, function() {
|
|
52
|
+
let obj = {}, _changes = [];
|
|
53
|
+
// -----
|
|
54
|
+
Observer.observe( obj, changes => {
|
|
55
|
+
_changes.push( ...changes );
|
|
56
|
+
} );
|
|
57
|
+
// -----
|
|
58
|
+
Observer.batch( obj, () => {
|
|
59
|
+
// This call to observe will not recieve events from the batch
|
|
60
|
+
Observer.observe( obj, changes => {
|
|
61
|
+
_changes.push( ...changes );
|
|
62
|
+
} );
|
|
63
|
+
Observer.set( obj, {
|
|
64
|
+
key1: 'value1',
|
|
65
|
+
} );
|
|
66
|
+
Observer.deleteProperty( obj, 'key1' );
|
|
67
|
+
} );
|
|
68
|
+
// -----
|
|
69
|
+
expect( _changes ).to.be.an( 'array' ).with.length( 2 );
|
|
70
|
+
} );
|
|
71
|
+
} );
|
|
72
|
+
|
|
73
|
+
describe( `Observe with namespaces.`, function() {
|
|
74
|
+
|
|
75
|
+
let obj = {},
|
|
76
|
+
ListenerRegistryCustomAddMethodCalled = false,
|
|
77
|
+
TrapsRegistryCustomAddMethodCalled = false,
|
|
78
|
+
_changesRecieved = [];
|
|
79
|
+
class ListenerRegistry2 extends ListenerRegistry {
|
|
80
|
+
// Catch when fireables added.
|
|
81
|
+
addRegistration( ...args ) {
|
|
82
|
+
ListenerRegistryCustomAddMethodCalled = true;
|
|
83
|
+
return super.addRegistration( ...args );
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
class TrapsRegistry2 extends TrapsRegistry {
|
|
87
|
+
// Catch when fireables added.
|
|
88
|
+
addRegistration( ...args ) {
|
|
89
|
+
TrapsRegistryCustomAddMethodCalled = true;
|
|
90
|
+
return super.addRegistration( ...args );
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
ListenerRegistry.namespace( 'ns1', ListenerRegistry2 );
|
|
94
|
+
TrapsRegistry.namespace( 'ns1', TrapsRegistry2 );
|
|
95
|
+
|
|
96
|
+
it( `Should assert that methods of an Observers namespace class are called.`, function() {
|
|
97
|
+
Observer.observe( obj, changes => {
|
|
98
|
+
_changesRecieved.push( changes );
|
|
99
|
+
}, {
|
|
100
|
+
namespace: 'ns1',
|
|
101
|
+
} );
|
|
102
|
+
expect( ListenerRegistryCustomAddMethodCalled ).to.be.true;
|
|
103
|
+
} );
|
|
104
|
+
|
|
105
|
+
it( `Should that "events" off the namespace dont't leak.`, function() {
|
|
106
|
+
_changesRecieved = [];
|
|
107
|
+
ListenerRegistryCustomAddMethodCalled = false;
|
|
108
|
+
TrapsRegistryCustomAddMethodCalled = false;
|
|
109
|
+
Observer.observe( obj, () => {} );
|
|
110
|
+
Observer.set( obj, {
|
|
111
|
+
key1: 'value1',
|
|
112
|
+
key2: 'value2',
|
|
113
|
+
} );
|
|
114
|
+
expect( _changesRecieved ).to.be.an( 'array' ).with.lengthOf( 0 );
|
|
115
|
+
expect( ListenerRegistryCustomAddMethodCalled ).to.be.false;
|
|
116
|
+
expect( TrapsRegistryCustomAddMethodCalled ).to.be.false;
|
|
117
|
+
} );
|
|
118
|
+
|
|
119
|
+
it( `Should assert that methods of an Interceptors namespace class are called.`, function() {
|
|
120
|
+
TrapsRegistryCustomAddMethodCalled = false;
|
|
121
|
+
Observer.intercept( obj, 'set', ( e, recieved, next ) => {
|
|
122
|
+
return next();
|
|
123
|
+
}, {
|
|
124
|
+
namespace: 'ns1',
|
|
125
|
+
} );
|
|
126
|
+
expect( TrapsRegistryCustomAddMethodCalled ).to.be.true;
|
|
127
|
+
} );
|
|
128
|
+
|
|
129
|
+
it( `Should assert that interceptor was called.`, function() {
|
|
130
|
+
let handlerWasCalled;
|
|
131
|
+
Observer.intercept( obj, {
|
|
132
|
+
set: ( e, recieved, next ) => {
|
|
133
|
+
handlerWasCalled = e.value;
|
|
134
|
+
e.value = 'new val';
|
|
135
|
+
return next();
|
|
136
|
+
},
|
|
137
|
+
} );
|
|
138
|
+
Observer.set( obj, 'someProp', 'some val' );
|
|
139
|
+
expect( handlerWasCalled ).to.be.eq( 'some val' );
|
|
140
|
+
expect( obj.someProp ).to.be.eq( 'new val' );
|
|
141
|
+
} );
|
|
142
|
+
|
|
143
|
+
it( `Should assert that "custom events" in the namespace fire.`, function() {
|
|
144
|
+
_changesRecieved = [];
|
|
145
|
+
ListenerRegistry2.getInstance( obj ).emit( [ {
|
|
146
|
+
key: 'costum-name', // required
|
|
147
|
+
type: 'costum-type', // required
|
|
148
|
+
} ] );
|
|
149
|
+
expect( _changesRecieved[ 0 ][ 0 ] ).to.be.an( 'object' ).that.includes( { key: 'costum-name', type: 'costum-type', } );
|
|
150
|
+
} );
|
|
151
|
+
|
|
152
|
+
it( `Should that "set" events in the namespace are recieved.`, function() {
|
|
153
|
+
_changesRecieved = [];
|
|
154
|
+
Observer.set( obj, {
|
|
155
|
+
key1: 'value1',
|
|
156
|
+
key2: 'value2',
|
|
157
|
+
}, {
|
|
158
|
+
namespace: 'ns1',
|
|
159
|
+
} );
|
|
160
|
+
expect( _changesRecieved[ 0 ] ).to.be.an( 'array' ).with.length( 2 );
|
|
161
|
+
} );
|
|
162
|
+
|
|
163
|
+
} );
|
|
164
|
+
|
|
165
|
+
describe( `Observe paths.`, function() {
|
|
166
|
+
|
|
167
|
+
it( `Observe a one-level path of an object.`, function() {
|
|
168
|
+
let obj = {}, _change;
|
|
169
|
+
// -----
|
|
170
|
+
Observer.observe( obj, 'key1', change => {
|
|
171
|
+
_change = change;
|
|
172
|
+
} );
|
|
173
|
+
// -----
|
|
174
|
+
Observer.set( obj, {
|
|
175
|
+
key1: 'value1',
|
|
176
|
+
} );
|
|
177
|
+
// -----
|
|
178
|
+
expect( _change ).to.be.an( 'object' ).that.includes( { key: 'key1', type: 'set', } );
|
|
179
|
+
} );
|
|
180
|
+
|
|
181
|
+
it( `Observe a two-level path of an object. (Using Observer.observe() with preflight option.)`, function() {
|
|
182
|
+
let obj = {}, _changes = [];
|
|
183
|
+
// -----
|
|
184
|
+
Observer.deep( obj, [ 'key1', 'sub.key1' ], Observer.observe, change => {
|
|
185
|
+
_changes.push( change );
|
|
186
|
+
}, { preflight: true } );
|
|
187
|
+
// -----
|
|
188
|
+
Observer.set( obj, {
|
|
189
|
+
key1: {},
|
|
190
|
+
key2: {},
|
|
191
|
+
} );
|
|
192
|
+
Observer.set( obj.key1, {
|
|
193
|
+
'sub.key1': {},
|
|
194
|
+
subkey1: {},
|
|
195
|
+
} );
|
|
196
|
+
// -----
|
|
197
|
+
expect( _changes ).to.have.lengthOf( 2 );
|
|
198
|
+
expect( _changes[ 0 ] ).to.be.an( 'object' ).that.deep.includes( { key: 'sub.key1', path: [ 'key1', 'sub.key1' ], type: 'get', value: undefined, } );
|
|
199
|
+
expect( _changes[ 1 ] ).to.be.an( 'object' ).that.deep.includes( { key: 'sub.key1', path: [ 'key1', 'sub.key1' ], type: 'set', value: {}, } );
|
|
200
|
+
} );
|
|
201
|
+
|
|
202
|
+
it( `Observe a two-level path of an object. (Using Observer.get() with "live" option.)`, function() {
|
|
203
|
+
let obj = {}, _changes = [];
|
|
204
|
+
// -----
|
|
205
|
+
Observer.deep( obj, [ 'key1', 'sub.key1' ], Observer.get, change => {
|
|
206
|
+
_changes.push( change );
|
|
207
|
+
}, { live: true } );
|
|
208
|
+
// -----
|
|
209
|
+
Observer.set( obj, {
|
|
210
|
+
key1: {},
|
|
211
|
+
key2: {},
|
|
212
|
+
} );
|
|
213
|
+
Observer.set( obj.key1, {
|
|
214
|
+
'sub.key1': {},
|
|
215
|
+
subkey1: {},
|
|
216
|
+
} );
|
|
217
|
+
// -----
|
|
218
|
+
expect( _changes ).to.have.lengthOf( 3 );
|
|
219
|
+
expect( _changes[ 1 ] ).to.be.an( 'object' ).that.deep.includes( { key: 'sub.key1', path: [ 'key1', 'sub.key1' ], type: 'get', value: undefined, } );
|
|
220
|
+
expect( _changes[ 2 ] ).to.be.an( 'object' ).that.deep.includes( { key: 'sub.key1', path: [ 'key1', 'sub.key1' ], type: 'set', value: {}, } );
|
|
221
|
+
} );
|
|
222
|
+
|
|
223
|
+
it( `Observe path 0 of an array.`, function() {
|
|
224
|
+
let arr = [], _changes = [];
|
|
225
|
+
// -----
|
|
226
|
+
Observer.observe( arr, 0, change => {
|
|
227
|
+
_changes.push( change );
|
|
228
|
+
} );
|
|
229
|
+
// -----
|
|
230
|
+
Observer.set( arr, 0, {} );
|
|
231
|
+
// -----
|
|
232
|
+
expect( _changes ).to.have.lengthOf( 1 );
|
|
233
|
+
expect( _changes[ 0 ] ).to.be.an( 'object' ).that.deep.includes( { key: 0, type: 'set', } );
|
|
234
|
+
} );
|
|
235
|
+
|
|
236
|
+
it( `Observe path [ 0, 'key1' ] of an array.`, function() {
|
|
237
|
+
let arr = [], _changes = [];
|
|
238
|
+
// -----
|
|
239
|
+
Observer.deep( arr, [ 0, 'key1' ], Observer.observe, change => {
|
|
240
|
+
_changes.push( change );
|
|
241
|
+
}, { preflight: true } );
|
|
242
|
+
// -----
|
|
243
|
+
Observer.set( arr, 0, {} );
|
|
244
|
+
Observer.set( arr[ 0 ], 'key1', {} );
|
|
245
|
+
// -----
|
|
246
|
+
expect( _changes ).to.have.lengthOf( 2 );
|
|
247
|
+
expect( _changes[ 0 ] ).to.be.an( 'object' ).that.deep.includes( { key: 'key1', path: [ 0, 'key1' ], type: 'get', } );
|
|
248
|
+
expect( _changes[ 1 ] ).to.be.an( 'object' ).that.deep.includes( { key: 'key1', path: [ 0, 'key1' ], type: 'set', } );
|
|
249
|
+
} );
|
|
250
|
+
|
|
251
|
+
it( `Observe wildcard paths.`, function() {
|
|
252
|
+
let obj = {}, _changes = [];
|
|
253
|
+
// -----
|
|
254
|
+
Observer.deep( obj, [ 'key1', Infinity ], Observer.observe, change => {
|
|
255
|
+
_changes.push( change );
|
|
256
|
+
}, { preflight: true } );
|
|
257
|
+
// -----
|
|
258
|
+
Observer.set( obj, {
|
|
259
|
+
key1: {},
|
|
260
|
+
key2: {},
|
|
261
|
+
} );
|
|
262
|
+
Observer.set( obj.key1, {
|
|
263
|
+
'sub.key1': {},
|
|
264
|
+
subkey1: {},
|
|
265
|
+
} );
|
|
266
|
+
// -----
|
|
267
|
+
expect( _changes ).to.have.lengthOf( 2 );
|
|
268
|
+
expect( _changes[ 0 ] ).to.be.an( 'array' ).lengthOf( 0 );
|
|
269
|
+
expect( _changes[ 1 ] ).to.be.an( 'array' ).lengthOf( 2 );
|
|
270
|
+
expect( _changes[ 1 ][ 0 ] ).to.be.an( 'object' ).that.deep.includes( { key: 'sub.key1', path: [ 'key1', 'sub.key1' ], type: 'set', } );
|
|
271
|
+
} );
|
|
272
|
+
|
|
273
|
+
} );
|
|
274
|
+
|
|
275
|
+
describe( `Accessorize/unaccessorize.`, function() {
|
|
276
|
+
|
|
277
|
+
it( `Should report a change on setting an accessorized prop. Should report nothing after unaccessorizing the prop.`, function() {
|
|
278
|
+
let obj = {}, _changes = [];
|
|
279
|
+
// -----
|
|
280
|
+
Observer.observe( obj, changes => {
|
|
281
|
+
_changes.push( changes );
|
|
282
|
+
} );
|
|
283
|
+
// -----
|
|
284
|
+
let accessorizeFlag = Observer.accessorize( obj, 'key11111' );
|
|
285
|
+
obj.key11111 = 'value1'; // Should fire event
|
|
286
|
+
// -----
|
|
287
|
+
let unaccessorizeFlag = Observer.unaccessorize( obj, 'key11111' );
|
|
288
|
+
obj.key11111 = 'value1-b'; // Should not fire event
|
|
289
|
+
// -----
|
|
290
|
+
expect( accessorizeFlag ).to.be.true;
|
|
291
|
+
expect( unaccessorizeFlag ).to.be.true;
|
|
292
|
+
expect( _changes ).to.be.an( 'array' ).with.length( 1 );
|
|
293
|
+
} );
|
|
294
|
+
|
|
295
|
+
} );
|
|
296
|
+
|
|
297
|
+
describe( `Proxy/unproxy.`, function() {
|
|
298
|
+
|
|
299
|
+
it( `Should report a change on setting a prop on a proxied instance.`, function() {
|
|
300
|
+
let obj = {}, _changes = [];
|
|
301
|
+
// -----
|
|
302
|
+
Observer.observe( obj, changes => {
|
|
303
|
+
_changes.push( changes );
|
|
304
|
+
} );
|
|
305
|
+
// -----
|
|
306
|
+
let _obj = Observer.proxy( obj );
|
|
307
|
+
_obj.key1 = 'value1'; // Should fire event
|
|
308
|
+
_obj.key2 = 'value2'; // Should fire event
|
|
309
|
+
// -----
|
|
310
|
+
expect( _changes ).to.be.an( 'array' ).with.length( 2 );
|
|
311
|
+
expect( _obj === obj ).to.be.false;
|
|
312
|
+
expect( Observer.unproxy( _obj ) === obj ).to.be.true;
|
|
313
|
+
} );
|
|
314
|
+
|
|
315
|
+
} );
|
|
316
|
+
|
|
317
|
+
describe( `Accessorize/unaccessorize.`, function() {
|
|
318
|
+
|
|
319
|
+
it( `Should report just a change on PROGRAMMATICALLY setting an already ACCESSORIZED prop.`, function() {
|
|
320
|
+
let obj = {}, _changes = [];
|
|
321
|
+
// -----
|
|
322
|
+
Observer.observe( obj, changes => {
|
|
323
|
+
_changes.push( changes );
|
|
324
|
+
} );
|
|
325
|
+
// -----
|
|
326
|
+
Observer.accessorize( obj, 'key1' );
|
|
327
|
+
Observer.set( obj, 'key1', 'value1' ); // Should fire just one event
|
|
328
|
+
// -----
|
|
329
|
+
obj.key1 = 'value1-b'; // Should fire event
|
|
330
|
+
// -----
|
|
331
|
+
expect( _changes ).to.be.an( 'array' ).with.length( 2 );
|
|
332
|
+
} );
|
|
333
|
+
|
|
334
|
+
it( `Should report just a change on PROGRAMMATICALLY setting an already ACCESSORIZED prop of a PROXIED instance.`, function() {
|
|
335
|
+
let obj = {}, _changes = [];
|
|
336
|
+
// -----
|
|
337
|
+
Observer.observe( obj, changes => {
|
|
338
|
+
_changes.push( changes );
|
|
339
|
+
} );
|
|
340
|
+
// -----
|
|
341
|
+
Observer.accessorize( obj, 'key1' );
|
|
342
|
+
let _obj = Observer.proxy( obj );
|
|
343
|
+
Observer.set( _obj, 'key1', 'value1' ); // Should fire just one event
|
|
344
|
+
// -----
|
|
345
|
+
obj.key1 = 'value1-b'; // Should fire event
|
|
346
|
+
// -----
|
|
347
|
+
expect( _changes ).to.be.an( 'array' ).with.length( 2 );
|
|
348
|
+
} );
|
|
349
|
+
|
|
350
|
+
} );
|
|
351
|
+
|
|
353
352
|
} );
|