Skip to content

Commit

Permalink
(feat) Provide function for obtaining default config tree in test code
Browse files Browse the repository at this point in the history
  • Loading branch information
brandones committed Dec 24, 2023
1 parent 4a0e8ab commit 7ca052e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
30 changes: 30 additions & 0 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
- [age](API.md#age)
- [canAccessStorage](API.md#canaccessstorage)
- [daysIntoYear](API.md#daysintoyear)
- [getDefaultsFromConfigSchema](API.md#getdefaultsfromconfigschema)
- [isSameDay](API.md#issameday)
- [isVersionSatisfied](API.md#isversionsatisfied)
- [retry](API.md#retry)
Expand Down Expand Up @@ -4841,6 +4842,35 @@ The number of days.

___

### getDefaultsFromConfigSchema

**getDefaultsFromConfigSchema**(`schema`): `Object`

Given a config schema, this returns an object like is returned by `useConfig`
with all default values.

This should be used in tests and not in production code.

If all you need is the default values in your tests, these are returned by
default from the `useConfig`/`getConfig` mock. This function is useful if you
need to override some of the default values.

#### Parameters

| Name | Type |
| :------ | :------ |
| `schema` | `any` |

#### Returns

`Object`

#### Defined in

[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)

___

### isSameDay

**isSameDay**(`firstDate`, `secondDate`): `boolean`
Expand Down
32 changes: 11 additions & 21 deletions packages/framework/esm-framework/mock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { createStore, type StoreApi } from 'zustand';
import { NEVER, of } from 'rxjs';
import { interpolateUrl } from '@openmrs/esm-config';
import { type SessionStore } from '@openmrs/esm-api';
export { parseDate, formatDate, formatDatetime, formatTime, age } from '@openmrs/esm-utils';
import { getDefaultsFromConfigSchema } from '@openmrs/esm-utils';
export {
getDefaultsFromConfigSchema,
parseDate,
formatDate,
formatDatetime,
formatTime,
age,
} from '@openmrs/esm-utils';
export { interpolateString, interpolateUrl, validators, validator } from '@openmrs/esm-config';

window.i18next = { ...window.i18next, language: 'en' };
Expand Down Expand Up @@ -135,28 +143,10 @@ export enum Type {
}

let configSchema = {};
function getDefaults(schema) {
let tmp = {};
for (let k of Object.keys(schema)) {
if (schema[k].hasOwnProperty('_default')) {
tmp[k] = schema[k]._default;
} else if (k.startsWith('_')) {
continue;
} else if (isOrdinaryObject(schema[k])) {
tmp[k] = getDefaults(schema[k]);
} else {
tmp[k] = schema[k];
}
}
return tmp;
}
function isOrdinaryObject(x) {
return !!x && x.constructor === Object;
}

export const getConfig = jest.fn().mockImplementation(() => Promise.resolve(getDefaults(configSchema)));
export const getConfig = jest.fn().mockImplementation(() => Promise.resolve(getDefaultsFromConfigSchema(configSchema)));

export const useConfig = jest.fn().mockImplementation(() => getDefaults(configSchema));
export const useConfig = jest.fn().mockImplementation(() => getDefaultsFromConfigSchema(configSchema));

export function defineConfigSchema(moduleName, schema) {
configSchema = schema;
Expand Down
1 change: 1 addition & 0 deletions packages/framework/esm-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './age-helpers';
export * from './omrs-dates';
export * from './shallowEqual';
export * from './storage';
export * from './test-helpers';
export * from './translate';
export * from './version';
export * from './retry';
31 changes: 31 additions & 0 deletions packages/framework/esm-utils/src/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** @module @category Utility */

/**
* Given a config schema, this returns an object like is returned by `useConfig`
* with all default values.
*
* This should be used in tests and not in production code.
*
* If all you need is the default values in your tests, these are returned by
* default from the `useConfig`/`getConfig` mock. This function is useful if you
* need to override some of the default values.
*/
export function getDefaultsFromConfigSchema(schema) {
let tmp = {};
for (let k of Object.keys(schema)) {
if (schema[k].hasOwnProperty('_default')) {
tmp[k] = schema[k]._default;
} else if (k.startsWith('_')) {
continue;
} else if (isOrdinaryObject(schema[k])) {
tmp[k] = getDefaultsFromConfigSchema(schema[k]);
} else {
tmp[k] = schema[k];
}
}
return tmp;
}

function isOrdinaryObject(x) {
return !!x && x.constructor === Object;
}

0 comments on commit 7ca052e

Please sign in to comment.