Skip to content

Commit 78678b1

Browse files
authored
Add tests to check config modifications are persisted. (#154)
When updating the configuration object, we want to make sure changes are persisted to disk so this commit adds tests to check that functionality does what is expected.
1 parent 3c0f233 commit 78678b1

File tree

4 files changed

+123
-2
lines changed

4 files changed

+123
-2
lines changed

lib/importer/package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/importer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"eslint": "^9.19.0",
3030
"eslint-plugin-jest": "^28.11.0",
3131
"globals": "^15.14.0",
32+
"mock-fs": "^5.5.0",
3233
"jest": "^29.7.0"
3334
}
3435
}

lib/importer/src/config.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
var assert = require('assert');
2+
const cfg = require('./config');
3+
const fs = require('node:fs');
4+
const mock_files = require('mock-fs');
5+
6+
7+
describe("Configuration tests", () => {
8+
9+
test('initial empty config', () => {
10+
mock_files({
11+
'./empty/app/config.json': '{}'
12+
})
13+
14+
withCurrent(
15+
"./empty",
16+
new cfg.PluginConfig(),
17+
(c) => {
18+
c.setFields(["A"])
19+
},
20+
(original, updated, c) => {
21+
expect(original.fields).toBeUndefined()
22+
expect(updated.fields).toStrictEqual(["A"])
23+
expect(c.fields).toStrictEqual(["A"])
24+
25+
// We expect a non-null value by default, but can't construct something
26+
// to compare against as the tmp directory given may be different on
27+
// each call
28+
expect(original.uploadPath).not.toBeNull()
29+
expect(updated.uploadPath).not.toBeNull()
30+
expect(c.uploadPath).not.toBeNull()
31+
}
32+
);
33+
34+
mock_files.restore();
35+
});
36+
37+
test('existing fields', () => {
38+
mock_files({
39+
'./fields/app/config.json': '{"fields": ["A", "B", "C"]}',
40+
})
41+
42+
withCurrent(
43+
"./fields",
44+
new cfg.PluginConfig(),
45+
(c) => {
46+
c.setFields(["A", "B"])
47+
},
48+
(original, updated, c) => {
49+
expect(original.fields).toStrictEqual(["A", "B", "C"])
50+
expect(updated.fields).toStrictEqual(["A", "B"])
51+
expect(c.fields).toStrictEqual(["A", "B"])
52+
}
53+
);
54+
55+
mock_files.restore();
56+
});
57+
58+
test('existing path', () => {
59+
mock_files({
60+
'./path/app/config.json': '{"uploadPath": "/tmp"}',
61+
})
62+
63+
withCurrent(
64+
"./path",
65+
new cfg.PluginConfig(),
66+
(c) => {
67+
c.setUploadPath("/opt")
68+
},
69+
(original, updated, c) => {
70+
expect(original.uploadPath).toBe("/tmp")
71+
expect(updated.uploadPath).toBe("/opt")
72+
expect(c.uploadPath).toBe("/opt")
73+
}
74+
);
75+
76+
mock_files.restore();
77+
});
78+
79+
})
80+
81+
82+
// withCurrent is a helper function which is used to load a plugin config
83+
// apply some changes, and then assert the expectations of the state.
84+
//
85+
// rootFolder is the folder where the `app/config.json` can
86+
// be found in this test.
87+
//
88+
// pluginConfig is a newly constructed PluginConfig
89+
//
90+
// cbFunction is a callback function that will be given the
91+
// plugin configuration so that changes can be made via its API.
92+
//
93+
// assertionsFunc is a function that is provided with:
94+
// * the original json object (before cbFunction was called)
95+
// * the new json object post-cbFunction
96+
// * the pluginConfig that was modified by cbFunction
97+
function withCurrent(rootFolder, pluginConfig, cbFunction, assertionsFunc) {
98+
assert(typeof (cbFunction) === 'function')
99+
assert(typeof (assertionsFunc) === 'function')
100+
101+
process.env.KIT_PROJECT_DIR = rootFolder
102+
103+
const original = JSON.parse(fs.readFileSync(pluginConfig.configPath, 'utf8'));
104+
cbFunction(pluginConfig)
105+
const updated = JSON.parse(fs.readFileSync(pluginConfig.configPath, 'utf8'));
106+
assertionsFunc(original, updated, pluginConfig)
107+
}

prototypes/basic/package-lock.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)