Skip to content

Commit 4769372

Browse files
committed
Unify DI boilerplate, closes #103
1 parent 80dd4ff commit 4769372

File tree

10 files changed

+172
-259
lines changed

10 files changed

+172
-259
lines changed

CHANGELOG.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
<a name="1.3.4"></a>
2+
# 1.3.4 (2016-06-19)
3+
4+
### Features
5+
6+
* **Unit Test**: Centralise Testing DI Boilerplate ([#103](https://github.com/lathonez/clicker/issues/103)) ([ff59b33](https://github.com/lathonez/clicker/commit/ff59b33))
7+
18
<a name="1.3.3"></a>
29
# 1.3.3 (2016-06-18)
310

411
### Features
512

613
* **Update**: Update to Ionic 2.0.0.beta.9 PR ([a61432c])(https://github.com/lathonez/clicker/commit/a61432c)
714

8-
915
<a name="1.3.2"></a>
1016
# 1.3.2 (2016-06-07)
1117

1218
### Features
1319

1420
* **Update**: Update to Ionic 2.0.0.beta.8 PR [#98](https://github.com/lathonez/clicker/pull/98) ([5f8d5fb])(https://github.com/lathonez/clicker/commit/5f8d5fb20f456d2e8b98d9db1098c51588e6568e)
1521

16-
1722
<a name="1.3.1"></a>
1823
# 1.3.1 (2016-05-25)
1924

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ npm run e2e
3838
* [Unit testing walkthrough](http://lathonez.com/2016/ionic-2-unit-testing/)
3939
* [E2E testing walkthrough](http://lathonez.com/2016/ionic-2-e2e-testing/)
4040
* [Removing assets from the APK](http://lathonez.com/2016/cordova-remove-assets/)
41+
* [Unifying DI Boilerplate](http://lathonez.com/2016/unify-di-boilerplate/)
4142

4243
## Contribute
4344
Issues and PRs are welcome, see the [roadmap sticky](https://github.com/lathonez/clicker/issues/38)
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,33 @@
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 { Config } from 'ionic-angular';
15-
import { ClickerButton } from './clickerButton';
16-
import { Clickers } from '../../services/clickers';
17-
import { TestUtils } from '../../../test/testUtils';
18-
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 { ClickerButton } from './clickerButton';
194

20-
let clickerButton: ClickerButton = null;
21-
let clickerButtonFixture: ComponentFixture<ClickerButton> = null;
22-
23-
class MockClickers {
24-
public doClick(): boolean {
25-
return true;
26-
}
27-
}
28-
29-
class MockClicker {
30-
public name: string = 'TEST CLICKER';
31-
public getCount(): number { return 10; };
32-
}
33-
34-
class MockClass {
35-
public get(): any {
36-
return {};
37-
}
38-
}
5+
this.fixture = null;
6+
this.instance = null;
397

408
describe('ClickerButton', () => {
419

42-
beforeEachProviders(() => [
43-
provide(Clickers, {useClass: MockClickers}),
44-
provide(Config, {useClass: MockClass}),
45-
]);
10+
let beforeEachFn: Function = ((testSpec) => {
11+
testSpec.instance['clicker'] = { name: 'TEST CLICKER' };
12+
testSpec.instance['clicker'].getCount = function(): number { return 10; };
13+
});
4614

47-
beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
48-
return tcb
49-
.createAsync(ClickerButton)
50-
.then((componentFixture: ComponentFixture<ClickerButton>) => {
51-
clickerButtonFixture = componentFixture;
52-
clickerButton = componentFixture.componentInstance;
53-
clickerButton['clicker'] = { name: 'TEST CLICKER' };
54-
clickerButton['clicker'].getCount = function(): number { return 10; };
55-
window['fixture'] = clickerButtonFixture;
56-
window['testUtils'] = TestUtils;
57-
})
58-
.catch(Utils.promiseCatchHandler);
59-
}));
15+
beforeEachProviders(() => providers);
16+
beforeEach(injectAsyncWrapper(asyncCallbackFactory(ClickerButton, this, false, beforeEachFn)));
6017

6118
it('initialises', () => {
62-
expect(clickerButton).not.toBeNull();
19+
expect(this.instance).not.toBeNull();
6320
});
6421

6522
it('displays the clicker name and count', () => {
66-
clickerButtonFixture.detectChanges();
67-
expect(clickerButtonFixture.nativeElement.querySelectorAll('.button-inner')[0].innerHTML).toEqual('TEST CLICKER (10)');
23+
this.fixture.detectChanges();
24+
expect(this.fixture.nativeElement.querySelectorAll('.button-inner')[0].innerHTML).toEqual('TEST CLICKER (10)');
6825
});
6926

7027
it('does a click', () => {
71-
clickerButtonFixture.detectChanges();
72-
spyOn(clickerButton['clickerService'], 'doClick');
73-
TestUtils.eventFire(clickerButtonFixture.nativeElement.querySelectorAll('button')[0], 'click');
74-
expect(clickerButton['clickerService'].doClick).toHaveBeenCalled();
28+
this.fixture.detectChanges();
29+
spyOn(this.instance['clickerService'], 'doClick');
30+
TestUtils.eventFire(this.fixture.nativeElement.querySelectorAll('button')[0], 'click');
31+
expect(this.instance['clickerService'].doClick).toHaveBeenCalled();
7532
});
7633
});
+24-74
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,45 @@
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';
245

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;
478

489
describe('ClickerForm', () => {
4910

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+
});
5715

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)));
6918

7019
it('initialises', () => {
71-
expect(clickerForm).not.toBeNull();
20+
expect(this.fixture).not.toBeNull();
21+
expect(this.instance).not.toBeNull();
7222
});
7323

7424
it('passes new clicker through to service', () => {
7525
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];
7828
spyOn(Utils, 'resetControl').and.callThrough();
7929
input.value = clickerName;
80-
clickerFormFixture.detectChanges();
81-
clickerForm['clickerNameInput']['updateValue'](clickerName, true);
30+
this.fixture.detectChanges();
31+
this.instance['clickerNameInput']['updateValue'](clickerName, true);
8232
TestUtils.eventFire(input, 'input');
8333
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']);
8737
});
8838

8939
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];
9141
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();
9444
});
9545
});
+9-60
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,17 @@
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 { ClickerList } from './clickerList';
15-
import { Utils } from '../../services/utils';
16-
import {
17-
Config,
18-
Form,
19-
App,
20-
NavController,
21-
NavParams,
22-
Platform,
23-
} from 'ionic-angular';
1+
import { beforeEach, beforeEachProviders, describe, expect, it } from '@angular/core/testing';
2+
import { asyncCallbackFactory, injectAsyncWrapper, providers } from '../../../test/diExports';
3+
import { ClickerList } from './clickerList';
244

