Skip to content

Commit

Permalink
patch(vest): Pass draft result to skipWhen conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent e84cd9d commit 0acb24d
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 7 deletions.
13 changes: 8 additions & 5 deletions packages/vest/src/__tests__/integration.stateful-tests.test.ts
Expand Up @@ -112,11 +112,14 @@ describe('more complex', () => {
enforce(data.password).isNotEmpty();
});

skipWhen(suite.get().hasErrors('password'), () => {
test('confirm', 'passwords do not match', () => {
enforce(data.confirm).equals(data.password);
});
});
skipWhen(
draft => draft.hasErrors('password'),
() => {
test('confirm', 'passwords do not match', () => {
enforce(data.confirm).equals(data.password);
});
}
);
});
});

Expand Down
@@ -0,0 +1,87 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`skipWhen Should pass result draft to the functional condition 1`] = `
Object {
"errorCount": 0,
"getErrors": [Function],
"getErrorsByGroup": [Function],
"getWarnings": [Function],
"getWarningsByGroup": [Function],
"groups": Object {},
"hasErrors": [Function],
"hasErrorsByGroup": [Function],
"hasWarnings": [Function],
"hasWarningsByGroup": [Function],
"isValid": [Function],
"suiteName": undefined,
"testCount": 0,
"tests": Object {},
"warnCount": 0,
}
`;

exports[`skipWhen Should pass result draft to the functional condition 2`] = `
Object {
"errorCount": 1,
"getErrors": [Function],
"getErrorsByGroup": [Function],
"getWarnings": [Function],
"getWarningsByGroup": [Function],
"groups": Object {},
"hasErrors": [Function],
"hasErrorsByGroup": [Function],
"hasWarnings": [Function],
"hasWarningsByGroup": [Function],
"isValid": [Function],
"suiteName": undefined,
"testCount": 1,
"tests": Object {
"f1": Object {
"errorCount": 1,
"errors": Array [
"msg",
],
"testCount": 1,
"warnCount": 0,
},
},
"warnCount": 0,
}
`;

exports[`skipWhen Should pass result draft to the functional condition 3`] = `
Object {
"errorCount": 2,
"getErrors": [Function],
"getErrorsByGroup": [Function],
"getWarnings": [Function],
"getWarningsByGroup": [Function],
"groups": Object {},
"hasErrors": [Function],
"hasErrorsByGroup": [Function],
"hasWarnings": [Function],
"hasWarningsByGroup": [Function],
"isValid": [Function],
"suiteName": undefined,
"testCount": 2,
"tests": Object {
"f1": Object {
"errorCount": 1,
"errors": Array [
"msg",
],
"testCount": 1,
"warnCount": 0,
},
"f2": Object {
"errorCount": 1,
"errors": Array [
"msg",
],
"testCount": 1,
"warnCount": 0,
},
},
"warnCount": 0,
}
`;
39 changes: 39 additions & 0 deletions packages/vest/src/hooks/__tests__/skipWhen.test.ts
@@ -1,3 +1,5 @@
import { dummyTest } from '../../../testUtils/testDummy';

import * as vest from 'vest';

describe('skipWhen', () => {
Expand Down Expand Up @@ -33,6 +35,43 @@ describe('skipWhen', () => {
expect(fn).toHaveBeenCalledTimes(4);
});

it('Should pass result draft to the functional condition', () => {
const f = jest.fn();
const control = jest.fn();

vest.create(() => {
vest.skipWhen(draft => {
expect(draft.hasErrors()).toBe(false);
expect(draft).toMatchSnapshot();
control();
return false;
}, f);
dummyTest.failing('f1', 'msg');
vest.skipWhen(draft => {
expect(draft.hasErrors()).toBe(true);
expect(draft.hasErrors('f1')).toBe(true);
expect(draft.hasErrors('f2')).toBe(false);
expect(draft.hasErrors('f3')).toBe(false);
expect(draft).toMatchSnapshot();
control();
return false;
}, f);
dummyTest.failing('f2', 'msg');
vest.skipWhen(draft => {
expect(draft.hasErrors()).toBe(true);
expect(draft.hasErrors('f1')).toBe(true);
expect(draft.hasErrors('f2')).toBe(true);
expect(draft.hasErrors('f3')).toBe(false);
expect(draft).toMatchSnapshot();
control();
return false;
}, f);
dummyTest.failing('f3', 'msg');
})();

expect(control).toHaveBeenCalledTimes(3);
});

it('Should skip tests when the condition is truthy', () => {
const res = suite(true);
expect(res.tests.username.testCount).toBe(0);
Expand Down
13 changes: 11 additions & 2 deletions packages/vest/src/hooks/skipWhen.ts
Expand Up @@ -3,12 +3,21 @@ import optionalFunctionValue from 'optionalFunctionValue';
import { IsolateTypes } from 'IsolateTypes';
import ctx from 'ctx';
import { isolate } from 'isolate';
import { produceDraft, TDraftResult } from 'produceDraft';

export default function skipWhen(
conditional: boolean | ((...args: any[]) => boolean),
conditional: boolean | ((draft: TDraftResult) => boolean),
callback: (...args: any[]) => void
): void {
isolate({ type: IsolateTypes.SKIP_WHEN }, () => {
ctx.run({ skipped: optionalFunctionValue(conditional) }, () => callback());
ctx.run(
{
skipped: optionalFunctionValue(
conditional,
optionalFunctionValue(produceDraft)
),
},
() => callback()
);
});
}

0 comments on commit 0acb24d

Please sign in to comment.