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

2024-03-30-debouce #1142

Merged
merged 3 commits into from
Apr 3, 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
1 change: 1 addition & 0 deletions packages/vest/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tsconfig.json
!enforce@date/
!enforce@compounds/
!enforce@compose/
!debounce/
!classnames/
!SuiteSerializer/

Expand Down
30 changes: 30 additions & 0 deletions packages/vest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,36 @@
"module": "./dist/es/enforce/compose.production.js",
"default": "./dist/cjs/enforce/compose.production.js"
},
"./debounce": {
"production": {
"types": "./types/debounce.d.ts",
"browser": "./dist/es/debounce.production.js",
"umd": "./dist/umd/debounce.production.js",
"import": "./dist/es/debounce.production.js",
"require": "./dist/cjs/debounce.production.js",
"node": "./dist/cjs/debounce.production.js",
"module": "./dist/es/debounce.production.js",
"default": "./dist/cjs/debounce.production.js"
},
"development": {
"types": "./types/debounce.d.ts",
"browser": "./dist/es/debounce.development.js",
"umd": "./dist/umd/debounce.development.js",
"import": "./dist/es/debounce.development.js",
"require": "./dist/cjs/debounce.development.js",
"node": "./dist/cjs/debounce.development.js",
"module": "./dist/es/debounce.development.js",
"default": "./dist/cjs/debounce.development.js"
},
"types": "./types/debounce.d.ts",
"browser": "./dist/es/debounce.production.js",
"umd": "./dist/umd/debounce.production.js",
"import": "./dist/es/debounce.production.js",
"require": "./dist/cjs/debounce.production.js",
"node": "./dist/cjs/debounce.production.js",
"module": "./dist/es/debounce.production.js",
"default": "./dist/cjs/debounce.production.js"
},
"./classnames": {
"production": {
"types": "./types/classnames.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import type { TIsolate } from 'vestjs-runtime';

import { ErrorStrings } from 'ErrorStrings';
import { IsolateReconciler } from 'IsolateReconciler';
import type { TIsolateTest } from 'IsolateTest';
import { VestTest } from 'VestTest';
import cancelOverriddenPendingTest from 'cancelOverriddenPendingTest';
import { isSameProfileTest } from 'isSameProfileTest';
import { useIsExcluded } from 'useIsExcluded';
import { useVerifyTestRun } from 'verifyTestRun';

export class IsolateTestReconciler extends IsolateReconciler {
export class IsolateTestReconciler {
static match(currentNode: TIsolate, historyNode: TIsolate): boolean {
return VestTest.is(currentNode) && VestTest.is(historyNode);
}
Expand Down Expand Up @@ -52,7 +51,7 @@
return newNode;
}

// FIXME: May-13-2023

Check warning on line 54 in packages/vest/src/core/isolate/IsolateTest/IsolateTestReconciler.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'fixme' comment: 'FIXME: May-13-2023'
// This may not be the most ideal solution.
// In short: if the node was omitted in the previous run,
// we want to re-evaluate it. The reason is that we may incorrectly
Expand Down
19 changes: 17 additions & 2 deletions packages/vest/src/core/isolate/VestReconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ import { TIsolate } from 'vestjs-runtime';

import { IsolateTestReconciler } from 'IsolateTestReconciler';

const reconcilers: IsolateReconciler[] = [IsolateTestReconciler];

export function registerReconciler(reconciler: IsolateReconciler) {
if (reconcilers.includes(reconciler)) {
return;
}

reconcilers.push(reconciler);
}

export function VestReconciler(
currentNode: TIsolate,
historyNode: TIsolate
historyNode: TIsolate,
): Nullable<TIsolate> {
return (
[IsolateTestReconciler]
reconcilers
.find(reconciler => reconciler.match(currentNode, historyNode))
?.reconcile(currentNode as any, historyNode as any) ?? null
);
}

export type IsolateReconciler = {
match(currentNode: TIsolate, historyNode: TIsolate): boolean;
reconcile(elecurrentNode: TIsolate, historyNode: TIsolate): TIsolate;
};
2 changes: 1 addition & 1 deletion packages/vest/src/core/test/TestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Maybe } from 'vest-utils';

import { TFieldName } from 'SuiteResultTypes';

type TestFnPayload = { signal: AbortSignal };
export type TestFnPayload = { signal: AbortSignal };

export type TestFn = (payload: TestFnPayload) => TestResult;
export type AsyncTest = Promise<void>;
Expand Down
233 changes: 233 additions & 0 deletions packages/vest/src/exports/__tests__/debounce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import wait from 'wait';

import { TestFnPayload } from 'TestTypes';
import * as vest from 'vest';
import debounce from 'vest/debounce';

describe('debounce', () => {
describe('Sync test', () => {
describe('Returning false', () => {
it('Should debounce test function calls when used', () => {
const test = jest.fn(() => {
return false;
});

return new Promise<void>(done => {
const suite = vest.create('suite', () => {
vest.test('test', 'message', debounce(test, 1500));
});

suite();
suite();
suite();
suite();
suite();
suite();
suite().done(() => {
expect(test).toHaveBeenCalledTimes(1);
expect(suite.isValid()).toBe(false);
done();
});
});
});
});

describe('Throwing an error', () => {
it('Should debounce test function calls when used', () => {
const test = jest.fn(() => {
throw new Error();
});

return new Promise<void>(done => {
const suite = vest.create('suite', () => {
vest.test('test', 'message', debounce(test, 1500));
});

suite();
suite();
suite();
suite();
suite();
suite();
suite().done(() => {
expect(test).toHaveBeenCalledTimes(1);
expect(suite.isValid()).toBe(false);
done();
});
});
});
});
});

describe('Async test', () => {
it('Should complete the async test after the delay', async () => {
const t = jest.fn(async () => {
await wait(1000);
vest.enforce(1).equals(2);
});

const suite = vest.create('suite', () => {
vest.test('test', 'message', debounce(t, 1500));
});

suite();
expect(t).toHaveBeenCalledTimes(0);
expect(suite.isPending()).toBe(true);
await wait(2000);
expect(t).toHaveBeenCalledTimes(1);
expect(suite.get().hasErrors('test')).toBe(false);
expect(suite.isPending()).toBe(true);
await wait(1000);
expect(suite.isPending()).toBe(false);
expect(suite.get().hasErrors('test')).toBe(true);
});
});

describe('When delay met multiple times', () => {
it('Should call once per completed delay', async () => {
const test = jest.fn(() => {
return false;
});

const suite = vest.create('suite', () => {
vest.test('test', 'message', debounce(test, 1000));
});

suite();
await wait(1000);
expect(suite.get().hasErrors('test')).toBe(true);
expect(test).toHaveBeenCalledTimes(1);

suite();
suite();
suite();
expect(test).toHaveBeenCalledTimes(1);
await wait(1000);
expect(suite.get().hasErrors('test')).toBe(true);
expect(test).toHaveBeenCalledTimes(2);

suite();
suite();
suite();
expect(test).toHaveBeenCalledTimes(2);
await wait(1000);
});
});

describe('Debounced tests with non-debounced tests', () => {
it('Should complete non-debounced tests immediately', () => {
const test = jest.fn(() => {
return false;
});

const suite = vest.create('suite', () => {
vest.test('test', 'message', debounce(test, 1000));
vest.test('test2', 'message', test);
});

return new Promise<void>(done => {
suite().done(() => {
expect(test).toHaveBeenCalledTimes(2);
expect(suite.get().hasErrors('test')).toBe(true);
expect(suite.get().hasErrors('test2')).toBe(true);
done();
});
expect(test).toHaveBeenCalledTimes(1);
expect(suite.get().hasErrors('test')).toBe(false);
expect(suite.get().hasErrors('test2')).toBe(true);
});
});
});

describe('Multiple debounced fields', () => {
it('Should conclude them on their own time', () => {
const t = jest.fn(() => {
return false;
});

const suite = vest.create('suite', () => {
vest.test('test', 'message', debounce(t, 1000));
vest.test('test2', 'message', debounce(t, 1500));
vest.test('test3', 'message', debounce(t, 2000));
});

const control = jest.fn();

return new Promise<void>(done => {
suite();
suite();
suite()
.done('test', () => {
expect(control).toHaveBeenCalledTimes(0);
expect(t).toHaveBeenCalledTimes(1);
expect(suite.get().hasErrors('test')).toBe(true);
expect(suite.get().hasErrors('test2')).toBe(false);
expect(suite.get().hasErrors('test3')).toBe(false);
control();
})
.done('test2', () => {
expect(control).toHaveBeenCalledTimes(1);
expect(t).toHaveBeenCalledTimes(2);
expect(suite.get().hasErrors('test')).toBe(true);
expect(suite.get().hasErrors('test2')).toBe(true);
expect(suite.get().hasErrors('test3')).toBe(false);
control();
})
.done('test3', () => {
expect(control).toHaveBeenCalledTimes(2);
expect(t).toHaveBeenCalledTimes(3);
expect(suite.get().hasErrors('test')).toBe(true);
expect(suite.get().hasErrors('test2')).toBe(true);
expect(suite.get().hasErrors('test3')).toBe(true);
control();
})
.done(() => {
expect(control).toHaveBeenCalledTimes(3);
expect(t).toHaveBeenCalledTimes(3);
expect(suite.get().hasErrors('test')).toBe(true);
expect(suite.get().hasErrors('test2')).toBe(true);
expect(suite.get().hasErrors('test3')).toBe(true);
done();
});
});
});
});

describe('Test payload', () => {
describe('AbortSignal', () => {
it('Should abort the test when signal is aborted', async () => {
const control = jest.fn();

let run = 0;
const test = jest.fn(async (payload: TestFnPayload) => {
expect(payload.signal.aborted).toBe(false);
await wait(50);
// We should only abort on the first run because
// the second run is canceling the firt, but nothing
// cancels the second.
expect(payload.signal.aborted).toBe(run === 0);
control();
return true;
});

const suite = vest.create('suite', () => {
vest.test('test', 'message', debounce(test, 200));
run++;
});

// eslint-disable-next-line no-async-promise-executor
return new Promise<void>(async done => {
suite();
await wait(200);
// This cancels the first run
suite().done(() => {
expect(suite.hasErrors('test')).toBe(false);
expect(test).toHaveBeenCalledTimes(2);
expect(control).toHaveBeenCalledTimes(1);
done();
});
});
});
});
});
});
Loading
Loading