Skip to content

Commit

Permalink
Unify DI boilerplate, closes #103
Browse files Browse the repository at this point in the history
  • Loading branch information
lathonez committed Jun 19, 2016
1 parent 80dd4ff commit 4769372
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 259 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
<a name="1.3.4"></a>
# 1.3.4 (2016-06-19)

### Features

* **Unit Test**: Centralise Testing DI Boilerplate ([#103](https://github.com/lathonez/clicker/issues/103)) ([ff59b33](https://github.com/lathonez/clicker/commit/ff59b33))

<a name="1.3.3"></a>
# 1.3.3 (2016-06-18)

### Features

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


<a name="1.3.2"></a>
# 1.3.2 (2016-06-07)

### Features

* **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)


<a name="1.3.1"></a>
# 1.3.1 (2016-05-25)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ npm run e2e
* [Unit testing walkthrough](http://lathonez.com/2016/ionic-2-unit-testing/)
* [E2E testing walkthrough](http://lathonez.com/2016/ionic-2-e2e-testing/)
* [Removing assets from the APK](http://lathonez.com/2016/cordova-remove-assets/)
* [Unifying DI Boilerplate](http://lathonez.com/2016/unify-di-boilerplate/)

## Contribute
Issues and PRs are welcome, see the [roadmap sticky](https://github.com/lathonez/clicker/issues/38)
Expand Down
79 changes: 18 additions & 61 deletions app/components/clickerButton/clickerButton.spec.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,33 @@
import {
beforeEach,
beforeEachProviders,
describe,
expect,
injectAsync,
it,
} from '@angular/core/testing';
import {
ComponentFixture,
TestComponentBuilder,
} from '@angular/compiler/testing';
import { provide } from '@angular/core';
import { Config } from 'ionic-angular';
import { ClickerButton } from './clickerButton';
import { Clickers } from '../../services/clickers';
import { TestUtils } from '../../../test/testUtils';
import { Utils } from '../../services/utils';
import { beforeEach, beforeEachProviders, describe, expect, it } from '@angular/core/testing';
import { asyncCallbackFactory, injectAsyncWrapper, providers, TestUtils } from '../../../test/diExports';
import { ClickerButton } from './clickerButton';

let clickerButton: ClickerButton = null;
let clickerButtonFixture: ComponentFixture<ClickerButton> = null;

class MockClickers {
public doClick(): boolean {
return true;
}
}

class MockClicker {
public name: string = 'TEST CLICKER';
public getCount(): number { return 10; };
}

class MockClass {
public get(): any {
return {};
}
}
this.fixture = null;
this.instance = null;

describe('ClickerButton', () => {

beforeEachProviders(() => [
provide(Clickers, {useClass: MockClickers}),
provide(Config, {useClass: MockClass}),
]);
let beforeEachFn: Function = ((testSpec) => {
testSpec.instance['clicker'] = { name: 'TEST CLICKER' };
testSpec.instance['clicker'].getCount = function(): number { return 10; };
});

beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb
.createAsync(ClickerButton)
.then((componentFixture: ComponentFixture<ClickerButton>) => {
clickerButtonFixture = componentFixture;
clickerButton = componentFixture.componentInstance;
clickerButton['clicker'] = { name: 'TEST CLICKER' };
clickerButton['clicker'].getCount = function(): number { return 10; };
window['fixture'] = clickerButtonFixture;
window['testUtils'] = TestUtils;
})
.catch(Utils.promiseCatchHandler);
}));
beforeEachProviders(() => providers);
beforeEach(injectAsyncWrapper(asyncCallbackFactory(ClickerButton, this, false, beforeEachFn)));

it('initialises', () => {
expect(clickerButton).not.toBeNull();
expect(this.instance).not.toBeNull();
});

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

it('does a click', () => {
clickerButtonFixture.detectChanges();
spyOn(clickerButton['clickerService'], 'doClick');
TestUtils.eventFire(clickerButtonFixture.nativeElement.querySelectorAll('button')[0], 'click');
expect(clickerButton['clickerService'].doClick).toHaveBeenCalled();
this.fixture.detectChanges();
spyOn(this.instance['clickerService'], 'doClick');
TestUtils.eventFire(this.fixture.nativeElement.querySelectorAll('button')[0], 'click');
expect(this.instance['clickerService'].doClick).toHaveBeenCalled();
});
});
98 changes: 24 additions & 74 deletions app/components/clickerForm/clickerForm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,45 @@
import {
beforeEach,
beforeEachProviders,
describe,
expect,
injectAsync,
it,
} from '@angular/core/testing';
import {
ComponentFixture,
TestComponentBuilder,
} from '@angular/compiler/testing';
import { provide } from '@angular/core';
import {
Config,
Form,
App,
Platform,
} from 'ionic-angular';
import { ClickerForm } from './clickerForm';
import { Clickers } from '../../services/clickers';
import { TestUtils } from '../../../test/testUtils';
import { Utils } from '../../services/utils';
import { beforeEach, beforeEachProviders, describe, expect, it } from '@angular/core/testing';
import { asyncCallbackFactory, injectAsyncWrapper, providers, TestUtils } from '../../../test/diExports';
import { Utils } from '../../services/utils';
import { ClickerForm } from './clickerForm';

let clickerForm: ClickerForm = null;
let clickerFormFixture: ComponentFixture<ClickerForm> = null;

class MockClickers {
public newClicker(): boolean {
return true;
}
}

class MockClass {
public get(): any {
return '';
}

public getBoolean(): boolean {
return true;
}

public getNumber(): number {
return 42;
}
}
this.fixture = null;
this.instance = null;

describe('ClickerForm', () => {

beforeEachProviders(() => [
Form,
provide(Clickers, {useClass: MockClickers}),
provide(App, {useClass: MockClass}),
provide(Platform, {useClass: MockClass}),
provide(Config, {useClass: MockClass}),
]);
let beforeEachFn: Function = ((testSpec) => {
spyOn(testSpec.instance, 'newClicker').and.callThrough();
spyOn(testSpec.instance['clickerService'], 'newClicker').and.callThrough();
});

beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb
.createAsync(ClickerForm)
.then((componentFixture: ComponentFixture<ClickerForm>) => {
clickerFormFixture = componentFixture;
clickerForm = componentFixture.componentInstance;
spyOn(clickerForm, 'newClicker').and.callThrough();
spyOn(clickerForm['clickerService'], 'newClicker').and.callThrough();
})
.catch(Utils.promiseCatchHandler);
}));
beforeEachProviders(() => providers);
beforeEach(injectAsyncWrapper(asyncCallbackFactory(ClickerForm, this, false, beforeEachFn)));

it('initialises', () => {
expect(clickerForm).not.toBeNull();
expect(this.fixture).not.toBeNull();
expect(this.instance).not.toBeNull();
});

it('passes new clicker through to service', () => {
let clickerName: string = 'dave';
let input: any = clickerFormFixture.nativeElement.querySelectorAll('.text-input')[0];
let button: any = clickerFormFixture.nativeElement.querySelectorAll('button')[1];
let input: any = this.fixture.nativeElement.querySelectorAll('.text-input')[0];
let button: any = this.fixture.nativeElement.querySelectorAll('button')[1];
spyOn(Utils, 'resetControl').and.callThrough();
input.value = clickerName;
clickerFormFixture.detectChanges();
clickerForm['clickerNameInput']['updateValue'](clickerName, true);
this.fixture.detectChanges();
this.instance['clickerNameInput']['updateValue'](clickerName, true);
TestUtils.eventFire(input, 'input');
TestUtils.eventFire(button, 'click');
expect(clickerForm.newClicker).toHaveBeenCalledWith(Object({ clickerNameInput: clickerName }));
expect(clickerForm['clickerService'].newClicker).toHaveBeenCalledWith(clickerName);
expect(Utils.resetControl).toHaveBeenCalledWith(clickerForm['clickerNameInput']);
expect(this.instance.newClicker).toHaveBeenCalledWith(Object({ clickerNameInput: clickerName }));
expect(this.instance['clickerService'].newClicker).toHaveBeenCalledWith(clickerName);
expect(Utils.resetControl).toHaveBeenCalledWith(this.instance['clickerNameInput']);
});

it('doesn\'t try to add a clicker with no name', () => {
let button: any = clickerFormFixture.nativeElement.querySelectorAll('button')[1];
let button: any = this.fixture.nativeElement.querySelectorAll('button')[1];
TestUtils.eventFire(button, 'click');
expect(clickerForm.newClicker).toHaveBeenCalled();
expect(clickerForm['clickerService'].newClicker).not.toHaveBeenCalled();
expect(this.instance.newClicker).toHaveBeenCalled();
expect(this.instance['clickerService'].newClicker).not.toHaveBeenCalled();
});
});
69 changes: 9 additions & 60 deletions app/pages/clickerList/clickerList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,17 @@
import {
beforeEach,
beforeEachProviders,
describe,
expect,
injectAsync,
it,
} from '@angular/core/testing';
import {
ComponentFixture,
TestComponentBuilder,
} from '@angular/compiler/testing';
import { provide } from '@angular/core';
import { ClickerList } from './clickerList';
import { Utils } from '../../services/utils';
import {
Config,
Form,
App,
NavController,
NavParams,
Platform,
} from 'ionic-angular';
import { beforeEach, beforeEachProviders, describe, expect, it } from '@angular/core/testing';
import { asyncCallbackFactory, injectAsyncWrapper, providers } from '../../../test/diExports';
import { ClickerList } from './clickerList';

