Ionic 2: Checking Bugs with Karma / Jasmine / TestBed

I am new to Ionic2 and I followed this tutorial and a simple test like

describe('Dummy test', () => {

it('should do nothing', () => {

    expect(true).toBeTruthy();
    expect(1 + 1).toBe(2);

});

});

      

works great, but for some reason I keep getting this error when I try to follow the rest of the tutorial.

Component: Root Component
✖ initialises with a root page of LoginPage
  Firefox 45.0.0 (Linux 0.0.0)
TypeError: win is undefined in src/test.ts (line 937)

      

My src / test.ts is the same as the tutorial and there is no win whatsoever. My app.spec.ts is

import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { IonicModule } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { UserData } from '../providers/user-data';
import { LoginPage } from '../pages/login/login';
import { Platform } from 'ionic-angular';
import { MyApp } from './app.component';
import { LoginPage } from '../pages/login/login';

let comp: MyApp;
let fixture: ComponentFixture<MyApp>;

describe('Component: Root Component', () => {

beforeEach(async(() => {

    TestBed.configureTestingModule({

        declarations: [MyApp],

        providers: [
            StatusBar,
            SplashScreen,
            UserData,
            Platform
        ],

        imports: [
            IonicModule.forRoot(MyApp)
        ]

    }).compileComponents();

}));

beforeEach(() => {

    fixture = TestBed.createComponent(MyApp);
    comp    = fixture.componentInstance;

});

afterEach(() => {
    fixture.destroy();
    comp = null;
});

it('initialises with a root page of LoginPage', () => {
    expect(comp['rootPage']).toBe(LoginPage);
});

});

      

And my app.component.ts is this

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { MenuSidePage } from '../pages/menu-side/menu-side';
import { LoginPage } from '../pages/login/login';
import { UserData } from '../providers/user-data';


@Component({
  template: `<ion-nav #nav [root]="rootPage"></ion-nav>`
})
export class MyApp {

  rootPage: any;

  constructor(
    public platform: Platform,
    public statusBar: StatusBar,
    public splashScreen: SplashScreen,
    private userData: UserData,
  ) {
    platform
      .ready()
      .then(() => {
        //First - check if user is logged
        if(this.userData.currentUser) { 
          this.rootPage = MenuSidePage;
        } else {
          this.rootPage = LoginPage;
        }
        statusBar.styleDefault();
        splashScreen.hide();

    });
  }
}

      

+3


source to share


2 answers


I don't have a solution yet, but you shouldn't use compileComponents()

because you are using template

, not templateUrl

as this tutorial says :

"We need to use compileComponents when we need to compile a component asynchronously, for example, one that has an external template (the one that is loaded via templateUrl and not tied to the template). That is why the beforeEach block that is executed by this in code uses the async parameter - it sets up an asynchronous test zone for compiling compiler components internally. "



Hope it helps :)

0


source


The function win()

comes from Plaftorm

, you should mock it like this:

export class PlatformMock {
  public ready(): Promise<string> {
    return new Promise((resolve) => {
      resolve('READY');
    });
  }

  public getQueryParam() {
    return true;
  }

  public registerBackButtonAction(fn: Function, priority?: number): Function {
    return (() => true);
  }

  public hasFocus(ele: HTMLElement): boolean {
    return true;
  }

  public doc(): HTMLDocument {
    return document;
  }

  public is(): boolean {
    return true;
  }

  public getElementComputedStyle(container: any): any {
    return {
      paddingLeft: '10',
      paddingTop: '10',
      paddingRight: '10',
      paddingBottom: '10',
    };
  }

  public onResize(callback: any) {
    return callback;
  }

  public registerListener(ele: any, eventName: string, callback: any): Function {
    return (() => true);
  }

  public win(): Window {
    return window;
  }

  public raf(callback: any): number {
    return 1;
  }

  public timeout(callback: any, timer: number): any {
    return setTimeout(callback, timer);
  }

  public cancelTimeout(id: any) {
    // do nothing
  }

  public getActiveElement(): any {
    return document['activeElement'];
  }
}

      



There is a link here to see a project for real integration of this mock class.

Hope this helps :)

0


source







All Articles