-
Notifications
You must be signed in to change notification settings - Fork 2
/
split-into-overlay.test.js
157 lines (147 loc) · 4.19 KB
/
split-into-overlay.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const splitIntoOverlay = require('./split-into-overlay')
describe('splitIntoOverlay', () => {
it('create an empty overlay definition by default', async () => {
const overlay = splitIntoOverlay({})
expect(overlay).toEqual({
overlays: '1.0.0'
})
})
it('should use exact value for "update"', async () => {
const input = {
one: {},
two: null,
three: '',
}
const overlay = splitIntoOverlay(input, {targets: '$.*'})
expect(overlay).toEqual({
overlays: '1.0.0',
actions: [{
target: '$["one"]',
update: {}
},{
target: '$["two"]',
update: null
},{
target: '$["three"]',
update: ''
}],
})
})
it('should allow multiple paths', async () => {
const input = {
one: 1,
two: 2
}
const overlay = splitIntoOverlay(input, {targets: ['$.one', '$.two']})
expect(overlay).toEqual({
overlays: '1.0.0',
actions: [{
target: '$["one"]',
update: 1
},{
target: '$["two"]',
update: 2
}],
})
})
it('should produce a jsonPath for each matched path and update', async () => {
const input = {
one: {},
two: {},
three: {
four: 4
}
}
const overlay = splitIntoOverlay(input, {targets: '$.*'})
expect(overlay).toEqual({
overlays: '1.0.0',
actions: [
{
target: '$["one"]',
update: {}
},
{
target: '$["two"]',
update: {}
},
{
target: '$["three"]',
update: {
four: 4
}
}
]
})
})
it('should support fields', async () => {
const input = {
paths: {
'/foo': {
get: {
description: 'desc0',
summary: 'sum0',
operationId: 'getFoo'
},
post: {
description: 'desc1',
summary: 'sum1',
operationId: 'postFoo'
}
}
}
}
const overlay = splitIntoOverlay(input, {
targets: '$.paths.*.*',
fields: ['description', 'summary'],
})
expect(overlay).toEqual({
overlays: '1.0.0',
actions: [{
target: '$["paths"]["/foo"]["get"]',
update: {
description: 'desc0',
summary: 'sum0'
}
},{
target: '$["paths"]["/foo"]["post"]',
update: {
description: 'desc1',
summary: 'sum1'
}
}],
})
})
it('should support where clause as JSONPath', async () => {
const input = {
paths: {
'/foo': {
get: {
description: 'desc0',
summary: 'sum0',
tags: ['user']
},
post: {
description: 'desc1',
summary: 'sum1',
tags: ['pet']
}
}
}
}
const overlay = splitIntoOverlay(input, {
targets: '$.paths.*.*',
where: '$.tags[?(@ == "user")]'
})
expect(overlay).toEqual({
overlays: '1.0.0',
actions: [{
target: '$["paths"]["/foo"]["get"]',
update: {
description: 'desc0',
summary: 'sum0',
tags: ['user']
}
}],
})
})
})