Skip to content

Commit 421ed34

Browse files
committed
Add tests to check config modifications are persisted.
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 421ed34

File tree

3 files changed

+110
-1
lines changed

3 files changed

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

0 commit comments

Comments
 (0)