|
| 1 | +import { getCallsiteForMethod } from '../../errors/get-callsite'; |
| 2 | +import { RunCustomActionCommand } from '../../test-run/commands/actions'; |
| 3 | +import { delegateAPI } from '../../utils/delegated-api'; |
| 4 | +import { Dictionary } from '../../configuration/interfaces'; |
| 5 | +import TestController from './index'; |
| 6 | +import delegatedAPI from './delegated-api'; |
| 7 | + |
| 8 | +export default class CustomActions { |
| 9 | + private _testController: TestController; |
| 10 | + private readonly _customActions: Dictionary<Function>; |
| 11 | + |
| 12 | + constructor (testController: TestController, customActions: Dictionary<Function>) { |
| 13 | + this._testController = testController; |
| 14 | + this._customActions = customActions || {}; |
| 15 | + |
| 16 | + this._registerCustomActions(); |
| 17 | + } |
| 18 | + |
| 19 | + _registerCustomActions (): void { |
| 20 | + Object.entries(this._customActions).forEach(([ name, fn ]) => { |
| 21 | + // @ts-ignore |
| 22 | + this[delegatedAPI(name)] = (...args) => { |
| 23 | + const callsite = getCallsiteForMethod(name) || void 0; |
| 24 | + |
| 25 | + return this._testController.enqueueCommand(RunCustomActionCommand, { fn, args, name }, this._validateCommand, callsite); |
| 26 | + }; |
| 27 | + }); |
| 28 | + |
| 29 | + this._delegateAPI(this._customActions); |
| 30 | + } |
| 31 | + |
| 32 | + _validateCommand (): boolean { |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + _delegateAPI (actions: Dictionary<Function>): void { |
| 37 | + const customActionsList = Object.entries(actions).map(([name]) => { |
| 38 | + return { |
| 39 | + srcProp: delegatedAPI(name), |
| 40 | + apiProp: name, |
| 41 | + accessor: '', |
| 42 | + }; |
| 43 | + }); |
| 44 | + |
| 45 | + delegateAPI(this, customActionsList, { useCurrentCtxAsHandler: true }); |
| 46 | + } |
| 47 | +} |
0 commit comments