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.
108 lines (107 loc) • 4.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Matcher = void 0;
const object_inspect_1 = __importDefault(require("object-inspect"));
const ng_helper_1 = require("../helpers/ng.helper");
const object_helper_1 = require("../helpers/object.helper");
const types_1 = require("../types");
class Matcher {
constructor(state) {
this.state = state;
}
get not() {
this.state.negate = true;
return this;
}
build() {
return [
this.state.description,
() => this.execute()
];
}
execute() {
if (!this.state.matcher) {
return this.throwError("Please choose a matcher first");
}
const result = this.state.matcher();
const [matcherResult, matcherReceived, matcherExpected] = Array.isArray(result) ? result : [result, types_1.NOTHING, types_1.NOTHING];
if ((!this.state.negate && !matcherResult) || (this.state.negate && matcherResult)) {
this.throwError(this.state.errorDescription, matcherReceived, matcherExpected);
}
// Dummy expect to keep Jest / Jasmine happy.
expect(true).toEqual(true);
}
setDescription(description, modifier) {
this.state.description = `'${this.state.selector}'${modifier ? ` ${modifier}` : ""} ${this.state.negate ? "should not" : "should"} ${description}`;
this.state.errorDescription = `Expected '${this.state.selector}'${modifier ? ` ${modifier}` : ""} ${this.state.negate ? "not to" : "to"} ${description}`;
}
appendDescription(description) {
this.state.description += ` ${description}`;
this.state.errorDescription += ` ${description}`;
}
setMatcher(matcher) {
this.state.matcher = matcher;
}
getFixture() {
const fixture = this.state.getFixture();
if (!fixture) {
return this.throwError("Could not get fixture, please initialize Piumino first");
}
return fixture;
}
getComponent() {
const fixture = this.getFixture();
// Support for ngMocks.
// https://ng-mocks.sudo.eu/api/MockRender#example-with-a-component
if (fixture.point) {
return fixture.point.componentInstance;
}
return fixture.componentInstance;
}
checkComponentHasProperty(property) {
const component = this.getComponent();
if (!object_helper_1.ObjectHelper.hasProperty(component, property)) {
this.throwError(`Property '${property}' does not exist on '${component.constructor.name}'`);
}
}
getElement(throwError = true) {
const fixture = this.getFixture();
fixture.detectChanges();
// TODO: support selector as DebugElement and/or HTMLElement.
const element = fixture.debugElement.query(el => { var _a, _b; return (_b = (_a = el.nativeElement) === null || _a === void 0 ? void 0 : _a.matches) === null || _b === void 0 ? void 0 : _b.call(_a, this.state.selector); });
if (throwError && !element) {
return this.throwError(`Could not find element with selector '${this.state.selector}'`);
}
return element;
}
checkElementHasProperty(property) {
const element = this.getElement();
if (!ng_helper_1.NgHelper.hasProperty(element, property)) {
this.throwError(`Property '${property}' does not exist on '${element.name}''`);
}
}
throwError(message, received = types_1.NOTHING, expected = types_1.NOTHING) {
let errorMessage = message;
if (received !== types_1.NOTHING || expected !== types_1.NOTHING) {
errorMessage += `\n\n\x1b[31mReceived: ${this.stringifyValue(received)}\x1b[0m`;
errorMessage += `\n\n\x1b[32mExpected: ${this.stringifyValue(expected)}\x1b[0m`;
}
throw new types_1.PiuminoError(errorMessage, this.state.errorStack);
}
stringifyValue(value) {
if (value === types_1.NOTHING) {
return "";
}
if (typeof value === "string") {
return `'${value}'`;
}
if (object_helper_1.ObjectHelper.isObject(value)) {
return `\n${(0, object_inspect_1.default)(value, { indent: 2 })}`;
}
return `${value}`;
}
}
exports.Matcher = Matcher;