Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch(vest): Set history root when exiting the root node #1153

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vest/src/core/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ export function useResetSuite() {
}

export function useLoadSuite(rootNode: TIsolateSuite): void {
VestRuntime.useLoadRootNode(rootNode);
VestRuntime.useSetHistoryRoot(rootNode);
useExpireSuiteResultCache();
}
6 changes: 5 additions & 1 deletion packages/vest/src/core/test/test.memo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@
return cache(dependencies, cacheAction);
}

VestRuntime.addNodeToHistory(cachedValue);
// FIXME(@ealush 2024-08-12): This is some kind of a hack. Instead organically letting Vest set the next

Check warning on line 69 in packages/vest/src/core/test/test.memo.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'fixme' comment: 'FIXME(@*** 2024-08-12): This is some...'
// child of the isolate, we're forcing it from the outside.
// Instead, an ideal solution would probably be to have test.memo be its own isolate
// that just injects a historic output from a previous test run.
VestRuntime.useSetNextIsolateChild(cachedValue);

return cachedValue;
}
Expand Down
12 changes: 6 additions & 6 deletions packages/vest/src/hooks/__tests__/mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ describe('mode', () => {
it('Should follow the same behavior as if it was not nested', () => {
const suite = create(() => {
group('group_1', () => {
dummyTest.failing('field_1', 'first-of-field_1');
dummyTest.failing('field_1', 'second-of-field_1');
dummyTest.failing('field_2', 'first-of-field_2');
dummyTest.failing('field_2', 'second-of-field_2');
dummyTest.failing('field_3', 'first-of-field_3');
dummyTest.failing('field_3', 'second-of-field_3');
Vest.test('field_1', 'first-of-field_1', () => false);
Vest.test('field_1', 'second-of-field_1', () => false);
Vest.test('field_2', 'first-of-field_2', () => false);
Vest.test('field_2', 'second-of-field_2', () => false);
Vest.test('field_3', 'first-of-field_3', () => false);
Vest.test('field_3', 'second-of-field_3', () => false);
});
});
expect(suite.get().testCount).toBe(0); // sanity
Expand Down
14 changes: 13 additions & 1 deletion packages/vestjs-runtime/src/Isolate/Isolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type UsedFeaturesOnly<P extends IsolatePayload> = Pick<
>;

export class Isolate {
// eslint-disable-next-line max-statements
static create<Payload extends IsolatePayload>(
type: string,
callback: CB,
Expand All @@ -48,14 +49,25 @@ export class Isolate {

const shouldRunNew = Object.is(nextIsolateChild, newCreatedNode);

VestRuntime.addNodeToHistory(nextIsolateChild);
if (parent) {
// We are within an isolate context. This means that
// we need to set the new node to be the child of this parent node.
VestRuntime.useSetNextIsolateChild(nextIsolateChild);
}

const output = shouldRunNew
? useRunAsNew(localHistoryNode, newCreatedNode, callback)
: nextIsolateChild.output;

IsolateMutator.saveOutput(nextIsolateChild, output);

if (!parent) {
// We're exiting the node, and there is no parent. This means
// that we're at the top level and this node should be set
// as the new root of the history tree.
VestRuntime.useSetHistoryRoot(nextIsolateChild);
}

return nextIsolateChild as TIsolate<Payload>;
}

Expand Down
22 changes: 4 additions & 18 deletions packages/vestjs-runtime/src/VestRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ export const Run = PersistedContext.run;

export const RuntimeApi = {
Run,
addNodeToHistory,
createRef,
persist,
reset,
useAvailableRoot,
useCurrentCursor,
useHistoryRoot,
useLoadRootNode,
useSetHistoryRoot,
useSetNextIsolateChild,
useXAppData,
};

Expand Down Expand Up @@ -131,18 +131,7 @@ export function useHistoryIsolateAtCurrentPosition() {
return historyNode;
}

export function addNodeToHistory(node: TIsolate): void {
const parent = useIsolate();
if (parent) {
useSetNextIsolateChild(node);
} else {
useSetHistory(node);
}

IsolateMutator.setParent(node, parent);
}

export function useSetHistory(history: TIsolate) {
export function useSetHistoryRoot(history: TIsolate) {
const [, setHistoryRoot] = useHistoryRoot();
setHistoryRoot(history);
}
Expand Down Expand Up @@ -172,6 +161,7 @@ export function useSetNextIsolateChild(child: TIsolate): void {
invariant(currentIsolate, ErrorStrings.NO_ACTIVE_ISOLATE);

IsolateMutator.addChild(currentIsolate, child);
IsolateMutator.setParent(child, currentIsolate);
}
export function useSetIsolateKey(key: Nullable<string>, node: TIsolate): void {
if (!key) {
Expand Down Expand Up @@ -207,7 +197,3 @@ export function reset() {

resetHistoryRoot();
}

export function useLoadRootNode(root: TIsolate): void {
useSetHistory(root);
}
Loading