Skip to content

Commit

Permalink
patch(vest): Sort out types for runstatic
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Feb 20, 2024
1 parent b3a4e72 commit 57f032d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
7 changes: 6 additions & 1 deletion packages/vest/src/exports/SuiteSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { CB } from 'vest-utils';
import { IsolateSerializer } from 'vestjs-runtime';

import { TIsolateSuite } from 'IsolateSuite';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { Suite } from 'SuiteTypes';

export type Dumpable = {
dump: CB<TIsolateSuite>;
};

export class SuiteSerializer {
static serialize(suite: Suite<TFieldName, TGroupName>) {
static serialize(suite: Dumpable) {
const dump = { ...suite.dump(), output: undefined };

return IsolateSerializer.serialize(dump);
Expand Down
3 changes: 2 additions & 1 deletion packages/vest/src/suite/SuiteTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TFieldName,
TGroupName,
} from 'SuiteResultTypes';
import { StaticSuiteRunResult } from 'createSuite';
import { TTypedMethods } from 'getTypedMethods';
import { SuiteSelectors } from 'suiteSelectors';

Expand All @@ -27,7 +28,7 @@ export type SuiteMethods<
reset: CB<void>;
remove: CB<void, [fieldName: F]>;
resetField: CB<void, [fieldName: F]>;
runStatic: CB<SuiteRunResult<F, G>, Parameters<T>>;
runStatic: CB<StaticSuiteRunResult<F, G>, Parameters<T>>;
subscribe: (cb: CB) => CB<void>;
} & TTypedMethods<F, G> &
SuiteSelectors<F, G>;
21 changes: 21 additions & 0 deletions packages/vest/src/suite/__tests__/staticSuite.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SuiteSerializer } from 'SuiteSerializer';
import { VestIsolateType } from 'VestIsolateType';
import * as vest from 'vest';
import { staticSuite } from 'vest';
Expand Down Expand Up @@ -118,4 +119,24 @@ describe('runStatic', () => {
expect(res.hasErrors('t1')).toBe(true);
expect(res.hasErrors('t2')).toBe(true);
});

it('Should serialize and resume a static suite', () => {
const suite = vest.create(() => {
vest.test('t1', () => false);
vest.test('t2', () => false);
});

const serialized = SuiteSerializer.serialize(suite.runStatic());
const suite2 = vest.create(() => {
vest.test('t3', () => false);
vest.test('t4', () => false);
});

SuiteSerializer.resume(suite2, serialized);

expect(suite2.hasErrors('t1')).toBe(true);
expect(suite2.hasErrors('t2')).toBe(true);
expect(suite2.hasErrors('t3')).toBe(false);
expect(suite2.hasErrors('t4')).toBe(false);
});
});
38 changes: 30 additions & 8 deletions packages/vest/src/suite/createSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ function createSuite<
reset: Bus.usePrepareEmitter(Events.RESET_SUITE),
resetField: Bus.usePrepareEmitter<string>(Events.RESET_FIELD),
resume: VestRuntime.persist(useLoadSuite),
runStatic: (...args: Parameters<T>): SuiteRunResult<F, G> => mountedStatic(...args) as SuiteRunResult<F, G>,
runStatic: (...args: Parameters<T>): StaticSuiteRunResult<F, G> =>
mountedStatic(...args) as StaticSuiteRunResult<F, G>,
subscribe: VestBus.subscribe,
...bindSuiteSelectors<F, G>(VestRuntime.persist(useCreateSuiteResult)),
...getTypedMethods<F, G>(),
Expand Down Expand Up @@ -125,14 +126,31 @@ function useRunSuiteCallback<
*
* suite(data);
*/

function staticSuite<
F extends TFieldName = string,
G extends TGroupName = string,
T extends CB = CB,
>(suiteName: SuiteName, suiteCallback: T): StaticSuite<F, G, T>;
function staticSuite<
F extends TFieldName = string,
G extends TGroupName = string,
T extends CB = CB,
>(suiteCallback: T): StaticSuite<F, G, T> {
>(suiteCallback: T): StaticSuite<F, G, T>;
// @vx-allow use-use
// eslint-disable-next-line max-lines-per-function
function staticSuite<
F extends TFieldName = string,
G extends TGroupName = string,
T extends CB = CB,
>(
...createArgs: [suiteName: SuiteName, suiteCallback: T] | [suiteCallback: T]
): StaticSuite<F, G, T> {
return assign(
(...args: Parameters<T>) => {
const suite = createSuite<F, G, T>(suiteCallback);
(...args: Parameters<T>): StaticSuiteRunResult<F, G> => {
const suite = createSuite<F, G, T>(
...(createArgs as unknown as [SuiteName, T]),
);

const result = suite(...args);

Expand All @@ -143,7 +161,7 @@ function staticSuite<
},
result,
),
);
) as StaticSuiteRunResult<F, G>;
},
{
...getTypedMethods<F, G>(),
Expand All @@ -155,9 +173,13 @@ export type StaticSuite<
F extends TFieldName = string,
G extends TGroupName = string,
T extends CB = CB,
> = ((...args: Parameters<T>) => SuiteRunResult<F, G> & {
> = (...args: Parameters<T>) => StaticSuiteRunResult<F, G>;

export type StaticSuiteRunResult<
F extends TFieldName = string,
G extends TGroupName = string,
> = SuiteRunResult<F, G> & {
dump: CB<TIsolateSuite>;
}) &
TTypedMethods<F, G>;
} & TTypedMethods<F, G>;

export { createSuite, staticSuite };

0 comments on commit 57f032d

Please sign in to comment.