soda-test
Version:
Package for Unit and API tests
339 lines (338 loc) • 13.1 kB
JavaScript
//
// Since TestBed cannot be loaded in Node, and soda-test need to work in Node and Angular,
// we redefine all the types.
// types that were defined as classes in angular/core we redefine as interfaces
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewEncapsulation = exports.ChangeDetectionStrategy = exports.InjectFlags = exports.createComponent = exports.addEvents = exports.createFixture = exports.component = exports.fixture = exports.getInitTestBedFunction = exports.TestBed = void 0;
const events_1 = require("events");
const testInfo_1 = require("./testInfo");
function getTestBed() {
// @angular/core/testing is the only libraray the exports "getTestBed"
for (const key of Object.keys(require.cache)) {
const getTestBed = require.cache[key].exports.getTestBed;
if (getTestBed) {
return getTestBed();
}
}
}
function Component(obj) {
if (AngularCore) {
return AngularCore.Component(obj);
}
return () => { };
}
function getAngularCore() {
// @angular/core is the only libraray the exports "Component"
for (const key of Object.keys(require.cache)) {
if (require.cache[key].exports.Component) {
return require.cache[key].exports;
}
}
return null;
}
exports.TestBed = getTestBed();
const AngularCore = getAngularCore();
function getInitTestBedFunction() {
/*
if ( TestBed ) {
const _testing = require('@angular/platform-browser-dynamic/testing')
return function(): void {
TestBed.initTestEnvironment(_testing.BrowserDynamicTestingModule, _testing.platformBrowserDynamicTesting())
}
}
*/
return null;
}
exports.getInitTestBedFunction = getInitTestBedFunction;
function fixture(component, options) {
return (target, propertyKey, parameterIndex) => {
if (options === null || options === void 0 ? void 0 : options.events) {
addEvents(...options.events);
}
(0, testInfo_1.getInfo)(target).addSinon(propertyKey, parameterIndex, {
caller: null,
target: { component, options },
method: null,
memberMethod: null,
kind: testInfo_1.SinonKind.Fixture,
context: null
});
};
}
exports.fixture = fixture;
function component(component) {
return (target, propertyKey, parameterIndex) => {
(0, testInfo_1.getInfo)(target).addSinon(propertyKey, parameterIndex, {
caller: null,
target: component,
method: null,
memberMethod: null,
kind: testInfo_1.SinonKind.Component,
context: null
});
};
}
exports.component = component;
function getSelector(component) {
var _a;
if (!component)
return null;
const annotations = component['__annotations__'];
if (!annotations || !Array.isArray(annotations) || annotations.length === 0)
return null;
return (_a = annotations[0]) === null || _a === void 0 ? void 0 : _a.selector;
}
function getModuleDef(component, options, thc) {
var _a;
const moduleDef = {
declarations: (_a = options === null || options === void 0 ? void 0 : options.declarations) !== null && _a !== void 0 ? _a : [],
imports: options === null || options === void 0 ? void 0 : options.imports
};
if (moduleDef.declarations.indexOf(component) < 0)
moduleDef.declarations.push(component);
moduleDef.declarations.push(thc);
return moduleDef;
}
class SodaFixtureWrapper {
constructor(fixtureWrapper, selector) {
this.fixtureWrapper = fixtureWrapper;
if (!By)
By = RetriveByMethod();
this.ngZone = fixtureWrapper.ngZone;
this.debugElement = fixtureWrapper.debugElement.query(By.css(selector));
this.componentInstance = this.debugElement.componentInstance;
this.nativeElement = this.debugElement.nativeElement;
}
detectChanges(checkNoChanges) {
this.fixtureWrapper.detectChanges(checkNoChanges);
}
checkNoChanges() {
this.fixtureWrapper.checkNoChanges();
}
autoDetectChanges(autoDetect) {
this.fixtureWrapper.autoDetectChanges(autoDetect);
}
isStable() {
return this.fixtureWrapper.isStable();
}
whenStable() {
return this.fixtureWrapper.whenStable();
}
whenRenderingDone() {
return this.fixtureWrapper.whenRenderingDone();
}
destroy() {
this.fixtureWrapper.destroy();
}
queryByCss(selector) {
return this.debugElement.query.by.css(selector);
}
get inputs() {
return this.fixtureWrapper.componentInstance.inputs;
}
get events() {
return this.fixtureWrapper.componentInstance.events;
}
}
let lastCreatedFixture;
let lastComponentType;
function createFixture(component, options) {
if (exports.TestBed) {
const selector = getSelector(component);
if (!selector)
throw new Error(`cannot create Fixture for type ${component.name}`);
let TestHostComponent = class TestHostComponent {
constructor() {
this.inputs = {};
this.events = new events_1.EventEmitter();
}
eventCalled(name, data) {
this.events.emit(name, data);
}
};
TestHostComponent = Reflect.decorate([
Component({
selector: `host-component`,
template: createTemplate(selector, options)
})
], TestHostComponent);
exports.TestBed.configureTestingModule(getModuleDef(component, options, TestHostComponent));
const outerfixture = exports.TestBed.createComponent(TestHostComponent);
const fixture = new SodaFixtureWrapper(outerfixture, selector);
lastCreatedFixture = fixture;
lastComponentType = component;
fillFixtureMethods(fixture);
fixture['restore'] = () => {
if (lastCreatedFixture === fixture) {
lastCreatedFixture = null;
lastComponentType = null;
}
};
fixture.detectChanges();
return fixture;
}
}
exports.createFixture = createFixture;
function createTemplate(selector, options) {
let template = `<${selector}`;
if (options) {
if (options.inputs) {
for (const input of options.inputs) {
template += ` [${input}]="inputs['${input}']"`;
}
}
if (options.outputs) {
for (const output of options.outputs) {
template += ` (${output})="eventCalled('${output}', $event)"`;
}
}
}
template += `></${selector}>`;
return template;
}
let By = null;
function RetriveByMethod() {
for (const libName in require.cache) {
const lib = require.cache[libName];
if (lib.exports.By && lib.exports.By.css && lib.exports.By.directive) {
return lib.exports.By;
}
}
}
const eventNames = ['click', 'input', 'ngModelChange'];
function addEvents(...names) {
for (const eventName of names) {
if (eventNames.indexOf(eventName) < 0) {
eventNames.push(eventName);
}
}
}
exports.addEvents = addEvents;
function fillFixtureMethods(fixture) {
// fill debugElement methods
const DebugElementPrototype = Object.getPrototypeOf(fixture.debugElement);
if (!DebugElementPrototype.query.by) {
if (!By)
By = RetriveByMethod();
const _query = DebugElementPrototype.query;
Object.defineProperty(DebugElementPrototype, 'query', {
get: function () {
_query.by = {
css: (selector) => {
return this.query(By.css(selector));
}
};
return _query;
}
});
const _queryAll = DebugElementPrototype.queryAll;
Object.defineProperty(DebugElementPrototype, 'queryAll', {
get: function () {
_queryAll.by = {
css: (selector) => {
return this.queryAll(By.css(selector));
}
};
return _queryAll;
}
});
const _queryAllNodes = DebugElementPrototype.queryAllNodes;
Object.defineProperty(DebugElementPrototype, 'queryAllNodes', {
get: function () {
_queryAllNodes.by = {
all: () => {
return this.queryAllNodes(By.all());
},
directive: (type) => {
return this.queryAllNodes(By.directive(type));
}
};
return _queryAllNodes;
}
});
const attributesDiscriptor = Object.getOwnPropertyDescriptor(DebugElementPrototype, 'attributes');
Object.defineProperty(DebugElementPrototype, 'attributes', {
configurable: true,
enumerable: true,
get: function () {
const attrs = attributesDiscriptor.get.bind(this)();
for (const key of Object.keys(attrs)) {
if (key.startsWith('ng-reflect-')) {
const key1 = key === 'ng-reflect-model' ? 'ngModel' : key.substring(11);
Object.defineProperty(attrs, key1, {
configurable: true,
enumerable: true,
get: function () { return this[key]; }
});
}
}
return attrs;
}
});
Object.defineProperty(DebugElementPrototype, 'text', {
get: function () {
return this.attributes.ngModel || this.nativeElement.innerText;
},
set: function (value) {
this.triggerEventHandler.ngModelChange(value);
}
});
}
const triggerEventHandlerDescriptor = Object.getOwnPropertyDescriptor(DebugElementPrototype, 'triggerEventHandler');
if (!triggerEventHandlerDescriptor.get) {
const _triggerEventHandler = DebugElementPrototype.triggerEventHandler;
Object.defineProperty(DebugElementPrototype, 'triggerEventHandler', {
get: function () {
for (const eventName of eventNames) {
_triggerEventHandler[eventName] = (objEvent) => {
this.triggerEventHandler(eventName, objEvent);
};
}
return _triggerEventHandler;
}
});
}
}
function createComponent(component) {
if (exports.TestBed) {
let fixture;
if (lastComponentType === component) {
// using existing fixture
fixture = lastCreatedFixture;
}
else {
// creating temporaray fixture
fixture = exports.TestBed.createComponent(component);
}
return fixture.componentInstance;
}
}
exports.createComponent = createComponent;
var InjectFlags;
(function (InjectFlags) {
InjectFlags[InjectFlags["Default"] = 0] = "Default";
InjectFlags[InjectFlags["Host"] = 1] = "Host";
InjectFlags[InjectFlags["Self"] = 2] = "Self";
InjectFlags[InjectFlags["SkipSelf"] = 4] = "SkipSelf";
InjectFlags[InjectFlags["Optional"] = 8] = "Optional";
})(InjectFlags = exports.InjectFlags || (exports.InjectFlags = {}));
var ChangeDetectionStrategy;
(function (ChangeDetectionStrategy) {
ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";
})(ChangeDetectionStrategy = exports.ChangeDetectionStrategy || (exports.ChangeDetectionStrategy = {}));
var ViewEncapsulation;
(function (ViewEncapsulation) {
ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated";
ViewEncapsulation[ViewEncapsulation["None"] = 2] = "None";
ViewEncapsulation[ViewEncapsulation["ShadowDom"] = 3] = "ShadowDom";
})(ViewEncapsulation = exports.ViewEncapsulation || (exports.ViewEncapsulation = {}));
var MissingTranslationStrategy;
(function (MissingTranslationStrategy) {
MissingTranslationStrategy[MissingTranslationStrategy["Error"] = 0] = "Error";
MissingTranslationStrategy[MissingTranslationStrategy["Warning"] = 1] = "Warning";
MissingTranslationStrategy[MissingTranslationStrategy["Ignore"] = 2] = "Ignore";
})(MissingTranslationStrategy || (MissingTranslationStrategy = {}));
//# sourceMappingURL=testbed.js.map
;