25-
class MockClass {
26-
public get(): any {
27-
return '';
28-
}
29-
30-
public getBoolean(): boolean {
31-
return true;
32-
}
33-
34-
public getNumber(): number {
35-
return 42;
36-
}
37-
}
38-
39-
let clickerList: ClickerList = null;
40-
let clickerListFixture: ComponentFixture<ClickerList> = null;
5+
this.fixture = null;
6+
this.instance = null;
417

428
describe('ClickerList', () => {
439

44-
beforeEachProviders(() => [
45-
Form,
46-
provide(NavController, {useClass: MockClass}),
47-
provide(NavParams, {useClass: MockClass}),
48-
provide(Config, {useClass: MockClass}),
49-
provide(App, {useClass: MockClass}),
50-
provide(Platform, {useClass: MockClass}),
51-
]);
52-
53-
beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
54-
return tcb
55-
.createAsync(ClickerList)
56-
.then((componentFixture: ComponentFixture<ClickerList>) => {
57-
clickerListFixture = componentFixture;
58-
clickerList = componentFixture.componentInstance;
59-
clickerListFixture.detectChanges();
60-
})
61-
.catch(Utils.promiseCatchHandler);
62-
}));
10+
beforeEachProviders(() => providers);
11+
beforeEach(injectAsyncWrapper(asyncCallbackFactory(ClickerList, this, true)));
6312

6413
it('initialises', () => {
65-
expect(clickerList).not.toBeNull();
66-
expect(clickerListFixture).not.toBeNull();
14+
expect(this.instance).not.toBeNull();
15+
expect(this.fixture).not.toBeNull();
6716
});
6817
});

0 commit comments

Comments
 (0)