I have trouble passing the unit test that I wrote for testing whether the root page is set correctly. The issue is that comp.rootPage
is always undefined
when the test executes. I know this is because that the test executes even before the root page is set in the component’s constructor, but I’m not sure how to force the test to wait until the callback of platform.ready()
finishes executing.
This is what the constructor of my root component looks like:
// 1. during testing, constructor is entered first
constructor(platform: Platform, storage: NativeStorageWrapper) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
storage.getItem('app_first_open').then(
data => this.rootPage = data ? LocationPage : MenuPage,
error => this.rootPage = LocationPage
);
});
}
And the unit test:
// 2. then the expect
line is executed without even executing the body of the ‘then’ in the constructor
it('should open location confirmation page for the 1st time', async(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(comp.rootPage).toBe(LocationPage);
});
}));