Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from openizr/0.0.18
Browse files Browse the repository at this point in the history
0.0.18
  • Loading branch information
matthieujabbour committed Jun 28, 2021
2 parents 88eb1d7 + 2bb274d commit b7a32f5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
1 change: 1 addition & 0 deletions library/src/scripts/lib/__mocks__/localforage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export default {
? '{"test": "value"}'
: null)),
setItem: jest.fn(() => Promise.resolve()),
removeItem: jest.fn(() => Promise.resolve()),
};
25 changes: 22 additions & 3 deletions library/src/scripts/plugins/__tests__/valuesLoader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ describe('plugins/valuesLoader', () => {

test('initialization - default options', () => {
valuesLoader({})(engine);
expect(engine.on).toHaveBeenCalledTimes(3);
expect(engine.on).toHaveBeenCalledTimes(4);
expect(engine.on).toHaveBeenNthCalledWith(1, 'userAction', expect.any(Function));
expect(engine.on).toHaveBeenNthCalledWith(2, 'loadNextStep', expect.any(Function));
expect(engine.on).toHaveBeenNthCalledWith(3, 'loadedNextStep', expect.any(Function));
expect(engine.on).toHaveBeenNthCalledWith(4, 'submit', expect.any(Function));
});

test('initialization - custom options', () => {
valuesLoader({ enabled: false })(engine);
expect(engine.on).toHaveBeenCalledTimes(1);
expect(engine.on).toHaveBeenCalledWith('loadedNextStep', expect.any(Function));
expect(engine.on).toHaveBeenCalledTimes(4);
expect(engine.on).toHaveBeenNthCalledWith(1, 'userAction', expect.any(Function));
expect(engine.on).toHaveBeenNthCalledWith(2, 'loadNextStep', expect.any(Function));
expect(engine.on).toHaveBeenNthCalledWith(3, 'loadedNextStep', expect.any(Function));
expect(engine.on).toHaveBeenNthCalledWith(4, 'submit', expect.any(Function));
});

test('userAction hook - null user action', async () => {
Expand Down Expand Up @@ -116,4 +120,19 @@ describe('plugins/valuesLoader', () => {
expect(engine.loadValues).toHaveBeenNthCalledWith(1, { test: 'first' });
expect(engine.loadValues).toHaveBeenNthCalledWith(2, { last: undefined });
});

test('submit hook - error in submission', async () => {
valuesLoader({})(engine);
await engine.trigger('submit', {}, null);
expect(localforage.removeItem).not.toHaveBeenCalled();
});

test('submit hook - successfully submitted', async () => {
valuesLoader({})(engine);
await engine.trigger('submit', {}, {});
await engine.trigger('userAction', {}, {});
expect(localforage.setItem).not.toHaveBeenCalled();
expect(localforage.removeItem).toHaveBeenCalledTimes(1);
expect(localforage.removeItem).toHaveBeenCalledWith('gincko_cache');
});
});
43 changes: 27 additions & 16 deletions library/src/scripts/plugins/valuesLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface Options {
export default function valuesLoader(options: Options): Plugin {
let loadedCache = false;
let timeout: number | null = null;
const enabled = options.enabled !== false;
let enabled = options.enabled !== false;
const autoSubmit = options.autoSubmit === true;
const cacheKey = `gincko_${options.cacheId || 'cache'}`;
const injectValuesTo = options.injectValuesTo || ['Message'];
Expand All @@ -39,28 +39,28 @@ export default function valuesLoader(options: Options): Plugin {
: values
), {}) as FormValues;

if (enabled === true) {
// Stores new user inputs into cache.
engine.on('userAction', (userAction, next) => next(userAction).then((updatedUserAction) => {
// Stores new user inputs into cache.
engine.on('userAction', (userAction, next) => next(userAction).then((updatedUserAction) => {
if (enabled === true) {
if (updatedUserAction !== null) {
Object.assign(formValues, { [updatedUserAction.fieldId]: updatedUserAction.value });
formValues[updatedUserAction.fieldId] = updatedUserAction.value;
window.clearTimeout(timeout as number);
timeout = window.setTimeout(() => {
localforage.setItem(cacheKey, JSON.stringify(formValues));
}, 500);
}
return Promise.resolve(updatedUserAction);
}));
}
return Promise.resolve(updatedUserAction);
}));

// Automatically injects form values in specified fields' options to allow for more dynamic
// behaviours such as using filled values as variables in another step's message field.
engine.on('loadNextStep', (nextStep, next) => next((nextStep === null) ? nextStep : {
...nextStep,
fields: nextStep.fields.map((field: Field) => ((injectValuesTo.includes(field.type))
? { ...field, options: { ...field.options, formValues } }
: field)),
}));
}
// Automatically injects form values in specified fields' options to allow for more dynamic
// behaviours such as using filled values as variables in another step's message field.
engine.on('loadNextStep', (nextStep, next) => next((nextStep === null) ? nextStep : {
...nextStep,
fields: nextStep.fields.map((field: Field) => ((injectValuesTo.includes(field.type))
? { ...field, options: { ...field.options, formValues } }
: field)),
}));

// Loads default values defined in configuration's fields, as well as values already filled.
engine.on('loadedNextStep', (nextStep, next) => {
Expand Down Expand Up @@ -95,5 +95,16 @@ export default function valuesLoader(options: Options): Plugin {
return next(nextStep);
});
});

// Empties values stored in cache if needed, and synchronizes values with engine ones on submit.
engine.on('submit', (submitValues, next) => next(submitValues).then((updatedFormValues) => {
if (updatedFormValues !== null && enabled === true) {
window.clearTimeout(timeout as number);
localforage.removeItem(cacheKey);
Object.assign(formValues, updatedFormValues);
enabled = false;
}
return Promise.resolve(updatedFormValues);
}));
};
}
2 changes: 1 addition & 1 deletion library/src/scripts/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type Configuration = PropTypes.InferProps<{
[x: string]: PropTypes.InferProps<{
fields: PropTypes.Validator<string[]>;
submit: PropTypes.Requireable<boolean>;
nextStep: PropTypes.Requireable<string | ((...args: Json[]) => string)>;
nextStep: PropTypes.Requireable<string | ((...args: Json[]) => string | null)>;
}>;
}>;
fields: PropTypes.Validator<{
Expand Down

0 comments on commit b7a32f5

Please sign in to comment.