leumas-universal-preset
Version:
A microservice to declare universal functions packed with reusable props via preset mappings.
63 lines (56 loc) • 2.15 kB
JavaScript
const { expect } = require('chai');
const { withPreset, mappings } = require('../index');
// Simulated executeCell function.
const executeCell = async (
gridId,
layerName,
cellId,
environment = 'development',
ownerid = null,
ownerToken = null
) => {
return { gridId, layerName, cellId, environment, ownerid, ownerToken };
};
describe('executeCell withPreset Tests', () => {
const presetValues = {
environment: 'production',
ownerid: 'defaultOwner',
ownerToken: 'defaultToken'
};
// Use the default mapping from our registry.
const executeCellMapping = mappings.executeCell.default;
const executeCellProd = withPreset(executeCell, presetValues, { mapping: executeCellMapping });
it('should apply preset values when not provided at call time', async () => {
const result = await executeCellProd('gridTest', 'layerTest', 'cellTest');
expect(result).to.deep.equal({
gridId: 'gridTest',
layerName: 'layerTest',
cellId: 'cellTest',
environment: 'production',
ownerid: 'defaultOwner',
ownerToken: 'defaultToken'
});
});
it('should override preset values if provided at call time', async () => {
// Override the environment parameter to 'staging'.
const result = await executeCellProd('gridTest', 'layerTest', 'cellTest', 'staging');
expect(result).to.deep.equal({
gridId: 'gridTest',
layerName: 'layerTest',
cellId: 'cellTest',
environment: 'staging',
ownerid: 'defaultOwner',
ownerToken: 'defaultToken'
});
});
it('should pass through extra arguments if preset array mode is used', async () => {
// This test is to ensure additional parameters are passed.
// We'll simulate a positional preset array for a similar function.
const fn = (a, b, c, d) => `${a}-${b}-${c}-${d}`;
const presetArray = [1, 2, 3]; // defaults for a, b, c
const wrappedFn = withPreset(fn, presetArray);
// Provide a value for d only.
const result = wrappedFn(undefined, undefined, undefined, 4);
expect(result).to.equal('1-2-3-4');
});
});