Skip to content

Commit

Permalink
types(vest): export core types (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush authored Feb 26, 2022
1 parent 7ce9a06 commit 1a6adf3
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 330 deletions.
6 changes: 3 additions & 3 deletions packages/vest/src/core/isolate/isolates/omitWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import optionalFunctionValue from 'optionalFunctionValue';
import { IsolateTypes } from 'IsolateTypes';
import ctx from 'ctx';
import { isolate } from 'isolate';
import { produceDraft, TDraftResult } from 'produceDraft';
import { produceSuiteResult, SuiteResult } from 'produceSuiteResult';

/**
* Conditionally omits tests from the suite.
Expand All @@ -15,7 +15,7 @@ import { produceDraft, TDraftResult } from 'produceDraft';
* });
*/
export default function omitWhen(
conditional: boolean | ((draft: TDraftResult) => boolean),
conditional: boolean | ((draft: SuiteResult) => boolean),
callback: (...args: any[]) => void
): void {
isolate({ type: IsolateTypes.OMIT_WHEN }, () => {
Expand All @@ -25,7 +25,7 @@ export default function omitWhen(
isOmitted() ||
optionalFunctionValue(
conditional,
optionalFunctionValue(produceDraft)
optionalFunctionValue(produceSuiteResult)
),
},
() => callback()
Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/core/isolate/isolates/skipWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import optionalFunctionValue from 'optionalFunctionValue';
import { IsolateTypes } from 'IsolateTypes';
import ctx from 'ctx';
import { isolate } from 'isolate';
import { produceDraft, TDraftResult } from 'produceDraft';
import { produceSuiteResult, SuiteResult } from 'produceSuiteResult';

/**
* Conditionally skips running tests within the callback.
Expand All @@ -15,7 +15,7 @@ import { produceDraft, TDraftResult } from 'produceDraft';
* });
*/
export default function skipWhen(
conditional: boolean | ((draft: TDraftResult) => boolean),
conditional: boolean | ((draft: SuiteResult) => boolean),
callback: (...args: any[]) => void
): void {
isolate({ type: IsolateTypes.SKIP_WHEN }, () => {
Expand All @@ -28,7 +28,7 @@ export default function skipWhen(
// Otherwise, we should skip the test if the conditional is true.
optionalFunctionValue(
conditional,
optionalFunctionValue(produceDraft)
optionalFunctionValue(produceSuiteResult)
),
},
() => callback()
Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/core/state/createStateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { NestedArray } from 'nestedArray';
import type { TState } from 'vast';

import VestTest from 'VestTest';
import type { TDraftResult } from 'produceDraft';
import type { SuiteResult } from 'produceSuiteResult';

export default function createStateRef(
state: TState,
Expand All @@ -15,8 +15,8 @@ export default function createStateRef(
suiteId: state.registerStateKey<string>(suiteId),
suiteName: state.registerStateKey<string | void>(suiteName),
testCallbacks: state.registerStateKey<{
fieldCallbacks: Record<string, Array<(res: TDraftResult) => void>>;
doneCallbacks: Array<(res: TDraftResult) => void>;
fieldCallbacks: Record<string, Array<(res: SuiteResult) => void>>;
doneCallbacks: Array<(res: SuiteResult) => void>;
}>(() => ({
fieldCallbacks: {},
doneCallbacks: [],
Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/core/state/stateHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { TStateHandlerReturn } from 'vast';
import VestTest from 'VestTest';
import type { TStateRef } from 'createStateRef';
import ctx from 'ctx';
import type { TDraftResult } from 'produceDraft';
import type { SuiteResult } from 'produceSuiteResult';

// STATE REF
export function useStateRef(): Exclude<TStateRef, void> {
Expand All @@ -23,8 +23,8 @@ export function useSuiteName(): string | void {
return useStateRef().suiteName()[0];
}
export function useTestCallbacks(): TStateHandlerReturn<{
fieldCallbacks: Record<string, ((res: TDraftResult) => void)[]>;
doneCallbacks: ((res: TDraftResult) => void)[];
fieldCallbacks: Record<string, ((res: SuiteResult) => void)[]>;
doneCallbacks: ((res: SuiteResult) => void)[];
}> {
return useStateRef().testCallbacks();
}
Expand Down
14 changes: 7 additions & 7 deletions packages/vest/src/core/suite/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { IsolateTypes } from 'IsolateTypes';
import createStateRef from 'createStateRef';
import context from 'ctx';
import { isolate } from 'isolate';
import { IVestResult, produceFullResult } from 'produce';
import { produceDraft, TDraftResult } from 'produceDraft';
import { produceSuiteResult, SuiteResult } from 'produceSuiteResult';
import { SuiteRunResult, produceFullResult } from 'produceSuiteRunResult';
import { initBus, Events } from 'vestBus';

type CreateProperties = {
get: () => TDraftResult;
get: () => SuiteResult;
reset: () => void;
resetField: (fieldName: string) => void;
remove: (fieldName: string) => void;
Expand All @@ -22,7 +22,7 @@ type CreateProperties = {
type CB = (...args: any[]) => void;

type SuiteReturnType<T extends CB> = {
(...args: Parameters<T>): IVestResult;
(...args: Parameters<T>): SuiteRunResult;
} & CreateProperties;

/**
Expand Down Expand Up @@ -61,9 +61,9 @@ function create<T extends CB>(
const stateRef = createStateRef(state, { suiteId: genId(), suiteName });

interface IVestSuite {
(...args: Parameters<T>): IVestResult;
(...args: Parameters<T>): SuiteRunResult;

get: () => TDraftResult;
get: () => SuiteResult;
reset: () => void;
resetField: (fieldName: string) => void;
remove: (fieldName: string) => void;
Expand Down Expand Up @@ -92,7 +92,7 @@ function create<T extends CB>(
return produceFullResult();
}),
{
get: context.bind(ctxRef, produceDraft),
get: context.bind(ctxRef, produceSuiteResult),
remove: context.bind(ctxRef, (fieldName: string) => {
bus.emit(Events.REMOVE_FIELD, fieldName);
}),
Expand Down
12 changes: 6 additions & 6 deletions packages/vest/src/core/suite/produce/__tests__/produce.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { produceFullResult } from 'produce';
import { produceDraft } from 'produceDraft';

import itWithContext from '../../../../../testUtils/itWithContext';
import { dummyTest } from '../../../../../testUtils/testDummy';
import { setTestObjects } from '../../../../../testUtils/testObjects';

import { produceSuiteResult } from 'produceSuiteResult';
import { produceFullResult } from 'produceSuiteRunResult';

const methods = {
produceDraft,
produceSuiteResult,
produceFullResult,
};

Expand Down Expand Up @@ -103,10 +103,10 @@ describe.each(Object.keys(methods))('produce method: %s', methodName => {
});
});

describe('produceDraft', () => {
describe('produceSuiteResult', () => {
describe('exposed methods', () => {
itWithContext('Should have all exposed methods', () => {
expect(produceDraft()).toMatchInlineSnapshot(`
expect(produceSuiteResult()).toMatchInlineSnapshot(`
Object {
"errorCount": 0,
"getErrors": [Function],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { produceFullResult } from 'produce';
import { produceDraft } from 'produceDraft';

import itWithContext from '../../../../../../testUtils/itWithContext';
import { dummyTest } from '../../../../../../testUtils/testDummy';
import { setTestObjects } from '../../../../../../testUtils/testObjects';

import { produceSuiteResult } from 'produceSuiteResult';
import { produceFullResult } from 'produceSuiteRunResult';

const methods = {
produceDraft,
produceSuiteResult,
produceFullResult,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { produceFullResult } from 'produce';
import { produceDraft } from 'produceDraft';

import itWithContext from '../../../../../../testUtils/itWithContext';
import { dummyTest } from '../../../../../../testUtils/testDummy';
import { setTestObjects } from '../../../../../../testUtils/testObjects';

import { produceSuiteResult } from 'produceSuiteResult';
import { produceFullResult } from 'produceSuiteRunResult';

const methods = {
produceDraft,
produceSuiteResult,
produceFullResult,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
emptyTestObjects,
} from '../../../../../../testUtils/testObjects';

import { produceFullResult } from 'produce';
import { produceDraft } from 'produceDraft';
import { produceSuiteResult } from 'produceSuiteResult';
import { produceFullResult } from 'produceSuiteRunResult';

const methods = {
produceDraft,
produceSuiteResult,
produceFullResult,
};

Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/core/suite/produce/isValid.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isNotEmpty, isEmpty } from 'isEmpty';

import { nonMatchingFieldName } from 'matchingFieldName';
import type { TDraftResult } from 'produceDraft';
import type { SuiteResult } from 'produceSuiteResult';
import {
useTestsFlat,
useAllIncomplete,
Expand All @@ -10,7 +10,7 @@ import {
} from 'stateHooks';

// eslint-disable-next-line max-statements, complexity
export function isValid(result: TDraftResult, fieldName?: string): boolean {
export function isValid(result: SuiteResult, fieldName?: string): boolean {
if (fieldIsOmitted(fieldName)) {
return true;
}
Expand Down Expand Up @@ -59,7 +59,7 @@ function hasNonOptionalIncomplete(fieldName?: string) {
);
}

function fieldDoesNotExist(result: TDraftResult, fieldName?: string): boolean {
function fieldDoesNotExist(result: SuiteResult, fieldName?: string): boolean {
return !!fieldName && isEmpty(result.tests[fieldName]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useStateRef, useTestsFlat, useSuiteName } from 'stateHooks';

const cache = createCache(20);

export function produceDraft(): TDraftResult {
export function produceSuiteResult(): SuiteResult {
const testObjects = useTestsFlat();

const ctxRef = { stateRef: useStateRef() };
Expand All @@ -31,15 +31,15 @@ export function produceDraft(): TDraftResult {
hasWarnings: ctx.bind(ctxRef, hasWarnings),
hasWarningsByGroup: ctx.bind(ctxRef, hasWarningsByGroup),
isValid: ctx.bind(ctxRef, (fieldName?: string) =>
isValid(produceDraft(), fieldName)
isValid(produceSuiteResult(), fieldName)
),
suiteName,
});
})
);
}

export type TDraftResult = ReturnType<typeof genTestsSummary> & {
export type SuiteResult = ReturnType<typeof genTestsSummary> & {
/**
* Returns whether the suite as a whole is valid.
* Determined if there are no errors, and if no
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import isFunction from 'isFunction';

import ctx from 'ctx';
import hasRemainingTests from 'hasRemainingTests';
import { produceDraft, TDraftResult } from 'produceDraft';
import { produceSuiteResult, SuiteResult } from 'produceSuiteResult';
import { useStateRef, useTestCallbacks, useTestsFlat } from 'stateHooks';

const cache = createCache(20);

export function produceFullResult(): IVestResult {
export function produceFullResult(): SuiteRunResult {
const testObjects = useTestsFlat();
const ctxRef = { stateRef: useStateRef() };
return cache(
[testObjects],
ctx.bind(ctxRef, () =>
assign({}, produceDraft(), {
assign({}, produceSuiteResult(), {
done: ctx.bind(ctxRef, done),
})
)
Expand All @@ -28,10 +28,10 @@ export function produceFullResult(): IVestResult {
*/

function shouldSkipDoneRegistration(
callback: (res: TDraftResult) => void,
callback: (res: SuiteResult) => void,

fieldName: string | undefined,
output: IVestResult
output: SuiteRunResult
): boolean {
// If we do not have any test runs for the current field
return !!(
Expand All @@ -54,9 +54,9 @@ function shouldRunDoneCallback(fieldName?: string): boolean {
* Registers done callbacks.
* @register {Object} Vest output object.
*/
const done: IDone = function done(...args): IVestResult {
const done: IDone = function done(...args): SuiteRunResult {
const [callback, fieldName] = args.reverse() as [
(res: TDraftResult) => void,
(res: SuiteResult) => void,
string
];

Expand All @@ -66,7 +66,7 @@ const done: IDone = function done(...args): IVestResult {
return output;
}

const doneCallback = () => callback(produceDraft());
const doneCallback = () => callback(produceSuiteResult());

if (shouldRunDoneCallback(fieldName)) {
doneCallback();
Expand All @@ -93,9 +93,11 @@ function deferDoneCallback(doneCallback: () => void, fieldName?: string): void {
});
}

export type IVestResult = TDraftResult & { done: IDone };
export type SuiteRunResult = SuiteResult & { done: IDone };

interface IDone {
(...args: [cb: (res: TDraftResult) => void]): IVestResult;
(...args: [fieldName: string, cb: (res: TDraftResult) => void]): IVestResult;
(...args: [cb: (res: SuiteResult) => void]): SuiteRunResult;
(
...args: [fieldName: string, cb: (res: SuiteResult) => void]
): SuiteRunResult;
}
6 changes: 3 additions & 3 deletions packages/vest/src/exports/classnames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import isFunction from 'isFunction';
import throwError from 'throwError';

import { parse } from 'parser';
import type { IVestResult } from 'produce';
import type { TDraftResult } from 'produceDraft';
import type { SuiteResult } from 'produceSuiteResult';
import type { SuiteRunResult } from 'produceSuiteRunResult';

/**
* Creates a function that returns class names that match the validation result
*/
export default function classnames(
res: IVestResult | TDraftResult,
res: SuiteRunResult | SuiteResult,
classes: TSupportedClasses = {}
): (fieldName: string) => string {
if (!res || !isFunction(res.hasErrors)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/exports/parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { greaterThan } from 'greaterThan';
import hasOwnProperty from 'hasOwnProperty';

import type { IVestResult } from 'produce';
import type { TDraftResult } from 'produceDraft';
import type { SuiteResult } from 'produceSuiteResult';
import type { SuiteRunResult } from 'produceSuiteRunResult';

export function parse(res: IVestResult | TDraftResult): {
export function parse(res: SuiteRunResult | SuiteResult): {
valid: (fieldName?: string) => boolean;
tested: (fieldName?: string) => boolean;
invalid: (fieldName?: string) => boolean;
Expand Down
8 changes: 4 additions & 4 deletions packages/vest/src/exports/promisify.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import isFunction from 'isFunction';
import throwError from 'throwError';

import { IVestResult } from 'produce';
import { TDraftResult } from 'produceDraft';
import { SuiteResult } from 'produceSuiteResult';
import { SuiteRunResult } from 'produceSuiteRunResult';

const promisify =
(validatorFn: (...args: any[]) => IVestResult) =>
(...args: any[]): Promise<TDraftResult> => {
(validatorFn: (...args: any[]) => SuiteRunResult) =>
(...args: any[]): Promise<SuiteResult> => {
if (!isFunction(validatorFn)) {
throwError('promisify: Expected validatorFn to be a function.');
}
Expand Down
Loading

1 comment on commit 1a6adf3

@vercel
Copy link

@vercel vercel bot commented on 1a6adf3 Feb 26, 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-website.vercel.app
vest-next.vercel.app
vest-next-ealush.vercel.app
vest-next-git-latest-ealush.vercel.app

Please sign in to comment.