Skip to content

Commit

Permalink
moving mocks closer to implementation classes. uses barrels to aggreg…
Browse files Browse the repository at this point in the history
…ate mocks, service classes
  • Loading branch information
kwv committed Jun 28, 2016
1 parent f702378 commit 0e027d4
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 26 deletions.
9 changes: 8 additions & 1 deletion app/components/clickerButton/clickerButton.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { beforeEach, beforeEachProviders, describe, expect, it } from '@angular/core/testing';
import { provide } from '@angular/core';
import { asyncCallbackFactory, injectAsyncWrapper, providers, TestUtils } from '../../../test/diExports';
import { ClickersMock } from '../../services/mocks';
import { ClickerButton } from './clickerButton';
import { Clickers } from '../../services';

this.fixture = null;
this.instance = null;

let clickerButtonProviders: Array<any> = [
provide(Clickers, {useClass: ClickersMock}),
];

describe('ClickerButton', () => {

let beforeEachFn: Function = ((testSpec) => {
testSpec.instance['clicker'] = { name: 'TEST CLICKER' };
testSpec.instance['clicker'].getCount = function(): number { return 10; };
});

beforeEachProviders(() => providers);
beforeEachProviders(() => providers.concat(clickerButtonProviders));
beforeEach(injectAsyncWrapper(asyncCallbackFactory(ClickerButton, this, false, beforeEachFn)));

it('initialises', () => {
Expand Down
10 changes: 8 additions & 2 deletions app/components/clickerForm/clickerForm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { beforeEach, beforeEachProviders, describe, expect, it } from '@angular/core/testing';
import { provide } from '@angular/core';
import { asyncCallbackFactory, injectAsyncWrapper, providers, TestUtils } from '../../../test/diExports';
import { Utils } from '../../services/utils';
import { ClickersMock } from '../../services/mocks';
import { Clickers, Utils } from '../../services';
import { ClickerForm } from './clickerForm';

this.fixture = null;
this.instance = null;

let clickerFormProviders: Array<any> = [
provide(Clickers, {useClass: ClickersMock}),
];

describe('ClickerForm', () => {

let beforeEachFn: Function = ((testSpec) => {
spyOn(testSpec.instance, 'newClicker').and.callThrough();
spyOn(testSpec.instance['clickerService'], 'newClicker').and.callThrough();
});

beforeEachProviders(() => providers);
beforeEachProviders(() => providers.concat(clickerFormProviders));
beforeEach(injectAsyncWrapper(asyncCallbackFactory(ClickerForm, this, false, beforeEachFn)));

it('initialises', () => {
Expand Down
3 changes: 1 addition & 2 deletions app/components/clickerForm/clickerForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import { AbstractControl, ControlGroup, FormBuilder, Validators } from '@angular/common';
import { Component } from '@angular/core';
import { Button, Icon, Item, Label, TextInput } from 'ionic-angular';
import { Clickers } from '../../services/clickers';
import { Utils } from '../../services/utils';
import { Clickers, Utils } from '../../services';

@Component({
selector: 'clicker-form',
Expand Down
9 changes: 8 additions & 1 deletion app/pages/clickerList/clickerList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { beforeEach, beforeEachProviders, describe, expect, it } from '@angular/core/testing';
import { provide } from '@angular/core';
import { asyncCallbackFactory, injectAsyncWrapper, providers } from '../../../test/diExports';
import { ClickersMock } from '../../services/mocks';
import { ClickerList } from './clickerList';
import { Clickers } from '../../services';

this.fixture = null;
this.instance = null;

let clickerListProviders: Array<any> = [
provide(Clickers, {useClass: ClickersMock}),
];

describe('ClickerList', () => {

beforeEachProviders(() => providers);
beforeEachProviders(() => providers.concat(clickerListProviders));
beforeEach(injectAsyncWrapper(asyncCallbackFactory(ClickerList, this, true)));

it('initialises', () => {
Expand Down
2 changes: 1 addition & 1 deletion app/pages/clickerList/clickerList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Clickers } from '../../services/clickers';
import { Clickers } from '../../services';
import { ClickerButton } from '../../components/clickerButton/clickerButton';
import { ClickerForm } from '../../components/clickerForm/clickerForm';

Expand Down
16 changes: 16 additions & 0 deletions app/services/clickers.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// CLICKERS:

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

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

export class ClickerMock {
public name: string = 'TEST CLICKER';
public getCount(): number { return 10; };
}
2 changes: 2 additions & 0 deletions app/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './clickers';
export * from './utils';
1 change: 1 addition & 0 deletions app/services/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './clickers.mock';
4 changes: 1 addition & 3 deletions test/diExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/te
import { injectAsync } from '@angular/core/testing';
import { Control } from '@angular/common';
import { App, Config, Form, NavController, Platform } from 'ionic-angular';
import { ClickersMock, ConfigMock, NavMock } from './mocks';
import { ConfigMock, NavMock } from './mocks';
import { Utils } from '../app/services/utils';
import { Clickers } from '../app/services/clickers';
export { TestUtils } from './testUtils';

export let providers: Array<any> = [
Form,
provide(Config, {useClass: ConfigMock}),
provide(Clickers, {useClass: ClickersMock}), // required by ClickerButton
provide(App, {useClass: ConfigMock}), // required by ClickerList
provide(NavController, {useClass: NavMock}), // required by ClickerList
provide(Platform, {useClass: ConfigMock}), // -> IonicApp
Expand Down
16 changes: 0 additions & 16 deletions test/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
// CLICKERS:

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

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

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

// IONIC:

Expand Down

0 comments on commit 0e027d4

Please sign in to comment.