1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 | let db;
|
8 |
|
9 | describe('model-utils', () => {
|
10 | context('coerce', () => {
|
11 | before(() => {
|
12 |
|
13 | db = getSchema();
|
14 | });
|
15 | it('coerces nested properties', () => {
|
16 | const nestedModel = db.define('nestedModel', {
|
17 | rootProp: {
|
18 | numProp: Number,
|
19 | dateProp: Date,
|
20 | nestedArray: [{
|
21 | numProp: Number,
|
22 | dateProp: Date,
|
23 | arrayWithinArray: [{
|
24 | numProp: Number,
|
25 | dateProp: Date,
|
26 | }],
|
27 | objectWithinArray: {
|
28 | numProp: Number,
|
29 | dateProp: Date,
|
30 | },
|
31 | }],
|
32 | nestedObject: {
|
33 | numProp: Number,
|
34 | dateProp: Date,
|
35 | arrayWithinObject: [{
|
36 | numProp: Number,
|
37 | dateProp: Date,
|
38 | }],
|
39 | objectWithinObject: {
|
40 | numProp: Number,
|
41 | dateProp: Date,
|
42 | },
|
43 | },
|
44 | },
|
45 | });
|
46 | const dateVal = new Date().toString();
|
47 | const data = {
|
48 | rootProp: {
|
49 | numProp: '0',
|
50 | dateProp: dateVal,
|
51 | nestedArray: [{
|
52 | numProp: '1',
|
53 | dateProp: dateVal,
|
54 | arrayWithinArray: [
|
55 | {
|
56 | numProp: '2',
|
57 | dateProp: dateVal,
|
58 | },
|
59 | ],
|
60 | objectWithinArray: {
|
61 | numProp: '3',
|
62 | dateProp: dateVal,
|
63 | },
|
64 | }],
|
65 | nestedObject: {
|
66 | numProp: '5',
|
67 | dateProp: dateVal,
|
68 | arrayWithinObject: [{
|
69 | numProp: '6',
|
70 | dateProp: dateVal,
|
71 | }],
|
72 | objectWithinObject: {
|
73 | numProp: '7',
|
74 | dateProp: dateVal,
|
75 | },
|
76 | },
|
77 | },
|
78 | };
|
79 | const coercedData = nestedModel._coerce(data, {});
|
80 | assertInstanceOf(coercedData.rootProp.numProp, Number);
|
81 | assertInstanceOf(coercedData.rootProp.dateProp, Date);
|
82 | assertInstanceOf(coercedData.rootProp.nestedArray[0].numProp, Number);
|
83 | assertInstanceOf(coercedData.rootProp.nestedArray[0].dateProp, Date);
|
84 | assertInstanceOf(coercedData.rootProp.nestedArray[0].arrayWithinArray[0].numProp, Number);
|
85 | assertInstanceOf(coercedData.rootProp.nestedArray[0].arrayWithinArray[0].dateProp, Date);
|
86 | assertInstanceOf(coercedData.rootProp.nestedArray[0].objectWithinArray.numProp, Number);
|
87 | assertInstanceOf(coercedData.rootProp.nestedArray[0].objectWithinArray.dateProp, Date);
|
88 | assertInstanceOf(coercedData.rootProp.nestedObject.numProp, Number);
|
89 | assertInstanceOf(coercedData.rootProp.nestedObject.dateProp, Date);
|
90 | assertInstanceOf(coercedData.rootProp.nestedObject.objectWithinObject.numProp, Number);
|
91 | assertInstanceOf(coercedData.rootProp.nestedObject.objectWithinObject.dateProp, Date);
|
92 | assertInstanceOf(coercedData.rootProp.nestedObject.arrayWithinObject[0].numProp, Number);
|
93 | assertInstanceOf(coercedData.rootProp.nestedObject.arrayWithinObject[0].dateProp, Date);
|
94 | });
|
95 | function assertInstanceOf(val, type) {
|
96 | val.should.be.instanceOf(type);
|
97 | }
|
98 | });
|
99 | });
|