|
1 |
| -import { |
2 |
| - beforeEach, |
3 |
| - beforeEachProviders, |
4 |
| - describe, |
5 |
| - expect, |
6 |
| - injectAsync, |
7 |
| - it, |
8 |
| -} from '@angular/core/testing'; |
9 |
| -import { |
10 |
| - ComponentFixture, |
11 |
| - TestComponentBuilder, |
12 |
| -} from '@angular/compiler/testing'; |
13 |
| -import { provide } from '@angular/core'; |
14 |
| -import { |
15 |
| - Config, |
16 |
| - Form, |
17 |
| - App, |
18 |
| - Platform, |
19 |
| -} from 'ionic-angular'; |
20 |
| -import { ClickerForm } from './clickerForm'; |
21 |
| -import { Clickers } from '../../services/clickers'; |
22 |
| -import { TestUtils } from '../../../test/testUtils'; |
23 |
| -import { Utils } from '../../services/utils'; |
| 1 | +import { beforeEach, beforeEachProviders, describe, expect, it } from '@angular/core/testing'; |
| 2 | +import { asyncCallbackFactory, injectAsyncWrapper, providers, TestUtils } from '../../../test/diExports'; |
| 3 | +import { Utils } from '../../services/utils'; |
| 4 | +import { ClickerForm } from './clickerForm'; |
24 | 5 |
|
25 |
| -let clickerForm: ClickerForm = null; |
26 |
| -let clickerFormFixture: ComponentFixture<ClickerForm> = null; |
27 |
| - |
28 |
| -class MockClickers { |
29 |
| - public newClicker(): boolean { |
30 |
| - return true; |
31 |
| - } |
32 |
| -} |
33 |
| - |
34 |
| -class MockClass { |
35 |
| - public get(): any { |
36 |
| - return ''; |
37 |
| - } |
38 |
| - |
39 |
| - public getBoolean(): boolean { |
40 |
| - return true; |
41 |
| - } |
42 |
| - |
43 |
| - public getNumber(): number { |
44 |
| - return 42; |
45 |
| - } |
46 |
| -} |
| 6 | +this.fixture = null; |
| 7 | +this.instance = null; |
47 | 8 |
|
48 | 9 | describe('ClickerForm', () => {
|
49 | 10 |
|
50 |
| - beforeEachProviders(() => [ |
51 |
| - Form, |
52 |
| - provide(Clickers, {useClass: MockClickers}), |
53 |
| - provide(App, {useClass: MockClass}), |
54 |
| - provide(Platform, {useClass: MockClass}), |
55 |
| - provide(Config, {useClass: MockClass}), |
56 |
| - ]); |
| 11 | + let beforeEachFn: Function = ((testSpec) => { |
| 12 | + spyOn(testSpec.instance, 'newClicker').and.callThrough(); |
| 13 | + spyOn(testSpec.instance['clickerService'], 'newClicker').and.callThrough(); |
| 14 | + }); |
57 | 15 |
|
58 |
| - beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => { |
59 |
| - return tcb |
60 |
| - .createAsync(ClickerForm) |
61 |
| - .then((componentFixture: ComponentFixture<ClickerForm>) => { |
62 |
| - clickerFormFixture = componentFixture; |
63 |
| - clickerForm = componentFixture.componentInstance; |
64 |
| - spyOn(clickerForm, 'newClicker').and.callThrough(); |
65 |
| - spyOn(clickerForm['clickerService'], 'newClicker').and.callThrough(); |
66 |
| - }) |
67 |
| - .catch(Utils.promiseCatchHandler); |
68 |
| - })); |
| 16 | + beforeEachProviders(() => providers); |
| 17 | + beforeEach(injectAsyncWrapper(asyncCallbackFactory(ClickerForm, this, false, beforeEachFn))); |
69 | 18 |
|
70 | 19 | it('initialises', () => {
|
71 |
| - expect(clickerForm).not.toBeNull(); |
| 20 | + expect(this.fixture).not.toBeNull(); |
| 21 | + expect(this.instance).not.toBeNull(); |
72 | 22 | });
|
73 | 23 |
|
74 | 24 | it('passes new clicker through to service', () => {
|
75 | 25 | let clickerName: string = 'dave';
|
76 |
| - let input: any = clickerFormFixture.nativeElement.querySelectorAll('.text-input')[0]; |
77 |
| - let button: any = clickerFormFixture.nativeElement.querySelectorAll('button')[1]; |
| 26 | + let input: any = this.fixture.nativeElement.querySelectorAll('.text-input')[0]; |
| 27 | + let button: any = this.fixture.nativeElement.querySelectorAll('button')[1]; |
78 | 28 | spyOn(Utils, 'resetControl').and.callThrough();
|
79 | 29 | input.value = clickerName;
|
80 |
| - clickerFormFixture.detectChanges(); |
81 |
| - clickerForm['clickerNameInput']['updateValue'](clickerName, true); |
| 30 | + this.fixture.detectChanges(); |
| 31 | + this.instance['clickerNameInput']['updateValue'](clickerName, true); |
82 | 32 | TestUtils.eventFire(input, 'input');
|
83 | 33 | TestUtils.eventFire(button, 'click');
|
84 |
| - expect(clickerForm.newClicker).toHaveBeenCalledWith(Object({ clickerNameInput: clickerName })); |
85 |
| - expect(clickerForm['clickerService'].newClicker).toHaveBeenCalledWith(clickerName); |
86 |
| - expect(Utils.resetControl).toHaveBeenCalledWith(clickerForm['clickerNameInput']); |
| 34 | + expect(this.instance.newClicker).toHaveBeenCalledWith(Object({ clickerNameInput: clickerName })); |
| 35 | + expect(this.instance['clickerService'].newClicker).toHaveBeenCalledWith(clickerName); |
| 36 | + expect(Utils.resetControl).toHaveBeenCalledWith(this.instance['clickerNameInput']); |
87 | 37 | });
|
88 | 38 |
|
89 | 39 | it('doesn\'t try to add a clicker with no name', () => {
|
90 |
| - let button: any = clickerFormFixture.nativeElement.querySelectorAll('button')[1]; |
| 40 | + let button: any = this.fixture.nativeElement.querySelectorAll('button')[1]; |
91 | 41 | TestUtils.eventFire(button, 'click');
|
92 |
| - expect(clickerForm.newClicker).toHaveBeenCalled(); |
93 |
| - expect(clickerForm['clickerService'].newClicker).not.toHaveBeenCalled(); |
| 42 | + expect(this.instance.newClicker).toHaveBeenCalled(); |
| 43 | + expect(this.instance['clickerService'].newClicker).not.toHaveBeenCalled(); |
94 | 44 | });
|
95 | 45 | });
|
0 commit comments