Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Provide function for obtaining default config tree in test code #863

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading