@@ -26,6 +26,17 @@ describe('set', function() {
2626 assert . equal ( o . a . b , 'c' ) ;
2727 } ) ;
2828
29+ it ( 'should create a nested array if it does not already exist' , function ( ) {
30+ const o = { } ;
31+ set ( o , 'a.0' , 'c' ) ;
32+ set ( o , 'a.1' , 'd' ) ;
33+ assert ( Array . isArray ( o . a ) ) ;
34+ assert . equal ( o . a [ 0 ] , 'c' ) ;
35+ let actual = "" ;
36+ o . a . map ( i => actual += i ) ;
37+ assert . equal ( actual , "cd" ) ;
38+ } ) ;
39+
2940 it ( 'should merge an existing value with the given value' , function ( ) {
3041 var o = { a : { b : { c : 'd' } } } ;
3142 set ( o , 'a.b' , { y : 'z' } , { merge : true } ) ;
@@ -199,25 +210,91 @@ describe('options', function() {
199210
200211 it ( 'should use a custom function to not split inside square brackets' , function ( ) {
201212 var o = { } ;
202- set ( o , " a.[b.c.d].e" , 'c' , options ) ;
213+ set ( o , ' a.[b.c.d].e' , 'c' , options ) ;
203214 assert . equal ( o . a [ '[b.c.d]' ] . e , 'c' ) ;
204215 } ) ;
205216
206217 it ( 'should use a custom function to not split inside parens' , function ( ) {
207218 var o = { } ;
208- set ( o , " a.(b.c.d).e" , 'c' , options ) ;
219+ set ( o , ' a.(b.c.d).e' , 'c' , options ) ;
209220 assert . equal ( o . a [ '(b.c.d)' ] . e , 'c' ) ;
210221 } ) ;
211222
212223 it ( 'should use a custom function to not split inside angle brackets' , function ( ) {
213224 var o = { } ;
214- set ( o , " a.<b.c.d>.e" , 'c' , options ) ;
225+ set ( o , ' a.<b.c.d>.e' , 'c' , options ) ;
215226 assert . equal ( o . a [ '<b.c.d>' ] . e , 'c' ) ;
216227 } ) ;
217228
218229 it ( 'should use a custom function to not split inside curly braces' , function ( ) {
219230 var o = { } ;
220- set ( o , " a.{b.c.d}.e" , 'c' , options ) ;
231+ set ( o , ' a.{b.c.d}.e' , 'c' , options ) ;
221232 assert . equal ( o . a [ '{b.c.d}' ] . e , 'c' ) ;
222233 } ) ;
223234} ) ;
235+
236+ const Rx = require ( 'rxjs' ) ;
237+ const opers = require ( 'rxjs/operators' ) ;
238+
239+ var _value = 0 ;
240+ var o = { a : { b : { } } } ;
241+ var obs = Rx . from ( new Rx . BehaviorSubject ( ) ) . pipe (
242+ opers . skip ( 1 ) ,
243+ ) ;
244+
245+ Object . defineProperty ( o . a . b , 'c' , {
246+ configurable : true ,
247+ get ( ) { return _value ; } ,
248+ set ( value ) {
249+ _value = value ;
250+ obs . next ( value ) ;
251+ }
252+ } ) ;
253+
254+ describe ( 'Setter with Observable' , function ( ) {
255+ // const expected = 11;
256+ var received = [ ] ;
257+ const noop = ( ) => { } ;
258+ it ( 'should only assign/emit once for each call of set' , function ( done ) {
259+ var subs = obs . subscribe (
260+ data => { received . push ( data ) ; } ,
261+ noop ,
262+ ( ) => {
263+ assert . equal ( received . length , 1 ) ;
264+ done ( ) ;
265+ }
266+ ) ;
267+ set ( o , 'a.b.c' , 5 ) ;
268+ subs . complete ( ) ;
269+ } ) ;
270+
271+ it ( 'should work assignment via setter' , function ( done ) {
272+ received = null ;
273+ var subs = obs . subscribe (
274+ data => { received = data ; } ,
275+ noop ,
276+ ( ) => {
277+ assert . equal ( received , 10 ) ;
278+ done ( ) ;
279+ }
280+ ) ;
281+ set ( o , 'a.b.c' , 10 ) ;
282+ subs . complete ( ) ;
283+ } ) ;
284+
285+ it ( 'should work with merge of object via setter' , function ( done ) {
286+ received = null ;
287+ set ( o , 'a.b.c' , { foo : 'bar' } ) ;
288+ var subs = obs . subscribe (
289+ data => { received = data ; } ,
290+ noop ,
291+ ( ) => {
292+ assert . deepEqual ( o . a . b . c , { foo : 'bar' , bing : 'bong' } ) ;
293+ done ( ) ;
294+ }
295+ ) ;
296+ set ( o , 'a.b.c' , { bing : 'bong' } , { merge : true } ) ;
297+ subs . complete ( ) ;
298+ } ) ;
299+
300+ } ) ;
0 commit comments