class MockClass {
public get(): any {
return '';
}

public getBoolean(): boolean {
return true;
}

public getNumber(): number {
return 42;
}
}

let clickerList: ClickerList = null;
let clickerListFixture: ComponentFixture<ClickerList> = null;
this.fixture = null;
this.instance = null;

describe('ClickerList', () => {

beforeEachProviders(() => [
Form,
provide(NavController, {useClass: MockClass}),
provide(NavParams, {useClass: MockClass}),
provide(Config, {useClass: MockClass}),
provide(App, {useClass: MockClass}),
provide(Platform, {useClass: MockClass}),
]);

beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb
.createAsync(ClickerList)
.then((componentFixture: ComponentFixture<ClickerList>) => {
clickerListFixture = componentFixture;
clickerList = componentFixture.componentInstance;
clickerListFixture.detectChanges();
})
.catch(Utils.promiseCatchHandler);
}));
beforeEachProviders(() => providers);
beforeEach(injectAsyncWrapper(asyncCallbackFactory(ClickerList, this, true)));

it('initialises', () => {
expect(clickerList).not.toBeNull();
expect(clickerListFixture).not.toBeNull();
expect(this.instance).not.toBeNull();
expect(this.fixture).not.toBeNull();
});
});

0 comments on commit 4769372

Please sign in to comment.