forked from oktadev/ionic-jhipster-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.component.spec.ts
62 lines (51 loc) · 1.96 KB
/
app.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { TestBed, async } from '@angular/core/testing';
import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import Mock = jest.Mock;
import { TranslateModule } from '@ngx-translate/core';
describe('AppComponent', () => {
let statusBarSpy, splashScreenSpy, platformReadySpy, platformSpy;
beforeEach(async(() => {
statusBarSpy = createSpyObj('StatusBar', ['styleDefault']);
splashScreenSpy = createSpyObj('SplashScreen', ['hide']);
platformReadySpy = Promise.resolve();
platformSpy = createSpyObj('Platform', [{ready: platformReadySpy}]);
TestBed.configureTestingModule({
declarations: [AppComponent],
imports: [TranslateModule.forRoot()],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [
{ provide: StatusBar, useValue: statusBarSpy },
{ provide: SplashScreen, useValue: splashScreenSpy },
{ provide: Platform, useValue: platformSpy }
]
}).compileComponents();
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
});
it('should initialize the app', async () => {
TestBed.createComponent(AppComponent);
expect(platformSpy.ready).toHaveBeenCalled();
await platformReadySpy;
expect(statusBarSpy.styleDefault).toHaveBeenCalled();
expect(splashScreenSpy.hide).toHaveBeenCalled();
});
// TODO: add more tests!
});
export const createSpyObj = (baseName, methodNames): { [key: string]: Mock<any> } => {
const obj: any = {};
for (const m of methodNames) {
if (typeof m === 'string') {
obj[m] = jest.fn();
} else {
obj[Object.keys(m)[0]] = jest.fn().mockImplementation(() => Object.values(m)[0]);
}
}
return obj;
};