|
| 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 | +} |
0 commit comments