I have the following code to invoke angular2
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { AppModule } from "./src/app";
export function runAngular2App(legacyModel: any) {
platformBrowserDynamic([
{ provide: "legacyModel", useValue: model }
]).bootstrapModule(AppModule)
.then(success => console.log("Ng2 Bootstrap success"))
.catch(err => console.error(err));
}
And somewhere I am invoking it like in this manner -
var legacyModel = {}; // some data
require(["myAngular2App"], function(app) {
app.runAngular2App(legacyModel); // Input to your APP
});
header.component.ts And in my component I use the legacy model -
import { Component, ViewEncapsulation, Inject } from '@angular/core';
@Component({
selector: 'app-header',
encapsulation: ViewEncapsulation.Emulated,
styleUrls: [ './header.less' ],
templateUrl: './header.html'
})
export class HeaderComponent {
public eventTitle;
constructor(@Inject("appModel") private appModel) {
this.eventTitle = this.appModel.get("eventTitle");
}
}
Now the problem is when I am testing this component -
header.component.spec.ts
import {} from 'jasmine';
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { HeaderComponent } from './header.component';
describe('HeaderComponent', () => {
let comp: HeaderComponent;
let fixture: ComponentFixture<HeaderComponent>;
let de: DebugElement;
let el: HTMLElement;
// async beforeEach
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ HeaderComponent ]
})
.compileComponents(); // compile template and css
}));
// synchronous beforeEach
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
comp = fixture.componentInstance; // HeaderComponent test instance
de = fixture.debugElement.query(By.css('.title'));
el = de.nativeElement;
});
it('should display event title', () => {
fixture.detectChanges();
expect(el.textContent).toContain(comp.eventTitle);
});
it('should display a different event title', () => {
comp.eventTitle = "Angular2 moderator dashboard";
fixture.detectChanges();
expect(el.textContent).toContain("Angular2 moderator dashboard");
});
});
I get the following error -
Error: No provider for appModel! in spec.bundle.ts (line 4110)
Since appModel
is not service I am not able to inject it.
How do I inject appModel so my tests can run?