UNPKG

gauge-ts

Version:
74 lines (73 loc) 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HookRegistry = void 0; const __1 = require(".."); const HookType_1 = require("./HookType"); class HookRegistry { _hooks; constructor() { this._hooks = new Map([ [HookType_1.HookType.BeforeSuite, new Array()], [HookType_1.HookType.BeforeSpec, new Array()], [HookType_1.HookType.BeforeScenario, new Array()], [HookType_1.HookType.BeforeStep, new Array()], [HookType_1.HookType.AfterSuite, new Array()], [HookType_1.HookType.AfterSpec, new Array()], [HookType_1.HookType.AfterScenario, new Array()], [HookType_1.HookType.AfterStep, new Array()], ]); } addHook(type, method) { this._hooks.get(type).push(method); } get(type, tags) { const hooks = this._hooks.get(type); if (!hooks || !hooks.length) { return []; } if (!tags.length) { return hooks.filter((hook) => { return hook.getTags().length === 0; }); } return hooks.filter((hook) => { const hookTags = hook.getTags(); const operator = hook.getTagAggregationOperator(); if (!hookTags.length) { return true; } const matched = HookRegistry.hasIntersection(tags, hookTags); switch (operator) { case __1.Operator.And: return matched === hookTags.length; case __1.Operator.Or: return matched > 0; } }); } setInstanceForMethodsIn(file, instance) { for (const hookMethods of this._hooks.values()) { for (const hookMethod of hookMethods) { if (hookMethod.getFilePath() === file) { hookMethod.setInstance(instance); } } } } clear() { this._hooks.forEach((v, k) => { this._hooks.set(k, new Array()); }); } static hasIntersection(tags, hookTags) { return tags.filter((t) => { return hookTags.includes(t); }).length; } } exports.HookRegistry = HookRegistry; if (!global.gaugeHookRegistry) { global.gaugeHookRegistry = new HookRegistry(); } const hookRegistry = global.gaugeHookRegistry; exports.default = hookRegistry;