Skip to content

Commit

Permalink
patch: add cursor util
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent 9380c09 commit fd049e8
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 26 deletions.
44 changes: 44 additions & 0 deletions packages/shared/src/cursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import asArray from 'asArray';
import last from 'last';

export function createCursor() {
const storage: { cursor: number[] } = {
cursor: [],
};

function addLevel(): void {
storage.cursor.push(0);
}

function removeLevel(): void {
storage.cursor.pop();
}

function cursorAt(): number {
return last(storage.cursor);
}

function getCursor(): number[] {
return asArray(storage.cursor);
}

function next(): number {
storage.cursor[storage.cursor.length - 1]++;
return last(storage.cursor);
}

function reset(): void {
storage.cursor = [0];
}

reset();

return {
addLevel,
cursorAt,
getCursor,
next,
removeLevel,
reset,
};
}
5 changes: 3 additions & 2 deletions packages/vest/src/core/ctx/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assign from 'assign';
import { createContext } from 'context';
import { createCursor } from 'cursor';

import VestTest from 'VestTest';
import type { TStateRef } from 'createStateRef';
Expand All @@ -10,7 +11,7 @@ export default createContext<CTXType>((ctxRef, parentContext) =>
: assign(
{},
{
cursorAt: [0],
testCursor: createCursor(),
exclusion: {
tests: {},
groups: {},
Expand All @@ -21,7 +22,7 @@ export default createContext<CTXType>((ctxRef, parentContext) =>
);

type CTXType = {
cursorAt: number[];
testCursor: ReturnType<typeof createCursor>;
stateRef?: TStateRef;
exclusion: {
tests: Record<string, boolean>;
Expand Down
15 changes: 0 additions & 15 deletions packages/vest/src/core/ctx/cursorAt.ts

This file was deleted.

21 changes: 21 additions & 0 deletions packages/vest/src/core/ctx/testCursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ctx from 'ctx';

export function useTestCursorAt(): number {
const context = ctx.useX();
return context.testCursor.cursorAt();
}

export function moveTestCursorForward(): number {
const context = ctx.useX();
return context.testCursor.next();
}

export function addTestCursorLevel(): void {
const context = ctx.useX();
context.testCursor.addLevel();
}

export function removeTestCursorLevel(): void {
const context = ctx.useX();
context.testCursor.removeLevel();
}
6 changes: 3 additions & 3 deletions packages/vest/src/core/test/lib/registerPrevRunTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import isPromise from 'isPromise';

import VestTest from 'VestTest';
import cancelOverriddenPendingTest from 'cancelOverriddenPendingTest';
import { moveCursorAtForward } from 'cursorAt';
import { isExcluded } from 'exclusive';
import registerTest from 'registerTest';
import runAsyncTest from 'runAsyncTest';
import { moveTestCursorForward } from 'testCursor';
import { useTestAtCursor, useSetTestAtCursor } from 'useTestAtCursor';

export default function registerPrevRunTest(testObject: VestTest): VestTest {
const prevRunTest = useTestAtCursor(testObject);
if (isExcluded(testObject)) {
testObject.skip();
moveCursorAtForward();
moveTestCursorForward();
return prevRunTest;
}

cancelOverriddenPendingTest(prevRunTest, testObject);

useSetTestAtCursor(testObject);
moveCursorAtForward();
moveTestCursorForward();

registerTestObjectByTier(testObject);

Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/core/test/lib/useTestAtCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import removeElementFromArray from 'removeElementFromArray';
import { throwErrorDeferred } from 'throwError';

import VestTest from 'VestTest';
import { useCursorAt } from 'cursorAt';
import isSameProfileTest from 'isSameProfileTest';
import { usePrevTestObjects, useTestObjects } from 'stateHooks';
import { useTestCursorAt } from 'testCursor';

export function useTestAtCursor(newTestObject: VestTest): VestTest {
const [, setPrevTestObjects] = usePrevTestObjects();
Expand Down Expand Up @@ -39,7 +39,7 @@ export function useTestAtCursor(newTestObject: VestTest): VestTest {
}

export function useSetTestAtCursor(testObject: VestTest): void {
const cursorAt = useCursorAt();
const cursorAt = useTestCursorAt();
const [testObjects, setTestObjects] = useTestObjects();

if (testObject === testObjects[cursorAt]) {
Expand All @@ -54,7 +54,7 @@ export function useSetTestAtCursor(testObject: VestTest): void {
}

function useGetTestAtCursor(): VestTest {
const cursorAt = useCursorAt();
const cursorAt = useTestCursorAt();
const [prevTestObjects] = usePrevTestObjects();

return prevTestObjects[cursorAt];
Expand Down
4 changes: 2 additions & 2 deletions packages/vest/src/core/test/test.memo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import createCache from 'cache';
import { isNull } from 'isNull';

import VestTest, { TTestFn } from 'VestTest';
import { useCursorAt } from 'cursorAt';
import registerPrevRunTest from 'registerPrevRunTest';
import { useSuiteId } from 'stateHooks';
import type { TTestBase } from 'test';
import { useTestCursorAt } from 'testCursor';
// eslint-disable-next-line max-lines-per-function
export default function bindTestMemo(test: TTestBase): {
(fieldName: string, test: TTestFn, deps: unknown[]): VestTest;
Expand Down Expand Up @@ -35,7 +35,7 @@ export default function bindTestMemo(test: TTestBase): {
| [test: TTestFn, deps: unknown[]]
): VestTest {
const [suiteId] = useSuiteId();
const cursorAt = useCursorAt();
const cursorAt = useTestCursorAt();

const [deps, testFn, msg] = args.reverse() as [any[], TTestFn, string];

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd049e8

Please sign in to comment.