Skip to content

Commit

Permalink
MF-74 Config library should import default config file using import-m…
Browse files Browse the repository at this point in the history
…ap (openmrs#2)

* Import config.json from import map working. Jest failing. 404s print to console.

* Fix tests

* Do lookup per Joel's advice -- much better error handling

* Get tests working; put config-file resolution into a function; weird eslint error?

* Fixing build
  • Loading branch information
brandones authored and joeldenning committed Oct 26, 2019
1 parent 5f30cb7 commit f74cdbc
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/esm-config/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
setupFiles: []
setupFiles: ["<rootDir>/src/setup-tests.js"]
};
219 changes: 211 additions & 8 deletions packages/esm-config/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/esm-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
"@babel/preset-env": "^7.6.0",
"@babel/preset-typescript": "^7.6.0",
"@types/jest": "^24.0.18",
"babel-eslint": "^10.0.3",
"@types/systemjs": "^0.20.6",
"babel-eslint": "^11.0.0-beta.0",
"babel-jest": "^24.9.0",
"babel-loader": "^8.0.6",
"babel-plugin-ramda": "^2.0.0",
"browserslist-config-openmrs": "^1.0.0",
"clean-webpack-plugin": "^3.0.0",
"concurrently": "^5.0.0",
"eslint": "^6.5.1",
"eslint-config-prettier": "^6.3.0",
"eslint-config-ts-important-stuff": "^1.0.0",
Expand Down
38 changes: 38 additions & 0 deletions packages/esm-config/src/module-config/module-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,41 @@ describe("getConfig", () => {
expect(config.foo.baz.quy).toBe("xyz");
});
});

describe("resolveImportMapConfig", () => {
afterEach(() => {
Config.clearAll();
(<any>window).System.resolve.mockReset();
(<any>window).System.import.mockReset();
});

it("gets config file from import map", async () => {
Config.defineConfigSchema("foo-module", { foo: { default: "qux" } });
const testConfig = { "foo-module": { foo: "bar" } };
(<any>window).System.resolve = jest.fn();
(<any>window).System.import = jest.fn().mockResolvedValue(testConfig);
await Config.resolveImportMapConfig();
const config = Config.getConfig("foo-module");
expect(config.foo).toBe("bar");
});

it("always puts config file from import map at lowest priority", async () => {
Config.defineConfigSchema("foo-module", { foo: { default: "qux" } });
const importedConfig = { "foo-module": { foo: "bar" } };
(<any>window).System.resolve = jest.fn();
(<any>window).System.import = jest.fn().mockResolvedValue(importedConfig);
const providedConfig = { "foo-module": { foo: "baz" } };
Config.provide(providedConfig);
await Config.resolveImportMapConfig();
const config = Config.getConfig("foo-module");
expect(config.foo).toBe("baz");
});

it("does not 404 when no config file is in the import map", async () => {
Config.defineConfigSchema("foo-module", { foo: { default: "qux" } });
// this line below is actually all that the test requires, the rest is sanity checking
expect(Config.resolveImportMapConfig).not.toThrow();
const config = Config.getConfig("foo-module");
expect(config.foo).toBe("qux");
});
});
22 changes: 22 additions & 0 deletions packages/esm-config/src/module-config/module-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ const configs: object[] = [];
// An object with module names for keys and schemas for values.
const schemas = {};

// resolveImportMapConfig -- not working
// Pull default config file from the import map. Module 'config-file'
// TODO: Get this to actually work
let importMapConfigHasBeenAdded = false;
export async function resolveImportMapConfig() {
if (importMapConfigHasBeenAdded) return;
let importMapConfigExists;
try {
System.resolve("config-file");
importMapConfigExists = true;
} catch {
importMapConfigExists = false;
}

if (importMapConfigExists) {
await System.import("config-file").then(res => {
configs.unshift(res);
importMapConfigHasBeenAdded = true;
});
}
}

export function defineConfigSchema(moduleName, schema) {
// console.log( "defineConfigSchema received schema for " + moduleName + ": " + JSON.stringify(schema));
schemas[moduleName] = schema;
Expand Down
9 changes: 9 additions & 0 deletions packages/esm-config/src/setup-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
window.System = {
import: jest
.fn()
.mockRejectedValue(new Error("config.json not available in import map")),
resolve: jest.fn().mockImplementation(() => {
throw new Error("config.json not available in import map");
}),
register: jest.fn()
};
1 change: 0 additions & 1 deletion packages/esm-config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = {
resolve: {
extensions: [".ts", ".js", ".tsx", ".jsx"]
},
externals: ["openmrs-config"],
plugins: [new CleanWebpackPlugin(), new ForkTsCheckerWebpackPlugin()],
devServer: {
disableHostCheck: true,
Expand Down

0 comments on commit f74cdbc

Please sign in to comment.