Testing an instance of ToastController

I’m having difficulty unit testing the ToastController.

I have this in my component -

let toast = this._toastCtrl.create( { 
        message: 'LOGIN.ASK_ENABLE_TOUCHID',
	duration: 5000,
	position: 'bottom',
	showCloseButton: true,
	closeButtonText: 'Enable'
} );

toast.present();

toast.onDidDismiss( () => {
	// Some logical stuff happens here that I want to be able to test
} );

So far I am able to spyon the creation of the Toast -

spyOn( comp._toastCtrl, 'create' )
expect( comp._toastCtrl.create ).toHaveBeenCalled();

I am not sure how I test the instance of that? Can anyone point me in the right direction?

Thanks

Hey,

You could return a mockToast that you have visibility of, so you can test if it’s been presented. Couldn’t find a solution to get access to the actual object created by the ToastController inside my login function. At least with this method you can see if the Toast that’s created is presented.

    it('Login() should show a toast', () => {
        let mockToast:Toast = instance.toast.create({
            message: `mockToast`,
            duration: 1,
          });

        spyOn(mockToast,'present').and.callThrough();
        spyOn(instance.toast,'create').and.returnValue(mockToast);
        instance.login(loginResponseMock);
        expect(mockToast.present).toHaveBeenCalled();
    });
1 Like