Skip to content

Commit

Permalink
patch(vest): Use the same test mutation function everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed May 21, 2022
1 parent 8b83eff commit 33cd6d9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
14 changes: 5 additions & 9 deletions packages/vest/src/core/test/lib/useTestAtCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,16 @@ export function useTestAtCursor(newTestObject: VestTest): VestTest {
}

function removeAllNextTestsInIsolate() {
const [testObjects, setTestObjects] = useTestObjects();

const prevTests = testObjects.prev;
const current = nestedArray.getCurrent(prevTests, useCurrentPath());
const cursorAt = useCursor().current();

current.splice(cursorAt);
// We actually don't mind mutating the state directly (as can be seen above). There is no harm in it
// since we're only touching the "prev" state. The reason we still use the setter function is
// to prevent future headaches if we ever do need to rely on prev-state immutability.
setTestObjects(({ current }) => ({
prev: prevTests,
current,
}));

useSetTests(current => {
current.splice(cursorAt);
return current;
});
}

export function useSetTestAtCursor(testObject: VestTest): void {
Expand Down
15 changes: 4 additions & 11 deletions packages/vest/testUtils/testObjects.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import VestTest from 'VestTest';
import { useTestObjects } from 'stateHooks';
import { useSetTests } from 'stateHooks';

export function emptyTestObjects(): void {
const [, setTestObjects] = useTestObjects();
setTestObjects(({ prev }) => ({ prev, current: [] }));
useSetTests(() => []);
}

export function setTestObjects(...args: VestTest[]): void {
const [, setTestObjects] = useTestObjects();
setTestObjects(({ prev }) => ({ prev, current: [...args] }));
useSetTests(() => [...args]);
}

export function addTestObject(addedTests: VestTest[] | VestTest): void {
const [, setTestObjects] = useTestObjects();

setTestObjects(({ prev, current }) => ({
prev,
current: current.concat(addedTests),
}));
useSetTests(tests => tests.concat(addedTests));
}

1 comment on commit 33cd6d9

@vercel
Copy link

@vercel vercel bot commented on 33cd6d9 May 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next-ealush.vercel.app
vest-next-git-latest-ealush.vercel.app
vest-next.vercel.app
vest-website.vercel.app

Please sign in to comment.