From 7ca4a56f9b658accfe9d43d5d74834df701f41c9 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Tue, 7 Nov 2023 00:58:36 +0200 Subject: [PATCH] patch(vestjs-runtime): set isolate status as pending --- .../__snapshots__/IsolateTest.test.ts.snap | 2 ++ .../__tests__/__snapshots__/test.test.ts.snap | 6 ++++ .../vest/src/core/test/__tests__/test.test.ts | 2 +- .../SuiteSerializer.test.ts.snap | 2 +- .../__snapshots__/staticSuite.test.ts.snap | 6 ++++ .../vestjs-runtime/src/Isolate/Isolate.ts | 9 ++++++ .../vestjs-runtime/src/Isolate/IsolateKeys.ts | 3 ++ .../src/Isolate/IsolateMutator.ts | 9 ++++++ .../src/Isolate/IsolateStatus.ts | 5 ++++ .../src/Isolate/__tests__/Isolate.test.ts | 20 +++++++++++++ .../__snapshots__/asyncIsolate.test.ts.snap | 19 ++++++++++++ .../Isolate/__tests__/asyncIsolate.test.ts | 29 +++++++++++++++++-- .../__tests__/IsolateSerializer.test.ts | 4 +++ .../IsolateSerializer.test.ts.snap | 2 +- .../vestjs-runtime/src/exports/test-utils.ts | 2 ++ packages/vestjs-runtime/src/vestjs-runtime.ts | 1 + packages/vestjs-runtime/tsconfig.json | 1 + 17 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 packages/vestjs-runtime/src/Isolate/IsolateStatus.ts diff --git a/packages/vest/src/core/test/__tests__/__snapshots__/IsolateTest.test.ts.snap b/packages/vest/src/core/test/__tests__/__snapshots__/IsolateTest.test.ts.snap index 908366a58..da2c53002 100644 --- a/packages/vest/src/core/test/__tests__/__snapshots__/IsolateTest.test.ts.snap +++ b/packages/vest/src/core/test/__tests__/__snapshots__/IsolateTest.test.ts.snap @@ -25,6 +25,7 @@ exports[`IsolateTest TestObject constructor 1`] = ` "keys": {}, "output": null, "parent": null, + "status": "INITIAL", } `; @@ -53,5 +54,6 @@ exports[`IsolateTest testObject.warn Should mark the test as warning 1`] = ` "keys": {}, "output": null, "parent": null, + "status": "INITIAL", } `; diff --git a/packages/vest/src/core/test/__tests__/__snapshots__/test.test.ts.snap b/packages/vest/src/core/test/__tests__/__snapshots__/test.test.ts.snap index 07db22bb4..f5b7c6b12 100644 --- a/packages/vest/src/core/test/__tests__/__snapshots__/test.test.ts.snap +++ b/packages/vest/src/core/test/__tests__/__snapshots__/test.test.ts.snap @@ -72,7 +72,9 @@ exports[`Test Vest's \`test\` function test params creates a test without a key "warnings": [], }, "parent": null, + "status": "DONE", }, + "status": "DONE", } `; @@ -149,7 +151,9 @@ exports[`Test Vest's \`test\` function test params creates a test without a mess "warnings": [], }, "parent": null, + "status": "DONE", }, + "status": "DONE", } `; @@ -224,6 +228,8 @@ exports[`Test Vest's \`test\` function test params creates a test without a mess "warnings": [], }, "parent": null, + "status": "DONE", }, + "status": "DONE", } `; diff --git a/packages/vest/src/core/test/__tests__/test.test.ts b/packages/vest/src/core/test/__tests__/test.test.ts index 2798ca82b..676eabd12 100644 --- a/packages/vest/src/core/test/__tests__/test.test.ts +++ b/packages/vest/src/core/test/__tests__/test.test.ts @@ -193,7 +193,7 @@ describe("Test Vest's `test` function", () => { expect(testObject.key).toBe('keyboardcat'); expect(testObject.data.message).toBe('failure message'); expect(IsolateSerializer.serialize(testObject)).toMatchInlineSnapshot( - `"{"$":"Test","D":{"severity":"error","status":"PASSING","abortController":{},"fieldName":"field_name","message":"failure message"},"k":"keyboardcat"}"` + `"{"$":"Test","D":{"severity":"error","status":"PASSING","abortController":{},"fieldName":"field_name","message":"failure message"},"S":"DONE","k":"keyboardcat"}"` ); }); diff --git a/packages/vest/src/exports/__tests__/__snapshots__/SuiteSerializer.test.ts.snap b/packages/vest/src/exports/__tests__/__snapshots__/SuiteSerializer.test.ts.snap index fe12a2c5b..c050d5022 100644 --- a/packages/vest/src/exports/__tests__/__snapshots__/SuiteSerializer.test.ts.snap +++ b/packages/vest/src/exports/__tests__/__snapshots__/SuiteSerializer.test.ts.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`SuiteSerializer Should produce a valid serialized dump 1`] = `"{"children":[{"$":"Focused","D":{"focusMode":0,"match":["field_1"],"matchAll":false}},{"$":"Test","D":{"severity":"error","status":"FAILED","abortController":{},"fieldName":"field_1","message":"field_1_message"}},{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_2","message":"field_2_message"}},{"children":[{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_3","groupName":"group_1","message":"field_3_message_1"}},{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_3","groupName":"group_1","message":"field_3_message_2"}},{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_4","groupName":"group_1","message":"field_4_message"}}],"$":"Group","D":{}},{"children":[{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_5","message":"field_5_message"}}],"$":"SkipWhen","D":{}}],"$":"Suite","D":{"optional":{}}}"`; +exports[`SuiteSerializer Should produce a valid serialized dump 1`] = `"{"children":[{"$":"Focused","D":{"focusMode":0,"match":["field_1"],"matchAll":false},"S":"DONE"},{"$":"Test","D":{"severity":"error","status":"FAILED","abortController":{},"fieldName":"field_1","message":"field_1_message"},"S":"DONE"},{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_2","message":"field_2_message"},"S":"DONE"},{"children":[{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_3","groupName":"group_1","message":"field_3_message_1"},"S":"DONE"},{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_3","groupName":"group_1","message":"field_3_message_2"},"S":"DONE"},{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_4","groupName":"group_1","message":"field_4_message"},"S":"DONE"}],"$":"Group","D":{},"S":"DONE"},{"children":[{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_5","message":"field_5_message"},"S":"DONE"}],"$":"SkipWhen","D":{},"S":"DONE"}],"$":"Suite","D":{"optional":{}},"S":"DONE"}"`; diff --git a/packages/vest/src/suite/__tests__/__snapshots__/staticSuite.test.ts.snap b/packages/vest/src/suite/__tests__/__snapshots__/staticSuite.test.ts.snap index c81bc15b1..b8fae8a67 100644 --- a/packages/vest/src/suite/__tests__/__snapshots__/staticSuite.test.ts.snap +++ b/packages/vest/src/suite/__tests__/__snapshots__/staticSuite.test.ts.snap @@ -28,6 +28,7 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "keys": null, "output": undefined, "parent": [Circular], + "status": "DONE", }, { "$type": "Test", @@ -52,6 +53,7 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "keys": null, "output": undefined, "parent": [Circular], + "status": "DONE", }, { "$type": "Group", @@ -81,6 +83,7 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "keys": null, "output": undefined, "parent": [Circular], + "status": "DONE", }, { "$type": "Test", @@ -106,6 +109,7 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "keys": null, "output": undefined, "parent": [Circular], + "status": "DONE", }, ], "data": {}, @@ -113,6 +117,7 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "keys": null, "output": undefined, "parent": [Circular], + "status": "DONE", }, ], "data": { @@ -212,5 +217,6 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "warnings": [], }, "parent": null, + "status": "DONE", } `; diff --git a/packages/vestjs-runtime/src/Isolate/Isolate.ts b/packages/vestjs-runtime/src/Isolate/Isolate.ts index e9ea74e8a..9f92a05d0 100644 --- a/packages/vestjs-runtime/src/Isolate/Isolate.ts +++ b/packages/vestjs-runtime/src/Isolate/Isolate.ts @@ -2,6 +2,7 @@ import { CB, Maybe, Nullable, isNotNullish, isPromise } from 'vest-utils'; import { IsolateKeys } from 'IsolateKeys'; import { IsolateMutator } from 'IsolateMutator'; +import { IsolateStatus } from 'IsolateStatus'; import { Reconciler } from 'Reconciler'; import * as VestRuntime from 'VestRuntime'; @@ -13,6 +14,7 @@ export type TIsolate

= { [IsolateKeys.Type]: string; [IsolateKeys.Keys]: Nullable>; [IsolateKeys.Data]: DataOnly

; + [IsolateKeys.Status]: IsolateStatus; children: Nullable; key: IsolateKey; output: any; @@ -85,11 +87,17 @@ function useRunAsNew( const output = callback(current); if (isPromise(output)) { + IsolateMutator.setPending(current); + output.then(iso => { if (Isolate.isIsolate(iso)) { IsolateMutator.addChild(current, iso); } + + IsolateMutator.setDone(current); }); + } else { + IsolateMutator.setDone(current); } return output; @@ -112,6 +120,7 @@ function baseIsolate( [IsolateKeys.Parent]: null, [IsolateKeys.Type]: type, [IsolateKeys.Data]: data as IsolateData, + [IsolateKeys.Status]: IsolateStatus.INITIAL, children: null, key, output: null, diff --git a/packages/vestjs-runtime/src/Isolate/IsolateKeys.ts b/packages/vestjs-runtime/src/Isolate/IsolateKeys.ts index 8a1c410ac..66cf037ef 100644 --- a/packages/vestjs-runtime/src/Isolate/IsolateKeys.ts +++ b/packages/vestjs-runtime/src/Isolate/IsolateKeys.ts @@ -5,6 +5,7 @@ export enum IsolateKeys { Parent = 'parent', Data = 'data', AllowReorder = 'allowReorder', + Status = 'status', } enum MinifiedKeys { @@ -14,6 +15,7 @@ enum MinifiedKeys { Parent = 'P', Data = 'D', AllowReorder = 'aR', + Status = 'S', } export const KeyToMinified = { @@ -23,6 +25,7 @@ export const KeyToMinified = { [IsolateKeys.Data]: MinifiedKeys.Data, [IsolateKeys.Key]: MinifiedKeys.Key, [IsolateKeys.AllowReorder]: MinifiedKeys.AllowReorder, + [IsolateKeys.Status]: MinifiedKeys.Status, }; // This const is an object that looks like this: diff --git a/packages/vestjs-runtime/src/Isolate/IsolateMutator.ts b/packages/vestjs-runtime/src/Isolate/IsolateMutator.ts index 79467c1f5..1c19c4b06 100644 --- a/packages/vestjs-runtime/src/Isolate/IsolateMutator.ts +++ b/packages/vestjs-runtime/src/Isolate/IsolateMutator.ts @@ -1,6 +1,7 @@ import { Nullable, invariant, isNullish } from 'vest-utils'; import { TIsolate } from 'Isolate'; +import { IsolateStatus } from 'IsolateStatus'; export class IsolateMutator { static setParent(isolate: TIsolate, parent: Nullable): TIsolate { @@ -50,4 +51,12 @@ export class IsolateMutator { static setData(isolate: TIsolate, data: any): void { isolate.data = data; } + + static setPending(isolate: TIsolate): void { + isolate.status = IsolateStatus.PENDING; + } + + static setDone(isolate: TIsolate): void { + isolate.status = IsolateStatus.DONE; + } } diff --git a/packages/vestjs-runtime/src/Isolate/IsolateStatus.ts b/packages/vestjs-runtime/src/Isolate/IsolateStatus.ts new file mode 100644 index 000000000..f8a4b8d5e --- /dev/null +++ b/packages/vestjs-runtime/src/Isolate/IsolateStatus.ts @@ -0,0 +1,5 @@ +export enum IsolateStatus { + PENDING = 'PENDING', + DONE = 'DONE', + INITIAL = 'INITIAL', +} diff --git a/packages/vestjs-runtime/src/Isolate/__tests__/Isolate.test.ts b/packages/vestjs-runtime/src/Isolate/__tests__/Isolate.test.ts index a27b0ea22..da7dfbc3d 100644 --- a/packages/vestjs-runtime/src/Isolate/__tests__/Isolate.test.ts +++ b/packages/vestjs-runtime/src/Isolate/__tests__/Isolate.test.ts @@ -93,6 +93,26 @@ describe('Isolate', () => { expect(child.parent).toBe(parent); }); }); + + test('Isolate status is INITIAL before running', () => { + const control = jest.fn(); + withRunTime(() => { + return Isolate.create(IsolateType.Isolate, () => { + expect(useAvailableRoot().status).toBe('INITIAL'); + control(); + }); + }); + + expect(control).toHaveBeenCalled(); + }); + + test('Isolate status is DONE after running', () => { + const isolate = withRunTime(() => { + return Isolate.create(IsolateType.Isolate, () => {}); + }); + + expect(isolate.status).toBe('DONE'); + }); }); function withRunTime(fn: CB) { diff --git a/packages/vestjs-runtime/src/Isolate/__tests__/__snapshots__/asyncIsolate.test.ts.snap b/packages/vestjs-runtime/src/Isolate/__tests__/__snapshots__/asyncIsolate.test.ts.snap index 0b332465b..969b3cf61 100644 --- a/packages/vestjs-runtime/src/Isolate/__tests__/__snapshots__/asyncIsolate.test.ts.snap +++ b/packages/vestjs-runtime/src/Isolate/__tests__/__snapshots__/asyncIsolate.test.ts.snap @@ -18,6 +18,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = ` "keys": null, "output": undefined, "parent": [Circular], + "status": "DONE", }, { "$type": "UGrandChild_2", @@ -28,6 +29,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = ` "keys": null, "output": undefined, "parent": [Circular], + "status": "DONE", }, { "$type": "UGrandChild_3", @@ -38,6 +40,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = ` "keys": null, "output": undefined, "parent": [Circular], + "status": "DONE", }, ], "data": {}, @@ -45,6 +48,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = ` "keys": null, "output": undefined, "parent": [Circular], + "status": "DONE", }, ], "data": {}, @@ -52,5 +56,20 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = ` "keys": null, "output": Promise {}, "parent": null, + "status": "DONE", +} +`; + +exports[`AsyncIsolate It should set the isolate state to pending 1`] = ` +{ + "$type": "URoot", + "allowReorder": undefined, + "children": null, + "data": {}, + "key": null, + "keys": null, + "output": Promise {}, + "parent": null, + "status": "PENDING", } `; diff --git a/packages/vestjs-runtime/src/Isolate/__tests__/asyncIsolate.test.ts b/packages/vestjs-runtime/src/Isolate/__tests__/asyncIsolate.test.ts index 05c8ae942..a0e63ae2d 100644 --- a/packages/vestjs-runtime/src/Isolate/__tests__/asyncIsolate.test.ts +++ b/packages/vestjs-runtime/src/Isolate/__tests__/asyncIsolate.test.ts @@ -8,12 +8,10 @@ describe('AsyncIsolate', () => { test('It should resolve async isolate into the parent', () => { return new Promise(async done => { let root = {} as TIsolate; - const control = jest.fn(); withRunTime(() => { // Create root isolate from which all others will be created root = Isolate.create('URoot', genChildren); }); - expect(control).not.toHaveBeenCalled(); expect(root).toMatchInlineSnapshot(` { "$type": "URoot", @@ -24,6 +22,7 @@ describe('AsyncIsolate', () => { "keys": null, "output": Promise {}, "parent": null, + "status": "PENDING", } `); await wait(10); @@ -46,6 +45,32 @@ describe('AsyncIsolate', () => { done(); }); }); + + test('It should set the isolate state to pending', () => { + return new Promise(done => { + let root = {} as TIsolate; + withRunTime(() => { + // Create root isolate from which all others will be created + root = Isolate.create('URoot', genChildren); + }); + expect(root).toMatchSnapshot(); + expect(root?.status).toBe('PENDING'); + done(); + }); + }); + + it('should set the isolate state to done when complete', () => { + return new Promise(async done => { + let root = {} as TIsolate; + withRunTime(() => { + // Create root isolate from which all others will be created + root = Isolate.create('URoot', genChildren); + }); + await wait(10); + expect(root?.status).toBe('DONE'); + done(); + }); + }); }); function withRunTime(fn: CB) { diff --git a/packages/vestjs-runtime/src/exports/__tests__/IsolateSerializer.test.ts b/packages/vestjs-runtime/src/exports/__tests__/IsolateSerializer.test.ts index 87908c54c..07fb055a1 100644 --- a/packages/vestjs-runtime/src/exports/__tests__/IsolateSerializer.test.ts +++ b/packages/vestjs-runtime/src/exports/__tests__/IsolateSerializer.test.ts @@ -29,21 +29,25 @@ describe('IsolateSerializer', () => { "some_data": true, }, "parent": [Circular], + "status": "DONE", }, { "$type": "UChild_2", "data": {}, "parent": [Circular], + "status": "DONE", }, { "$type": "UChild_3", "data": {}, "parent": [Circular], + "status": "DONE", }, ], "data": { "some_data": true, }, + "status": "DONE", } `); }); diff --git a/packages/vestjs-runtime/src/exports/__tests__/__snapshots__/IsolateSerializer.test.ts.snap b/packages/vestjs-runtime/src/exports/__tests__/__snapshots__/IsolateSerializer.test.ts.snap index 197a5a7cb..6adbe02aa 100644 --- a/packages/vestjs-runtime/src/exports/__tests__/__snapshots__/IsolateSerializer.test.ts.snap +++ b/packages/vestjs-runtime/src/exports/__tests__/__snapshots__/IsolateSerializer.test.ts.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`IsolateSerializer serialize Should produce serialized dump 1`] = `"{"children":[{"$":"UChild_1","D":{"some_data":true}},{"$":"UChild_2","D":{}},{"$":"UChild_3","D":{}}],"$":"URoot","D":{"some_data":true}}"`; +exports[`IsolateSerializer serialize Should produce serialized dump 1`] = `"{"children":[{"$":"UChild_1","D":{"some_data":true},"S":"DONE"},{"$":"UChild_2","D":{},"S":"DONE"},{"$":"UChild_3","D":{},"S":"DONE"}],"$":"URoot","D":{"some_data":true},"S":"DONE"}"`; diff --git a/packages/vestjs-runtime/src/exports/test-utils.ts b/packages/vestjs-runtime/src/exports/test-utils.ts index f5fa3ead4..a623f6f8a 100644 --- a/packages/vestjs-runtime/src/exports/test-utils.ts +++ b/packages/vestjs-runtime/src/exports/test-utils.ts @@ -1,5 +1,6 @@ import { TIsolate } from 'Isolate'; import { IsolateKeys } from 'IsolateKeys'; +import { IsolateStatus } from 'IsolateStatus'; export function genTestIsolate(data: Record = {}): TIsolate { return { @@ -9,6 +10,7 @@ export function genTestIsolate(data: Record = {}): TIsolate { keys: {}, output: null, parent: null, + status: IsolateStatus.INITIAL, [IsolateKeys.Type]: 'UnitTest', }; } diff --git a/packages/vestjs-runtime/src/vestjs-runtime.ts b/packages/vestjs-runtime/src/vestjs-runtime.ts index ce38c7785..b9d162788 100644 --- a/packages/vestjs-runtime/src/vestjs-runtime.ts +++ b/packages/vestjs-runtime/src/vestjs-runtime.ts @@ -1,4 +1,5 @@ export { IsolateKey, TIsolate, Isolate } from 'Isolate'; +export { IsolateStatus } from 'IsolateStatus'; export { Reconciler, IRecociler } from 'Reconciler'; export * as Walker from 'IsolateWalker'; export { RuntimeApi as VestRuntime } from 'VestRuntime'; diff --git a/packages/vestjs-runtime/tsconfig.json b/packages/vestjs-runtime/tsconfig.json index d748a0d0e..156a69a70 100644 --- a/packages/vestjs-runtime/tsconfig.json +++ b/packages/vestjs-runtime/tsconfig.json @@ -15,6 +15,7 @@ "test-utils": ["./src/exports/test-utils.ts"], "IsolateSerializer": ["./src/exports/IsolateSerializer.ts"], "ErrorStrings": ["./src/errors/ErrorStrings.ts"], + "IsolateStatus": ["./src/Isolate/IsolateStatus.ts"], "IsolateSelectors": ["./src/Isolate/IsolateSelectors.ts"], "IsolateMutator": ["./src/Isolate/IsolateMutator.ts"], "IsolateKeys": ["./src/Isolate/IsolateKeys.ts"],