4

I'm building my first Ionic app and trying hard to follow TDD. I've hit a stumbling block with the Platform.ready promise that Ionic provides. I cannot, for the life of me, figure out how to trigger it while testing. In the Ionic demos it appears in the initializeApp function like this:

initializeApp() {
    this.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.
      this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
}

In a simple test I'm checking to see if the styleDefault method of statusBar has been called, but I have yet to figure out how to trigger platform.ready to resolve. EDIT: Including the whole test file to stave off questions about what is or is not in it.

import { async, TestBed } from '@angular/core/testing';
import { IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { SplashScreenMock as SplashScreen, StatusBarMock as StatusBar, PlatformMock as Platform } from '../../test-config/mocks-ionic';
import { LoginPageMock as LoginPage } from "../../test-config/custom-mocks/login.mock"

describe('App Component', () => {
    let fixture, component, SB;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations:[MyApp, LoginPage],
            imports: [IonicModule.forRoot(MyApp)],
            providers :[StatusBar, SplashScreen, Platform]
        }).compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(MyApp);
        SB = TestBed.get(StatusBar);
        spyOn(SB, 'styleDefault').and.callThrough();
        component = fixture.componentInstance;
    });

    describe('general status before initialization', () => {
        it('should be defined', () => {
        expect(component).toBeDefined();
        });

        it('should be created', () => {
            expect(component instanceof MyApp).toBe(true);
        });

        it('should have a populated pages array', () => {
            expect(component.pages.length).toBeGreaterThan(0);
        });
    });

    describe('general status after initialization', () => {
        it('should style the statusbar when the app is initialized', async((done) => {
            component.initializeApp();
            expect(SB.styleDefault).toHaveBeenCalled();
            done();
        }));
    });
});

I'm probably holding it wrong, or maybe even don't know what I am holding, but at this point I have no idea what is wrong. Any and all options will be considered, and all help appreciated.

NOTE: Yes, I've tried putting in fixture.detectChanges() and/or fixture.autoDetectChanges(true) and I get an error about an unhandled promise rejection and no component factor found for LoginPage. I'm still trying to address that error, but I am not sure if it has anything to do with resolving the promise. If you've got a solution for this little hiccup I'd be happy to see it, too.

MBielski
  • 6,628
  • 3
  • 32
  • 43

1 Answers1

1

Your ready Promise never resolves, you need to get a hold on the platform dependency and force it to resolve:

const plaftorm = TestBed.get(Platform);
spyOn(plaftorm , 'ready').and.callFake(() => Promise.resolve(''));
distante
  • 6,438
  • 6
  • 48
  • 90