Skip to content
This repository was archived by the owner on Feb 25, 2019. It is now read-only.

Commit ff262b2

Browse files
committed
test(Initializer): test initialize and project
1 parent 4074525 commit ff262b2

File tree

1 file changed

+94
-2
lines changed

1 file changed

+94
-2
lines changed

test/unit/core/data/InitializerSpec.js

+94-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ let expect = chai.expect
1919
* Code under test
2020
*/
2121
const Initializer = require(path.join(process.cwd(), 'core', 'data', 'Initializer'))
22+
const initialize = Initializer.initialize
2223
const traverse = Initializer.traverse
2324
const assign = Initializer.assign
2425
const select = Initializer.select
2526
const map = Initializer.map
27+
const project = Initializer.project
2628
const getDeepProperty = Initializer.getDeepProperty
2729
const setDeepProperty = Initializer.setDeepProperty
2830

@@ -317,7 +319,33 @@ describe('Initializer', () => {
317319
})
318320

319321
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+
})
321349
})
322350

323351
describe('select', () => {
@@ -378,7 +406,71 @@ describe('Initializer', () => {
378406
})
379407

380408
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+
382474
})
383475

384476
})

0 commit comments

Comments
 (0)