-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.test.js
More file actions
226 lines (194 loc) · 7.37 KB
/
config.test.js
File metadata and controls
226 lines (194 loc) · 7.37 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var assert = require('assert');
const cfg = require('./config');
const fs = require('node:fs');
const mock_files = require('mock-fs');
function fieldHelper(name, typ = "text", required = true) {
return {
name: name, type: typ, required: required
}
}
describe("Configuration tests", () => {
test('initial empty config', () => {
mock_files({
'./empty/app/config.json': '{}'
})
withCurrent(
"./empty",
new cfg.PluginConfig(),
(c) => {
c.setFields([fieldHelper("A")])
c.persistConfig()
},
(original, updated, c) => {
expect(original.fields).toBeUndefined()
expect(updated.fields[0].name).toStrictEqual("A")
expect(c.fields[0].name).toStrictEqual("A")
// We expect a non-null value by default, but can't construct something
// to compare against as the tmp directory given may be different on
// each call
expect(original.uploadPath).not.toBeNull()
expect(updated.uploadPath).not.toBeNull()
expect(c.uploadPath).not.toBeNull()
expect(updated.sheetSelection).not.toBeNull()
expect(updated.sheetSelection).toStrictEqual("automatic")
}
);
mock_files.restore();
});
test('existing fields', () => {
mock_files({
'./fields/app/config.json': '{"fields": [{"name":"A"}, {"name": "B"}, {"name": "C"}]}',
})
withCurrent(
"./fields",
new cfg.PluginConfig(),
(c) => {
c.setFields([fieldHelper("A"), fieldHelper("B")])
c.persistConfig()
},
(original, updated, c) => {
expect(original.fields.map((x) => x.name)).toStrictEqual(["A", "B", "C"])
expect(updated.fields.map((x) => x.name)).toStrictEqual(["A", "B"])
expect(c.fields.map((x) => x.name)).toStrictEqual(["A", "B"])
}
);
mock_files.restore();
});
test('migration from only field name to inc type and required', () => {
mock_files({
'./fields/app/config.json': '{"fields": [{"name":"A"}, {"name": "B"}, {"name": "C"}]}',
})
withCurrent(
"./fields",
new cfg.PluginConfig(),
(c) => {
c.setFields([fieldHelper("A"), fieldHelper("B")])
c.persistConfig()
},
(original, updated, c) => {
expect(original.fields.map((x) => x.name)).toStrictEqual(["A", "B", "C"])
expect(updated.fields.map((x) => x.name)).toStrictEqual(["A", "B"])
expect(c.fields.map((x) => x.name)).toStrictEqual(["A", "B"])
expect(c.fields.map((x) => x.type)).toStrictEqual(["text", "text"])
expect(c.fields.map((x) => x.required)).toStrictEqual([true, true])
}
);
mock_files.restore();
});
test('supports new field structure', () => {
mock_files({
'./fields/app/config.json': '{"fields": [{"name":"A", "type": "number"}, {"name": "B"}, {"name": "C"}]}',
})
withCurrent(
"./fields",
new cfg.PluginConfig(),
(c) => {
c.setFields([fieldHelper("A"), fieldHelper("B", "number")])
c.persistConfig()
},
(original, updated, c) => {
expect(original.fields.map((x) => x.name)).toStrictEqual(["A", "B", "C"])
expect(updated.fields.map((x) => x.name)).toStrictEqual(["A", "B"])
expect(c.fields.map((x) => x.name)).toStrictEqual(["A", "B"])
expect(c.fields.map((x) => x.type)).toStrictEqual(["text", "number"])
expect(c.fields.map((x) => x.required)).toStrictEqual([true, true])
}
);
mock_files.restore();
});
test('existing path is not modified', () => {
mock_files({
'./path/app/config.json': '{"uploadPath": "/tmp"}',
})
withCurrent(
"./path",
new cfg.PluginConfig(),
(c) => {
c.setFields([fieldHelper("A"), fieldHelper("B", "number")])
c.persistConfig()
},
(original, updated) => {
expect(original.uploadPath).toBe("/tmp")
expect(updated.uploadPath).toBe("/tmp")
}
);
mock_files.restore();
});
test('manual sheet selection', () => {
mock_files({
'./fields/app/config.json': '{"fields": [{"name":"A"}, {"name": "B"}, {"name": "C"}]}',
})
withCurrent(
"./fields",
new cfg.PluginConfig(),
(c) => {
c.setSheetSelection("manual")
c.persistConfig()
},
(original, updated) => {
expect(updated.sheetSelection).toStrictEqual("manual")
}
);
mock_files.restore();
});
test('default sheet selection', () => {
mock_files({
'./fields/app/config.json': '{"fields": [{"name":"A"}, {"name": "B"}, {"name": "C"}]}',
})
withCurrent(
"./fields",
new cfg.PluginConfig(),
(c) => {
c.persistConfig()
},
(original, updated) => {
expect(updated.sheetSelection).toStrictEqual("automatic")
}
);
mock_files.restore();
});
test('invalid sheet selection', () => {
mock_files({
'./fields/app/config.json': '{"fields": [{"name":"A"}, {"name": "B"}, {"name": "C"}]}',
})
withCurrent(
"./fields",
new cfg.PluginConfig(),
(c) => {
expect(() => {
c.setSheetSelection("invalid")
}
).toThrow("'invalid' is not a valid sheet selection mode")
c.persistConfig()
},
(original, updated) => {
expect(updated.sheetSelection).toStrictEqual("automatic")
}
);
mock_files.restore();
});
})
// withCurrent is a helper function which is used to load a plugin config
// apply some changes, and then assert the expectations of the state.
//
// rootFolder is the folder where the `app/config.json` can
// be found in this test.
//
// pluginConfig is a newly constructed PluginConfig
//
// cbFunction is a callback function that will be given the
// plugin configuration so that changes can be made via its API.
//
// assertionsFunc is a function that is provided with:
// * the original json object (before cbFunction was called)
// * the new json object post-cbFunction
// * the pluginConfig that was modified by cbFunction
function withCurrent(rootFolder, pluginConfig, cbFunction, assertionsFunc) {
assert(typeof (cbFunction) === 'function')
assert(typeof (assertionsFunc) === 'function')
process.env.KIT_PROJECT_DIR = rootFolder
const original = JSON.parse(fs.readFileSync(pluginConfig.configPath, 'utf8'));
cbFunction(pluginConfig)
const updated = JSON.parse(fs.readFileSync(pluginConfig.configPath, 'utf8'));
assertionsFunc(original, updated, pluginConfig)
}