Files
nebula.js/apis/enigma-mocker/src/mocks/get-object-mock.js
Tobias Åström 3dacac587b feat: sheet embed support (#1013)
* feat: add basic sheet rendering support

* chore: add missing file

* fix: correct bg colors for none support

* chore: fix test that relied on dark bg

* chore: fix ref

* chore: api spec update

* chore: add todo comments

* chore: use memo

* chore: a bit less verbose

* chore: list

* chore: cleaning

* chore: add rendering test

* chore: enable rendering test

* chore: settings

* chore: settings

* chore: disable rendering tests

* chore: revert test tests
2022-12-12 13:54:04 +01:00

133 lines
3.5 KiB
JavaScript

import { getPropValue, getPropFn } from '../prop';
/**
* Properties on `getObject()` operating synchronously.
* @ignore
* @type Array
*/
const PROPS_SYNC = [
'addListener',
'emit',
'listeners',
'on',
'once',
'removeAllListeners',
'removeListener',
'setMaxListerners',
];
/**
* Is property operating asynchrously.
* @param {string} name Property name.
* @ignore
* @returns `true` if property is operating asynchrously, otherwise `false`.
*/
function isPropAsync(name) {
return !PROPS_SYNC.includes(name);
}
/**
* Get `qId` for visualization.
* @param {object} genericObject Generic object describing behaviour of mock
* @ignore
* @returns The `qId`, undefined if not present
*/
function getQId(genericObject) {
const layout = getPropValue(genericObject.getLayout);
return layout.qInfo && layout.qInfo.qId;
}
/**
* Create a mock of a generic object. Mandatory properties are added, functions returns async values where applicable etc.
* @param {object} genericObject Generic object describing behaviour of mock
* @param {EnigmaMockerOptions} options Options.
* @ignore
* @returns The mocked object
*/
function createMock(genericObject, options) {
let qId = getQId(genericObject);
const { delay } = options;
const { id, session, ...props } = genericObject;
if (id && qId && id !== qId) {
throw new Error(`Generic object has multiple IDs, qInfo.qId: ${qId}, id: ${id}`);
}
qId = qId || id || `object - ${+Date.now()}`;
const mock = {
id: qId,
session: getPropValue(session, { defaultValue: true }),
on: () => {},
once: () => {},
...Object.entries(props).reduce(
(fns, [name, value]) => ({
...fns,
[name]: getPropFn(value, { async: isPropAsync(name), delay }),
}),
{}
),
genericType: genericObject.type,
};
return { [qId]: mock };
}
/**
* Create mocked objects from list of generic objects.
* @param {Array<object>} genericObjects Generic objects describing behaviour of mock
* @param {EnigmaMockerOptions} options options
* @ignore
* @returns Object with mocks where key is `qId` and value is the mocked object.
*/
function createMocks(genericObjects, options) {
return genericObjects.reduce(
(mocks, genericObject) => ({
...mocks,
...createMock(genericObject, options),
}),
{}
);
}
/**
* Validates if mandatory information is available.
* @param {object} genericObject Generic object to validate
* @ignore
* @throws {}
* <ul>
* <li>{Error} If getLayout is missing</li>
* <li>{Error} If getLayout.qInfo.qId is missing</li>
* </ul>
*/
function validate(genericObject) {
if (!genericObject.getLayout) {
throw new Error('Generic object is missing "getLayout"');
}
const qId = getQId(genericObject);
if (!qId) {
throw new Error('Generic object is missing "qId" for path "getLayout().qInfo.qId"');
}
}
/**
* Creates mock of `getObject(id)` based on an array of generic objects.
* @param {Array<object>} genericObjects Generic objects.
* @param {EnigmaMockerOptions} options Options.
* @ignore
* @returns Function to retrieve the mocked generic object with the corresponding id.
*/
function GetObjectMock(genericObjects = [], options = {}) {
if (!Array.isArray(genericObjects) || genericObjects.length === 0) {
return () => {
throw new Error('No "genericObjects" specified');
};
}
genericObjects.forEach(validate);
const mocks = createMocks(genericObjects, options);
return async (id) => Promise.resolve(mocks[id]);
}
export default GetObjectMock;