Skip to content

Commit

Permalink
feat(vest): Make the state rely on execution order. Make skipWhen set…
Browse files Browse the repository at this point in the history
… skipped context.
  • Loading branch information
ealush committed Nov 10, 2021
1 parent 2b902cb commit 4032bd8
Show file tree
Hide file tree
Showing 38 changed files with 238 additions and 136 deletions.
Empty file added .vscode/launch.json
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ Object {
"isValid": [Function],
"testCount": 3,
"tests": Object {
"confirm": Object {
"errorCount": 0,
"testCount": 0,
"warnCount": 0,
},
"password": Object {
"errorCount": 1,
"errors": Array [
Expand Down Expand Up @@ -263,6 +268,11 @@ Object {
"isValid": [Function],
"testCount": 3,
"tests": Object {
"confirm": Object {
"errorCount": 0,
"testCount": 0,
"warnCount": 0,
},
"password": Object {
"errorCount": 1,
"errors": Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ describe('more complex', () => {
expect(suite.get()).toMatchSnapshot();

suite(data, 'confirm');
expect(suite.get().tests.confirm).toMatchInlineSnapshot(`undefined`);
expect(suite.get().tests.confirm).toMatchInlineSnapshot(`
Object {
"errorCount": 0,
"testCount": 0,
"warnCount": 0,
}
`);
expect(suite.get()).toMatchSnapshot();
expect(suite.get().hasErrors('password')).toBe(true);
expect(suite.get().hasErrors('confirm')).toBe(false);
Expand Down
1 change: 1 addition & 0 deletions packages/vest/src/core/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ type CTXType = {
};
currentTest?: VestTest;
groupName?: string;
skipped?: boolean;
};
3 changes: 2 additions & 1 deletion packages/vest/src/core/state/createStateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default function createStateRef(
fieldCallbacks: {},
doneCallbacks: [],
})),
testObjects: state.registerStateKey<VestTest[]>(() => []),
testsOrdered: state.registerStateKey<VestTest[]>(() => []),
testsOrderedCursor: state.registerStateKey<number>(() => 0),
};
}

Expand Down
53 changes: 50 additions & 3 deletions packages/vest/src/core/state/stateHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export function useTestCallbacks(): TStateHandlerReturn<{
}> {
return useStateRef().testCallbacks();
}
export function useTestObjects(): TStateHandlerReturn<VestTest[]> {
return useStateRef().testObjects();
}
export function useSkippedTests(): TStateHandlerReturn<VestTest[]> {
return useStateRef().skippedTests();
}
Expand All @@ -38,3 +35,53 @@ export function useStateRef(): Exclude<TStateRef, void> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return ctx.useX().stateRef!; // I should revisit this
}

export function useTestsOrdered(): TStateHandlerReturn<VestTest[]> {
return useStateRef().testsOrdered();
}

export function useCursorAt(): TStateHandlerReturn<number> {
return useStateRef().testsOrderedCursor();
}

export function useTestAtCursor(initialValue: VestTest): VestTest {
const [cursorAt] = useCursorAt();
const [testsOrder, setTestsOrder] = useTestsOrdered();

if (!testsOrder[cursorAt]) {
setTestsOrder((testsOrder: VestTest[]) => {
const newTestsOrder = testsOrder.slice(0);
newTestsOrder[cursorAt] = initialValue;
return newTestsOrder;
});
}

return testsOrder[cursorAt] ?? initialValue;
}

export function useSetTestAtCursor(testObject: VestTest): void {
const [cursorAt] = useCursorAt();
const [testsOrder, setTestsOrder] = useTestsOrdered();

if (testObject === testsOrder[cursorAt]) {
return;
}

setTestsOrder((testsOrder: VestTest[]) => {
const newTestsOrder = testsOrder.slice(0);
newTestsOrder[cursorAt] = testObject;
return newTestsOrder;
});
}

export function useSetNextCursorAt(): void {
const [, setCursorAt] = useCursorAt();

setCursorAt((cursorAt: number) => cursorAt + 1);
}

export function useRefreshTestObjects(): void {
const [, setTestsOrder] = useTestsOrdered();

setTestsOrder(testsOrdered => testsOrdered.slice(0));
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Object {
"skippedTests": [Function],
"suiteId": [Function],
"testCallbacks": [Function],
"testObjects": [Function],
"testsOrdered": [Function],
"testsOrderedCursor": [Function],
},
"type": "suiteSubscribeInit",
}
Expand Down
15 changes: 5 additions & 10 deletions packages/vest/src/core/suite/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import createStateRef, { TStateRef } from 'createStateRef';
import context from 'ctx';
import { IVestResult, produceFullResult } from 'produce';
import { produceDraft, TDraftResult } from 'produceDraft';
import {
usePending,
useTestObjects,
useCarryOverTests,
useLagging,
} from 'stateHooks';
import { usePending, useLagging, useTestsOrdered } from 'stateHooks';

export default function create<T extends (...args: any[]) => void>(
suiteCallback: T
Expand Down Expand Up @@ -55,12 +50,12 @@ export default function create<T extends (...args: any[]) => void>(

const suite: IVestSuite = assign(
context.bind({ stateRef }, (...args: unknown[]) => {
const [previousTestObjects] = useTestObjects();
const [, setCarryOverTests] = useCarryOverTests();
const [prevTestsOrdered, testsOrdered] = useTestsOrdered();

const [pending] = usePending();
const [prevLagging, setLagging] = useLagging();
state.reset();
setCarryOverTests(() => previousTestObjects);
testsOrdered(() => prevTestsOrdered);

// Move all the active pending tests to the lagging array
setLagging(pending.concat(prevLagging));
Expand All @@ -75,7 +70,7 @@ export default function create<T extends (...args: any[]) => void>(
{
get: context.bind({ stateRef }, produceDraft),
remove: context.bind({ stateRef }, name => {
const [testObjects] = useTestObjects();
const [testObjects] = useTestsOrdered();

// We're mutating the array in `cancel`, so we have to first copy it.
asArray(testObjects).forEach(testObject => {
Expand Down
5 changes: 5 additions & 0 deletions packages/vest/src/core/test/VestTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class VestTest {
failed = false;
isWarning = false;
canceled = false;
skipped = false;

constructor(
fieldName: string,
Expand Down Expand Up @@ -59,6 +60,10 @@ export default class VestTest {
this.isWarning = true;
}

skip(): void {
this.skipped = true;
}

cancel(): void {
this.canceled = true;
removePending(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Object {
"id": "8",
"isWarning": false,
"message": "some message string",
"skipped": false,
"testFn": [MockFunction],
},
],
Expand All @@ -25,7 +26,8 @@ Object {
"field_1": Array [],
},
},
"testObjects": Array [],
"testsOrdered": Array [],
"testsOrderedCursor": 0,
}
`;

Expand All @@ -43,6 +45,7 @@ Object {
"id": "0",
"isWarning": false,
"message": "some message string",
"skipped": false,
"testFn": [MockFunction],
},
],
Expand All @@ -54,6 +57,7 @@ Object {
"field_1": Array [],
},
},
"testObjects": Array [],
"testsOrdered": Array [],
"testsOrderedCursor": 0,
}
`;
1 change: 0 additions & 1 deletion packages/vest/src/core/test/__tests__/memo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ describe('test.memo', () => {

it('Should produce correct validation result', () => {
const cachedRes = validate(1);
expect(cachedRes).isDeepCopyOf(res);
expect(res.hasErrors('field_1')).toBe(true);
expect(res.hasErrors('field_2')).toBe(true);
expect(res.hasErrors('field_3')).toBe(false);
Expand Down
43 changes: 19 additions & 24 deletions packages/vest/src/core/test/lib/__tests__/VestTest.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import runCreateRef from '../../../../../testUtils/runCreateRef';
import * as vest from 'vest';
import wait from 'wait';

import VestTest from 'VestTest';
import addTestToState from 'addTestToState';
import context from 'ctx';
import { setPending } from 'pending';
import { usePending, useTestObjects, useLagging } from 'stateHooks';
import { usePending, useLagging } from 'stateHooks';

const fieldName = 'unicycle';
const message = 'I am Root.';

let stateRef;

describe('VestTest', () => {
let testObject;

Expand Down Expand Up @@ -76,27 +73,25 @@ describe('VestTest', () => {
});

describe('testObject.cancel', () => {
const getCtx = () => ({ stateRef });

beforeEach(() => {
stateRef = runCreateRef();

context.run({ stateRef }, () => {
addTestToState(testObject);
it('Should remove a testObject from the state', () => {
return new Promise<void>(done => {
let testObject: VestTest;
const suite = vest.create(() => {
testObject = vest.test('f1', async () => {
await wait(100);
});
vest.test('f2', async () => {
await wait(100);
});
testObject.cancel();
});
suite();
expect(suite.get().tests.f1).toBeUndefined();
expect(suite.get().tests.f2).toBeDefined();
done();
});
});

it.withContext(
'Should remove a testObject from the state',
() => {
const [testObjects] = useTestObjects();
expect(testObjects).toEqual(expect.arrayContaining([testObject]));
testObject.cancel();
expect(testObjects).toEqual(expect.not.arrayContaining([testObject]));
},
getCtx
);

it.withContext('Should remove a testObject from the pending state', () => {
setPending(testObject);
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ VestTest {
"id": "0",
"isWarning": false,
"message": "I am Root.",
"skipped": false,
"testFn": [MockFunction],
}
`;
Expand All @@ -20,6 +21,7 @@ VestTest {
"id": "102",
"isWarning": true,
"message": "I am Root.",
"skipped": false,
"testFn": [MockFunction],
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Object {
"doneCallbacks": Array [],
"fieldCallbacks": Object {},
},
"testObjects": Array [],
"testsOrdered": Array [],
"testsOrderedCursor": 0,
}
`;

Expand All @@ -28,7 +29,8 @@ Object {
"doneCallbacks": Array [],
"fieldCallbacks": Object {},
},
"testObjects": Array [],
"testsOrdered": Array [],
"testsOrderedCursor": 0,
}
`;

Expand All @@ -46,6 +48,7 @@ Object {
"id": "4",
"isWarning": false,
"message": "Some statement string",
"skipped": false,
"testFn": [MockFunction],
},
],
Expand All @@ -55,7 +58,8 @@ Object {
"doneCallbacks": Array [],
"fieldCallbacks": Object {},
},
"testObjects": Array [],
"testsOrdered": Array [],
"testsOrderedCursor": 0,
}
`;

Expand All @@ -70,6 +74,7 @@ Object {
"id": "12",
"isWarning": false,
"message": "Some statement string",
"skipped": false,
"testFn": [MockFunction],
},
VestTest {
Expand All @@ -79,6 +84,7 @@ Object {
"id": "11",
"isWarning": false,
"message": "Some statement string",
"skipped": false,
"testFn": [MockFunction],
},
],
Expand All @@ -92,6 +98,7 @@ Object {
"id": "15",
"isWarning": false,
"message": "failure message",
"skipped": false,
"testFn": [MockFunction],
},
],
Expand All @@ -101,6 +108,7 @@ Object {
"doneCallbacks": Array [],
"fieldCallbacks": Object {},
},
"testObjects": Array [],
"testsOrdered": Array [],
"testsOrderedCursor": 0,
}
`;
10 changes: 0 additions & 10 deletions packages/vest/src/core/test/lib/addTestToState.ts

This file was deleted.

Loading

0 comments on commit 4032bd8

Please sign in to comment.