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.
96 lines (95 loc) • 4.45 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InputMatcher = 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 InputMatcher extends matcher_1.Matcher {
constructor(state) {
super(state);
}
/**
* Expect the input of the selected element to equal the provided value.
*
* input="value"
*
* @param value - The value to compare with the input value of the selected element.
*/
toEqual(value) {
this.setDescription(`equal '${value}'`, this.getDescriptionModifier());
this.setMatcher(() => {
this.checkElementHasProperty(this.state.inputSelector);
const element = this.getElement();
const input = ng_helper_1.NgHelper.getProperty(element, this.state.inputSelector);
return [(0, es6_1.default)(input, value), input, value];
});
return this;
}
/**
* Expect the input of the selected element to be bound to the provided property of the fixture's component.\
* NOTE: Does not work for getters.
*
* [input]="property"
*
* @param property - The property of the fixture's component that should be bounded to the input of the selected element.
*/
toBeBoundTo(property) {
this.setDescription(`be bound to '${property}'`, this.getDescriptionModifier());
this.setMatcher((payload = "binding") => {
this.checkComponentHasProperty(property);
this.checkElementHasProperty(this.state.inputSelector);
const component = this.getComponent();
const element = this.getElement();
// TODO: What to do if the property is a getter?
// Getters can now only be checked using isEqual.
object_helper_1.ObjectHelper.setProperty(component, property, payload);
this.getFixture().detectChanges();
const input = ng_helper_1.NgHelper.getProperty(element, this.state.inputSelector);
const componentValue = object_helper_1.ObjectHelper.getProperty(component, property);
return [(0, es6_1.default)(input, componentValue), input, componentValue];
});
return new modify_with_matcher_1.ModifyWithMatcher(Object.assign({}, this.state));
}
/**
* Expect the input of the selected element to call the provided function of the fixture's component.
* Also checks if the return value of the function is assigned to the input.
*
* [input]="func()"
*
* @param func - The function of the fixture's component that should be called by the input of the selected element.
*/
toCall(func) {
this.setDescription(`call '${func}'`, this.getDescriptionModifier());
this.setMatcher(() => {
this.checkComponentHasProperty(func);
this.checkElementHasProperty(this.state.inputSelector);
const component = this.getComponent();
const element = this.getElement();
let hasBeenCalled = false;
let callValues;
object_helper_1.ObjectHelper.replaceFunction(component, func, (...args) => {
hasBeenCalled = true;
callValues = args.length ? args : types_1.NOTHING;
return "binding";
});
this.getFixture().detectChanges();
object_helper_1.ObjectHelper.restoreFunction(component, func);
// TODO: Maybe split this into a separate matcher, but probably not necessary.
const input = ng_helper_1.NgHelper.getProperty(element, this.state.inputSelector);
return [hasBeenCalled && input === "binding", callValues, types_1.NOTHING];
});
return new to_call_with_matcher_1.ToCallWithMatcher(Object.assign({}, this.state));
}
// TODO: toCallThrough
getDescriptionModifier() {
return `input '${this.state.inputSelector}'`;
}
}
exports.InputMatcher = InputMatcher;