UNPKG

shallow-render

Version:

Shallow rendering test utility for Angular

72 lines 3.7 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mockDirective = mockDirective; const core_1 = require("@angular/core"); const reflect_1 = require("./reflect"); const mock_of_directive_1 = require("./mock-of.directive"); const testing_1 = require("@angular/core/testing"); const mock_with_inputs_and_outputs_and_stubs_1 = require("./mock-with-inputs-and-outputs-and-stubs"); const forms_1 = require("@angular/forms"); function mockDirective(directive, config) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const { selector, exportAs, standalone } = reflect_1.reflect.resolveDirective(directive); let MockDirective = class MockDirective extends (0, mock_with_inputs_and_outputs_and_stubs_1.mockWithInputsOutputsAndStubs)(directive, config === null || config === void 0 ? void 0 : config.stubs) { constructor(_viewContainer, _template) { super(); this._viewContainer = _viewContainer; this._template = _template; } ngOnInit() { if (config === null || config === void 0 ? void 0 : config.renderContentsOnInit) { this.renderContents(); } } renderContents() { if (this._viewContainer && this._template) { this._viewContainer.clear(); this._viewContainer.createEmbeddedView(this._template); } } clearContents() { var _a; (_a = this._viewContainer) === null || _a === void 0 ? void 0 : _a.clear(); } }; MockDirective = __decorate([ (0, mock_of_directive_1.MockOf)(directive), (0, core_1.Directive)({ selector: selector || `__${directive.name}-selector`, providers: [ { provide: directive, useExisting: (0, core_1.forwardRef)(() => MockDirective) }, { provide: forms_1.NG_VALUE_ACCESSOR, useClass: forms_1.DefaultValueAccessor, multi: true }, ], exportAs, standalone, }), __param(0, (0, core_1.Inject)(core_1.ViewContainerRef)), __param(0, (0, core_1.Optional)()), __param(1, (0, core_1.Inject)((core_1.TemplateRef))), __param(1, (0, core_1.Optional)()), __metadata("design:paramtypes", [core_1.ViewContainerRef, core_1.TemplateRef]) ], MockDirective); // Provide our mock in place of any other usage of 'thing'. // This makes `ViewChild` and `ContentChildren` selectors work! testing_1.TestBed.overrideDirective(MockDirective, { add: { providers: [{ provide: directive, useExisting: (0, core_1.forwardRef)(() => MockDirective) }] }, }); return MockDirective; } //# sourceMappingURL=mock-directive.js.map