@@ -19,10 +19,12 @@ let expect = chai.expect
19
19
* Code under test
20
20
*/
21
21
const Initializer = require ( path . join ( process . cwd ( ) , 'core' , 'data' , 'Initializer' ) )
22
+ const initialize = Initializer . initialize
22
23
const traverse = Initializer . traverse
23
24
const assign = Initializer . assign
24
25
const select = Initializer . select
25
26
const map = Initializer . map
27
+ const project = Initializer . project
26
28
const getDeepProperty = Initializer . getDeepProperty
27
29
const setDeepProperty = Initializer . setDeepProperty
28
30
@@ -317,7 +319,33 @@ describe('Initializer', () => {
317
319
} )
318
320
319
321
describe ( 'project' , ( ) => {
320
- it ( 'should be tested' )
322
+ let mapping , source , target
323
+
324
+ beforeEach ( ( ) => {
325
+ source = {
326
+ q : 1 ,
327
+ r : { s : 2 }
328
+ }
329
+
330
+ mapping = {
331
+ 'q' : 'a' ,
332
+ 'r.s' : 'b.c.d' ,
333
+ 't' : 'e.f'
334
+ }
335
+
336
+ target = { }
337
+ } )
338
+
339
+ it ( 'should project properties to an object from a mapping' , ( ) => {
340
+ project ( mapping , source , target )
341
+ target . a . should . equal ( 1 )
342
+ target . b . c . d . should . equal ( 2 )
343
+ } )
344
+
345
+ it ( 'should ignore properties of the source not defined in the mapping' , ( ) => {
346
+ project ( mapping , source , target )
347
+ expect ( target . t ) . to . be . undefined
348
+ } )
321
349
} )
322
350
323
351
describe ( 'select' , ( ) => {
@@ -378,7 +406,71 @@ describe('Initializer', () => {
378
406
} )
379
407
380
408
describe ( 'initialize' , ( ) => {
381
- it ( 'should be tested' )
409
+ let schema , source , target , options
410
+
411
+ beforeEach ( ( ) => {
412
+ schema = { }
413
+ target = { }
414
+ } )
415
+
416
+ describe ( 'with mapping option' , ( ) => {
417
+
418
+ beforeEach ( ( ) => {
419
+ options = {
420
+ mapping : { }
421
+ }
422
+
423
+ sinon . spy ( Initializer , 'map' )
424
+ } )
425
+
426
+ it ( 'should map the source to the target' , ( ) => {
427
+ Initializer . initialize ( schema , source , target , options )
428
+ Initializer . map . should . have . been . calledWith ( options . mapping , { } , target )
429
+ } )
430
+
431
+ after ( ( ) => {
432
+ Initializer . map . restore ( )
433
+ } )
434
+ } )
435
+
436
+ describe ( 'with select option' , ( ) => {
437
+
438
+ beforeEach ( ( ) => {
439
+ options = {
440
+ select : [ ]
441
+ }
442
+
443
+ sinon . spy ( Initializer , 'select' )
444
+ } )
445
+
446
+ it ( 'should call select over the source object' , ( ) => {
447
+ Initializer . initialize ( schema , source , target , options )
448
+ Initializer . select . should . have . been . calledWith ( options . select , { } , target )
449
+ } )
450
+
451
+ after ( ( ) => {
452
+ Initializer . select . restore ( )
453
+ } )
454
+ } )
455
+
456
+ describe ( 'with no option' , ( ) => {
457
+
458
+ beforeEach ( ( ) => {
459
+ options = { }
460
+ sinon . spy ( Initializer , 'traverse' )
461
+ } )
462
+
463
+ it ( 'should traverse the source object' , ( ) => {
464
+ Initializer . initialize ( schema , source , target , options )
465
+ Initializer . traverse . should . have . been . calledWith ( schema , { } , target , Initializer . assign , options )
466
+ } )
467
+
468
+ after ( ( ) => {
469
+ Initializer . traverse . restore ( )
470
+ } )
471
+
472
+ } )
473
+
382
474
} )
383
475
384
476
} )
0 commit comments