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

Introduce CustomActions feature (closes #1535) #7393

Merged
merged 15 commits into from
Dec 14, 2022
Merged
17 changes: 17 additions & 0 deletions src/api/test-controller/add-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import addRenderedWarning from '../../notifications/add-rendered-warning';
import TestRun from '../../test-run';
import TestCafeErrorList from '../../errors/error-list';

export function addWarnings (callsiteSet: Set<Record<string, any>>, message: string, testRun: TestRun): void {
callsiteSet.forEach(callsite => {
addRenderedWarning(testRun.warningLog, message, callsite);
callsiteSet.delete(callsite);
});
}

export function addErrors (callsiteSet: Set<Record<string, any>>, ErrorClass: any, errList: TestCafeErrorList): void {
callsiteSet.forEach(callsite => {
errList.addError(new ErrorClass(callsite));
callsiteSet.delete(callsite);
});
}
2 changes: 1 addition & 1 deletion src/api/test-controller/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class Assertion {
message = void 0;
}

return this._testController._enqueueCommand(command, {
return this._testController.enqueueCommand(command, {
assertionType: command.methodName,
actual: this._actual,
expected: assertionArgs.expected,
Expand Down
47 changes: 47 additions & 0 deletions src/api/test-controller/custom-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getCallsiteForMethod } from '../../errors/get-callsite';
import { RunCustomActionCommand } from '../../test-run/commands/actions';
import { delegateAPI } from '../../utils/delegated-api';
import { Dictionary } from '../../configuration/interfaces';
import TestController from './index';
import delegatedAPI from './delegated-api';

export default class CustomActions {
private _testController: TestController;
private readonly _customActions: Dictionary<Function>;

constructor (testController: TestController, customActions: Dictionary<Function>) {
this._testController = testController;
this._customActions = customActions || {};

this._registerCustomActions();
}

_registerCustomActions (): void {
Object.entries(this._customActions).forEach(([ name, fn ]) => {
// @ts-ignore
this[delegatedAPI(name)] = (...args) => {
const callsite = getCallsiteForMethod(name) || void 0;

return this._testController.enqueueCommand(RunCustomActionCommand, { fn, args, name }, this._validateCommand, callsite);
};
});

this._delegateAPI(this._customActions);
}

_validateCommand (): boolean {
return true;
}

_delegateAPI (actions: Dictionary<Function>): void {
const customActionsList = Object.entries(actions).map(([name]) => {
return {
srcProp: delegatedAPI(name),
apiProp: name,
accessor: '',
};
});

delegateAPI(this, customActionsList, { useCurrentCtxAsHandler: true });
}
}
3 changes: 3 additions & 0 deletions src/api/test-controller/delegated-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function delegatedAPI (methodName: string, accessor = ''): string {
return `_${ methodName }$${ accessor }`;
}
2 changes: 1 addition & 1 deletion src/api/test-controller/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class TestController {
public constructor (testRun: TestRun | TestRunProxy);
public testRun: TestRun;
public warningLog: WarningLog;
public _enqueueCommand (CmdCtor: unknown, cmdArgs: object, validateCommand: Function): () => Promise<unknown>;
public enqueueCommand (CmdCtor: unknown, cmdArgs: object, validateCommand: Function, callsite?: CallsiteRecord): () => Promise<unknown>;
public checkForExcessiveAwaits (checkedCallsite: CallsiteRecord, { actionId }: CommandBase): void;
public static enableDebugForNonDebugCommands (): void;
public static disableDebugForNonDebugCommands (): void;
Expand Down
Loading