Skip to content

Commit 0207d4d

Browse files
authored
(feat) Provide function for obtaining default config tree in test code (#863)
1 parent 4a0e8ab commit 0207d4d

File tree

4 files changed

+73
-21
lines changed

4 files changed

+73
-21
lines changed

packages/framework/esm-framework/docs/API.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
- [age](API.md#age)
199199
- [canAccessStorage](API.md#canaccessstorage)
200200
- [daysIntoYear](API.md#daysintoyear)
201+
- [getDefaultsFromConfigSchema](API.md#getdefaultsfromconfigschema)
201202
- [isSameDay](API.md#issameday)
202203
- [isVersionSatisfied](API.md#isversionsatisfied)
203204
- [retry](API.md#retry)
@@ -4841,6 +4842,35 @@ The number of days.
48414842

48424843
___
48434844

4845+
### getDefaultsFromConfigSchema
4846+
4847+
**getDefaultsFromConfigSchema**(`schema`): `Object`
4848+
4849+
Given a config schema, this returns an object like is returned by `useConfig`
4850+
with all default values.
4851+
4852+
This should be used in tests and not in production code.
4853+
4854+
If all you need is the default values in your tests, these are returned by
4855+
default from the `useConfig`/`getConfig` mock. This function is useful if you
4856+
need to override some of the default values.
4857+
4858+
#### Parameters
4859+
4860+
| Name | Type |
4861+
| :------ | :------ |
4862+
| `schema` | `any` |
4863+
4864+
#### Returns
4865+
4866+
`Object`
4867+
4868+
#### Defined in
4869+
4870+
[packages/framework/esm-utils/src/test-helpers.ts:13](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/test-helpers.ts#L13)
4871+
4872+
___
4873+
48444874
### isSameDay
48454875

48464876
**isSameDay**(`firstDate`, `secondDate`): `boolean`

packages/framework/esm-framework/mock.tsx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import { createStore, type StoreApi } from 'zustand';
44
import { NEVER, of } from 'rxjs';
55
import { interpolateUrl } from '@openmrs/esm-config';
66
import { type SessionStore } from '@openmrs/esm-api';
7-
export { parseDate, formatDate, formatDatetime, formatTime, age } from '@openmrs/esm-utils';
7+
import { getDefaultsFromConfigSchema } from '@openmrs/esm-utils';
8+
export {
9+
getDefaultsFromConfigSchema,
10+
parseDate,
11+
formatDate,
12+
formatDatetime,
13+
formatTime,
14+
age,
15+
} from '@openmrs/esm-utils';
816
export { interpolateString, interpolateUrl, validators, validator } from '@openmrs/esm-config';
917

1018
window.i18next = { ...window.i18next, language: 'en' };
@@ -135,28 +143,10 @@ export enum Type {
135143
}
136144

137145
let configSchema = {};
138-
function getDefaults(schema) {
139-
let tmp = {};
140-
for (let k of Object.keys(schema)) {
141-
if (schema[k].hasOwnProperty('_default')) {
142-
tmp[k] = schema[k]._default;
143-
} else if (k.startsWith('_')) {
144-
continue;
145-
} else if (isOrdinaryObject(schema[k])) {
146-
tmp[k] = getDefaults(schema[k]);
147-
} else {
148-
tmp[k] = schema[k];
149-
}
150-
}
151-
return tmp;
152-
}
153-
function isOrdinaryObject(x) {
154-
return !!x && x.constructor === Object;
155-
}
156146

157-
export const getConfig = jest.fn().mockImplementation(() => Promise.resolve(getDefaults(configSchema)));
147+
export const getConfig = jest.fn().mockImplementation(() => Promise.resolve(getDefaultsFromConfigSchema(configSchema)));
158148

159-
export const useConfig = jest.fn().mockImplementation(() => getDefaults(configSchema));
149+
export const useConfig = jest.fn().mockImplementation(() => getDefaultsFromConfigSchema(configSchema));
160150

161151
export function defineConfigSchema(moduleName, schema) {
162152
configSchema = schema;

packages/framework/esm-utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './age-helpers';
22
export * from './omrs-dates';
33
export * from './shallowEqual';
44
export * from './storage';
5+
export * from './test-helpers';
56
export * from './translate';
67
export * from './version';
78
export * from './retry';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** @module @category Utility */
2+
3+
/**
4+
* Given a config schema, this returns an object like is returned by `useConfig`
5+
* with all default values.
6+
*
7+
* This should be used in tests and not in production code.
8+
*
9+
* If all you need is the default values in your tests, these are returned by
10+
* default from the `useConfig`/`getConfig` mock. This function is useful if you
11+
* need to override some of the default values.
12+
*/
13+
export function getDefaultsFromConfigSchema(schema) {
14+
let tmp = {};
15+
for (let k of Object.keys(schema)) {
16+
if (schema[k].hasOwnProperty('_default')) {
17+
tmp[k] = schema[k]._default;
18+
} else if (k.startsWith('_')) {
19+
continue;
20+
} else if (isOrdinaryObject(schema[k])) {
21+
tmp[k] = getDefaultsFromConfigSchema(schema[k]);
22+
} else {
23+
tmp[k] = schema[k];
24+
}
25+
}
26+
return tmp;
27+
}
28+
29+
function isOrdinaryObject(x) {
30+
return !!x && x.constructor === Object;
31+
}

0 commit comments

Comments
 (0)