diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /spec/frontend_integration/test_helpers/utils/obj.js | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) | |
download | gitlab-ce-6e4e1050d9dba2b7b2523fdd1768823ab85feef4.tar.gz |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec/frontend_integration/test_helpers/utils/obj.js')
-rw-r--r-- | spec/frontend_integration/test_helpers/utils/obj.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/spec/frontend_integration/test_helpers/utils/obj.js b/spec/frontend_integration/test_helpers/utils/obj.js new file mode 100644 index 00000000000..6c301798489 --- /dev/null +++ b/spec/frontend_integration/test_helpers/utils/obj.js @@ -0,0 +1,36 @@ +import { has, mapKeys, pick } from 'lodash'; + +/** + * This method is used to type-safely set values on the given object + * + * @template T + * @returns {T} A shallow copy of `obj`, with the values from `values` + * @throws {Error} If `values` contains a key that isn't already on `obj` + * @param {T} source + * @param {Object} values + */ +export const withValues = (source, values) => + Object.entries(values).reduce( + (acc, [key, value]) => { + if (!has(acc, key)) { + throw new Error( + `[mock_server] Cannot write property that does not exist on object '${key}'`, + ); + } + + return { + ...acc, + [key]: value, + }; + }, + { ...source }, + ); + +/** + * This method returns a subset of the given object and maps the key names based on the + * given `keys`. + * + * @param {Object} obj The source object. + * @param {Object} map The object which contains the keys to use and mapped key names. + */ +export const withKeys = (obj, map) => mapKeys(pick(obj, Object.keys(map)), (val, key) => map[key]); |