Skip to content

Commit

Permalink
fix: outdated cache in done results
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jul 5, 2020
1 parent efa240c commit 2ff6948
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
6 changes: 5 additions & 1 deletion packages/vest/src/core/produce/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ const done = (state, ...args) => {
return output;
}

const cb = () => callback(produce(state, { draft: true }));
// This won't be cached. Because we do not know where in the
// Lifecycle of our validations this produce will run, the test may be
// outdated and we might even not have a reference for it, so we're spreading
// to skip the cache.
const cb = () => callback(produce({ ...state }, { draft: true }));
const isFinishedTest = fieldName && !hasRemainingTests(state, fieldName);
const isSuiteFinished = !hasRemainingTests(state);
const shouldRunCallback = isFinishedTest || isSuiteFinished;
Expand Down
109 changes: 79 additions & 30 deletions packages/vest/src/core/produce/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,36 +727,85 @@ runSpec(vest => {
});

describe('produce cache (integragion)', () => {
const control = jest.fn();
let draft, state;
const validate = vest.create('cache', () => {
draft = vest.draft();
state = vest.get('cache');
expect(draft).toBe(vest.draft());
expect(state).toBe(vest.get('cache'));
testDummy(vest).failing();
expect(state).not.toBe(vest.get('cache'));
expect(draft).not.toBe(vest.draft());
testDummy(vest).failing();
state = vest.get('cache');
control();
});

it('Should return same result as as long as the state did not change', () => {
let res = validate().done(result => {
expect(result).toBe(vest.get('cache'));
});
expect(control).toHaveBeenCalledTimes(1);
expect(state).toBe(vest.get('cache'));
expect(res).not.toBe(draft);
expect(res).not.toBe(state);
expect(draft).not.toBe(vest.get('cache'));
res = validate().done(result => {
expect(result).toBe(vest.get('cache'));
});
expect(res).not.toBe(draft);
expect(res).not.toBe(state);
expect(control).toHaveBeenCalledTimes(2);
describe('sync', () => {
const control = jest.fn();
let draft, state;
const validate = vest.create('cache', () => {
draft = vest.draft();
state = vest.get('cache');
expect(draft).toBe(vest.draft());
expect(state).toBe(vest.get('cache'));
testDummy(vest).failing();
expect(state).not.toBe(vest.get('cache'));
expect(draft).not.toBe(vest.draft());
testDummy(vest).failing();
state = vest.get('cache');
control();
});

it('Should return same result as as long as the state did not change', () => {
let res = validate().done(result => {
// Done results are not cached.
// See produce / index for more info
expect(result).isDeepCopyOf(vest.get('cache'));
});
expect(control).toHaveBeenCalledTimes(1);
expect(state).toBe(vest.get('cache'));
expect(res).not.toBe(draft);
expect(res).not.toBe(state);
expect(draft).not.toBe(vest.get('cache'));
res = validate().done(result => {
expect(result).isDeepCopyOf(vest.get('cache'));
});
expect(res).not.toBe(draft);
expect(res).not.toBe(state);
expect(control).toHaveBeenCalledTimes(2);
});
});

describe('Async', () => {
let result;
const validate = vest.create('cache-async', () => {
testDummy(vest).passing('field_1');
testDummy(vest).failing('field_2');
testDummy(vest).failingAsync('field_3');
testDummy(vest).failingAsync('field_4', { time: 250 });
});

it('Should produce correct validation result', () => {
result = validate();
const control = jest.fn();
return new Promise(done => {
expect(result.hasErrors('field_1')).toBe(false);
expect(result.hasErrors('field_2')).toBe(true);
expect(result.hasErrors('field_3')).toBe(false);
expect(result.hasErrors('field_4')).toBe(false);

result.done('field_3', res => {
expect(res).isDeepCopyOf(vest.get('cache-async'));
expect(res).not.toBe(result);
expect(res).not.isDeepCopyOf(result);
expect(res.hasErrors('field_1')).toBe(false);
expect(res.hasErrors('field_2')).toBe(true);
expect(res.hasErrors('field_3')).toBe(true);
expect(res.hasErrors('field_4')).toBe(false);
control();
});

result.done(res => {
expect(res.tests.field_4.errorCount).toBe(1);
expect(res).isDeepCopyOf(vest.get('cache-async'));
expect(res).not.toBe(result);
expect(res).not.isDeepCopyOf(result);
expect(res.hasErrors('field_1')).toBe(false);
expect(res.hasErrors('field_2')).toBe(true);
expect(res.hasErrors('field_3')).toBe(true);
expect(res.hasErrors('field_4')).toBe(true);

done();
});
});
});
});
});
});
Expand Down
1 change: 0 additions & 1 deletion packages/vest/src/core/test/lib/VestTest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function VestTest({ suiteId, fieldName, statement, testFn, group }) {
this.groupName = group;
}
}

/**
* @returns {Boolean} Current validity status of a test.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/vest/src/core/test/runAsyncTest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const runAsyncTest = testObject => {
};
const fail = rejectionMessage => {
done(() => {
const state = getSuiteState(suiteId);
testObject.statement =
typeof rejectionMessage === 'string' ? rejectionMessage : statement;
testObject.fail();
Expand Down

0 comments on commit 2ff6948

Please sign in to comment.