-
Notifications
You must be signed in to change notification settings - Fork 0
/
object2object.test.js
115 lines (109 loc) · 2.38 KB
/
object2object.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const { o2o, MapperType } = require('../build');
describe('test', () => {
it('it works', () => {
const result = o2o({}, []);
expect(JSON.stringify(result)).toBe('{}');
});
it('can map', () => {
const result = o2o(
{
abc: 'abc',
},
[
{
type: MapperType.mapping,
originalFieldName: 'abc',
fieldName: 'cba',
},
],
);
expect(result.cba).toBe('abc');
});
it('can map from deep fields', () => {
const original = {
abc: {
def: {
ghi: 'ghi',
},
},
};
const mapper = {
type: MapperType.mapping,
originalFieldName: 'abc.def.ghi',
fieldName: 'cba',
};
const result = o2o(original, [mapper]);
expect(result.cba).toBe('ghi');
});
it('can map from constants', () => {
const original = {
abc: {
def: {
ghi: 'ghi',
},
},
};
const mapper = {
type: MapperType.mapping,
originalFieldName: 'abc.def.ghi',
fieldName: 'cba',
};
const result = o2o(original, [mapper]);
expect(result.cba).toBe('ghi');
});
it('can set to deep fields', () => {
const original = {
abc: {
def: {
ghi: 'ghi',
},
},
};
const mapper = {
type: MapperType.mapping,
originalFieldName: 'abc.def.ghi',
fieldName: 'cba.ihg',
};
const result = o2o(original, [mapper]);
expect(result.cba.ihg).toBe('ghi');
});
it('can map many fields', () => {
const original = {
abc: {
def1: {
ghi1: 'ghi1',
},
def2: {
ghi2: 'ghi2',
},
},
};
const mapper1 = {
type: MapperType.mapping,
originalFieldName: 'abc.def1.ghi1',
fieldName: 'cba1',
};
const mapper2 = {
type: MapperType.mapping,
originalFieldName: 'abc.def2.ghi2',
fieldName: 'cba2',
};
const result = o2o(original, [mapper1, mapper2]);
expect(result.cba1).toBe('ghi1');
expect(result.cba2).toBe('ghi2');
});
it('get null when original fields not found', () => {
const original = {
abc: {
def: 'def',
},
};
const mapper = {
type: MapperType.mapping,
originalFieldName: 'abc.def.ghi',
fieldName: 'cba',
};
const result = o2o(original, [mapper]);
expect(result.cba).toBe(null);
});
});