UNPKG

piumino

Version:

Piumino, meaning duvet in Italian, is like a duvet over the bed called Angular TestBed. It provides test helpers to test trivial things like inputs and outputs with a single line.

93 lines (92 loc) 4.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OutputMatcher = void 0; const es6_1 = __importDefault(require("fast-deep-equal/es6")); const ng_helper_1 = require("../helpers/ng.helper"); const object_helper_1 = require("../helpers/object.helper"); const types_1 = require("../types"); const matcher_1 = require("./matcher"); const modify_with_matcher_1 = require("./modify-with.matcher"); const to_call_with_matcher_1 = require("./to-call-with.matcher"); class OutputMatcher extends matcher_1.Matcher { constructor(state) { super(state); } /** * Expect the output of the selected element to be bound to the provided property of the fixture's component.\ * NOTE: Does not work for setters if there is no getter. * * (output)="property = $event" * * @param property - The property of the fixture's component that should be bounded to the output of the selected element. */ toBeBoundTo(property) { this.setDescription(`be bound to '${property}'`, this.getDescriptionModifier()); this.setMatcher((payload = "binding") => { this.checkComponentHasProperty(property); this.dispatchEvent(payload); // TODO: What to do if the property is a setter without a getter? const component = this.getComponent(); const componentValue = object_helper_1.ObjectHelper.getProperty(component, property); return [(0, es6_1.default)(componentValue, payload), componentValue, payload]; }); return new modify_with_matcher_1.ModifyWithMatcher(Object.assign({}, this.state)); } /** * Expect the output of the selected element to call the provided function of the fixture's component. * * (output)="func()" * * @param func - The function of the fixture's component that should be called by the output of the selected element. */ toCall(func) { this.setDescription(`call '${func}'`, this.getDescriptionModifier()); this.setMatcher((payload = types_1.NOTHING) => { this.checkComponentHasProperty(func); const component = this.getComponent(); let hasBeenCalled = false; let callValues; object_helper_1.ObjectHelper.replaceFunction(component, func, (...args) => { hasBeenCalled = true; callValues = args.length ? args : types_1.NOTHING; }); this.dispatchEvent(payload); object_helper_1.ObjectHelper.restoreFunction(component, func); return [hasBeenCalled, callValues, types_1.NOTHING]; }); return new to_call_with_matcher_1.ToCallWithMatcher(Object.assign({}, this.state)); } // TODO: toCallThrough getDescriptionModifier() { return `output '${this.state.outputSelector}'`; } dispatchEvent(payload = types_1.NOTHING) { const element = this.getElement(); const output = ng_helper_1.NgHelper.getProperty(element, this.state.outputSelector); if (output === null || output === void 0 ? void 0 : output.emit) { if (payload === types_1.NOTHING) { output.emit(); } else { output.emit(payload); } } else { this.dispatchNativeEvent(element, payload); } } dispatchNativeEvent(element, payload) { const [outputSelector, eventType] = this.state.outputSelector.split("."); if (payload instanceof Event) { return element.nativeElement.dispatchEvent(payload); } if (["keydown", "keyup"].includes(outputSelector)) { return element.nativeElement.dispatchEvent(new KeyboardEvent(outputSelector, { key: eventType })); } return element.nativeElement.dispatchEvent(new Event(outputSelector)); } } exports.OutputMatcher = OutputMatcher;