I’m following this unit testing example, referenced on the Ionic blog and I’ve found that the Ionic lifecycle methods (eg. ionViewDidLoad) do not get called when executing these tests. I modified page1 to set some text in ionViewDidLoad and then display that text on the page - a common pattern I use in my app and would like to test. However, the test fails because ionViewDidLoad does not get called. I was expecting either createComponent or detectChanges to invoke the Ionic lifecycle. Am I missing something?
page1.ts
import { Component } from '@angular/core';
import { IonicPage, NavController } from 'ionic-angular';
@IonicPage()
@Component({
selector: 'page-page1',
templateUrl: 'page1.html'
})
export class Page1 {
title: string = 'Ionic did not load';
constructor(public navCtrl: NavController) {
}
ionViewDidLoad(): void {
this.title = 'Ionic loaded...';
}
}
page1.html
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Page One</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<h3>{{ title }}</h3>
<img src="assets/icon/favicon.ico" alt="" />
<p>
If you get lost, the <a href="http://ionicframework.com/docs/v2">docs</a> will show you the way.
</p>
<button ion-button secondary menuToggle>Toggle Menu</button>
</ion-content>
page1.spec.ts
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { Page1 } from './page1';
import { IonicModule, Platform, NavController} from 'ionic-angular/index';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
describe('Page1', () => {
let de: DebugElement;
let comp: Page1;
let fixture: ComponentFixture<Page1>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [Page1],
imports: [
IonicModule.forRoot(Page1)
],
providers: [
NavController
]
});
}));
beforeEach(() => {
fixture = TestBed.createComponent(Page1);
comp = fixture.componentInstance;
de = fixture.debugElement.query(By.css('h3'));
});
it('should create component', () => expect(comp).toBeDefined());
it('should have expected <h3> text', () => {
fixture.detectChanges();
const h3 = de.nativeElement;
expect(h3.innerText).toMatch('Ionic loaded...',
'<h3> should say something about "Ionic"');
});
it('should show the favicon as <img>', () => {
fixture.detectChanges();
const img: HTMLImageElement = fixture.debugElement.query(By.css('img')).nativeElement;
expect(img.src).toContain('assets/icon/favicon.ico');
});
});
output:
Error: Expected ‘Ionic did not load’ to match ‘Ionic loaded…’, ‘
should say something about “Ionic”’.