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 #18 from openizr/0.0.27
Browse files Browse the repository at this point in the history
Update cache when using setValues only if enabled
  • Loading branch information
matthieujabbour committed Aug 27, 2021
2 parents 7273d60 + 9325e49 commit 43755d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
32 changes: 17 additions & 15 deletions library/src/scripts/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,22 +414,24 @@ export default class Engine {
*/
public setValues(values: FormValues): void {
Object.assign(this.formValues, deepCopy(values));
localforage.setItem(this.cacheKey, {
formValues: this.formValues,
// We remove all functions from fields' options as they can't be stored in IndexedDB.
steps: this.generatedSteps.map((step) => ({
...step,
fields: step.fields.map((field) => ({
...field,
options: Object.keys(field.options).reduce((options, key) => {
if (typeof field.options[key] !== 'function') {
Object.assign(options, { [key]: field.options[key] });
}
return options;
}, {}),
if (this.useCache) {
localforage.setItem(this.cacheKey, {
formValues: this.formValues,
// We remove all functions from fields' options as they can't be stored in IndexedDB.
steps: this.generatedSteps.map((step) => ({
...step,
fields: step.fields.map((field) => ({
...field,
options: Object.keys(field.options).reduce((options, key) => {
if (typeof field.options[key] !== 'function') {
Object.assign(options, { [key]: field.options[key] });
}
return options;
}, {}),
})),
})),
})),
});
});
}
}

/**
Expand Down
14 changes: 13 additions & 1 deletion library/src/scripts/core/__tests__/Engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ describe('core/Engine', () => {
expect(() => engine.createStep('other')).toThrow(new Error('Step "other" does not exist.'));
});

test('getValues & setValues', async () => {
test('getValues & setValues - cache enabled', async () => {
await createEngine({
root: 'test',
steps: { test: { fields: ['test'] } },
Expand Down Expand Up @@ -620,6 +620,18 @@ describe('core/Engine', () => {
});
});

test('setValues - cache disabled', async () => {
await createEngine({
root: 'test',
cache: false,
steps: { test: { fields: [] } },
fields: {},
});
engine.setValues({ test: 'test', other: 'other' });
expect(engine.getValues()).toEqual({ test: 'test', other: 'other' });
expect(localforage.setItem).not.toHaveBeenCalled();
});

test('getStore', async () => {
await createEngine({ root: 'test', steps: { test: { fields: [] } }, fields: {} });
expect(store).toBe(engine.getStore());
Expand Down

0 comments on commit 43755d0

Please sign in to comment.