-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Provide function for obtaining default config tree in test code
- Loading branch information
Showing
4 changed files
with
73 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |