Skip to content

Commit

Permalink
Type checking tests after move to sources. This closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
lathonez committed Feb 24, 2016
1 parent 3f150c6 commit 3d3650f
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 96 deletions.
37 changes: 20 additions & 17 deletions app/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
import { TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS} from 'angular2/platform/testing/browser';
import { setBaseTestProviders } from 'angular2/testing';
import { IonicApp, Platform } from 'ionic-framework/ionic';
import { ClickerApp } from '../app/app';
import { ClickerApp } from './app';

// this needs doing _once_ for the entire test suite, hence it's here
setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);

let clickerApp = null;
let clickerApp: ClickerApp = null;

function getComponentStub(name) {
let component = {
setRoot: function() { return true; },
close: function(root) { return true; },
function getComponentStub(name: string): any {
'use strict';

let component: Object = {
setRoot: function(): boolean { return true; },
close: function(root: any): boolean { return true; },
};
return component;
}

export function main() {
export function main(): void {
'use strict';

describe('ClickerApp', () => {

beforeEach(function() {
let ionicApp = new IonicApp(null, null, null);
let platform = new Platform();
beforeEach(() => {
let ionicApp: IonicApp = new IonicApp(null, null, null);
let platform: Platform = new Platform();
clickerApp = new ClickerApp(ionicApp, platform);
});

it('initialises with two possible pages', () => {
expect(clickerApp.pages.length).toEqual(2);
expect(clickerApp['pages'].length).toEqual(2);
});

it('initialises with a root page', () => {
expect(clickerApp.rootPage).not.toBe(null);
expect(clickerApp['rootPage']).not.toBe(null);
});

it('initialises with an app', () => {
expect(clickerApp.app).not.toBe(null);
expect(clickerApp['app']).not.toBe(null);
});

it('opens a page', () => {
spyOn(clickerApp.app, 'getComponent').and.callFake(getComponentStub);
clickerApp.openPage(clickerApp.pages[1]);
expect(clickerApp.app.getComponent).toHaveBeenCalledWith('leftMenu');
expect(clickerApp.app.getComponent).toHaveBeenCalledWith('nav');
spyOn(clickerApp['app'], 'getComponent').and.callFake(getComponentStub);
clickerApp.openPage(clickerApp['pages'][1]);
expect(clickerApp['app'].getComponent).toHaveBeenCalledWith('leftMenu');
expect(clickerApp['app'].getComponent).toHaveBeenCalledWith('nav');
});
});
}
31 changes: 15 additions & 16 deletions app/components/clickerButton/clickerButton.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import {
} from 'angular2/testing';
import { provide } from 'angular2/core';
import { Config } from 'ionic-framework/ionic';
import { ClickerButton } from '../../../app/components/clickerButton/clickerButton';
import { Clickers } from '../../../app/services/clickers';
import { TestUtils } from '../../testUtils';
import { Utils } from '../../../app/services/utils';
import { ClickerButton } from './clickerButton';
import { Clickers } from '../../services/clickers';
import { TestUtils } from '../../../test/testUtils';
import { Utils } from '../../services/utils';

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

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

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

class MockClass {
Expand All @@ -35,7 +35,8 @@ class MockClass {
}
}

export function main() {
export function main(): void {
'use strict';

describe('ClickerForm', () => {

Expand All @@ -50,10 +51,8 @@ export function main() {
.then((componentFixture: ComponentFixture) => {
clickerButtonFixture = componentFixture;
clickerButton = componentFixture.componentInstance;
clickerButton.clicker = {
name: 'TEST CLICKER'
};
clickerButton.clicker.getCount = function() { return 10; };
clickerButton['clicker'] = { name: 'TEST CLICKER' };
clickerButton['clicker'].getCount = function(): number { return 10; };
window['fixture'] = clickerButtonFixture;
window['testUtils'] = TestUtils;
})
Expand All @@ -71,9 +70,9 @@ export function main() {

it('does a click', () => {
clickerButtonFixture.detectChanges();
spyOn(clickerButton.clickerService, 'doClick');
spyOn(clickerButton['clickerService'], 'doClick');
TestUtils.eventFire(clickerButtonFixture.nativeElement.querySelectorAll('button')[0], 'click');
expect(clickerButton.clickerService.doClick).toHaveBeenCalled();
expect(clickerButton['clickerService'].doClick).toHaveBeenCalled();
});
});
}
25 changes: 13 additions & 12 deletions app/components/clickerForm/clickerForm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { ClickerForm } from '../../../app/components/clickerForm/clickerForm';
import { Clickers } from '../../../app/services/clickers';
import { FormBuilder } from 'angular2/common';
import { Utils } from '../../../app/services/utils';
import { ClickerForm } from './clickerForm';
import { Clickers } from '../../services/clickers';
import { Utils } from '../../services/utils';

let clickerForm = null;
let clickerForm: ClickerForm = null;

let mockClickers = Object.create(Clickers);
let mockClickers: Clickers = Object.create(Clickers);

mockClickers.newClicker = function() { return true; };
mockClickers.newClicker = function(): string { return 'dave'; };

export function main() {
export function main(): void {
'use strict';

describe('ClickerForm', () => {

beforeEach(function() {
beforeEach(() => {
clickerForm = new ClickerForm(mockClickers, new FormBuilder());
spyOn(clickerForm, 'newClicker').and.callThrough();
spyOn(mockClickers, 'newClicker').and.callThrough();
Expand All @@ -24,17 +25,17 @@ export function main() {
});

it('passes new clicker through to service', () => {
let clickerName = 'dave';
let clickerName: string = 'dave';
spyOn(Utils, 'resetControl').and.callThrough();
clickerForm.clickerNameInput.updateValue(clickerName, true);
clickerForm['clickerNameInput']['updateValue'](clickerName, true);
clickerForm.newClicker({clickerNameInput: clickerName});
expect(clickerForm.newClicker).toHaveBeenCalledWith(Object({ clickerNameInput: clickerName }));
expect(mockClickers.newClicker).toHaveBeenCalledWith(clickerName);
expect(Utils.resetControl).toHaveBeenCalledWith(clickerForm.clickerNameInput);
expect(Utils.resetControl).toHaveBeenCalledWith(clickerForm['clickerNameInput']);
});

it('doesn\'t try to add a clicker with no name', () => {
let rtn = clickerForm.newClicker();
let rtn: boolean = clickerForm.newClicker('dave');
expect(rtn).toBe(false);
expect(clickerForm.newClicker).toHaveBeenCalled();
expect(mockClickers.newClicker).not.toHaveBeenCalled();
Expand Down
17 changes: 9 additions & 8 deletions app/models/click.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
'use strict';

import { Click } from '../../app/models/click';
import { Click } from './click';

export function main() {
export function main(): void {
'use strict';

describe('Click', () => {

it('initialises with defaults', () => {
let click = new Click();
let click: Click = new Click();

// toString() prints out something like "Thu Jan 07 2016 14:05:14 GMT+1300 (NZDT)"
// comparing millis directly sometimes fails test (as it will be one milli too late!)
let currentDateString = new Date().toString();
let defaultDateString = new Date(click.getTime()).toString();
let currentDateString: string = new Date().toString();
let defaultDateString: string = new Date(click.getTime()).toString();

expect(currentDateString).toEqual(defaultDateString);
expect(click.getLocation()).toEqual('TODO');
});

it('initialises with overrides', () => {
let current = new Date().getTime();
let location = 'MY LOCATION';
let click = new Click(current, location);
let current: number = new Date().getTime();
let location: string = 'MY LOCATION';
let click: Click = new Click(current, location);
expect(click.getTime()).toEqual(current);
expect(click.getLocation()).toEqual(location);
});
Expand Down
8 changes: 5 additions & 3 deletions app/models/clicker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict';

import { Clicker } from '../../app/models/clicker';
import { Clicker } from './clicker';

export function main(): void {
'use strict';

export function main() {
describe('Clicker', () => {

it('initialises with the correct name', () => {
let clicker = new Clicker('12434', 'testClicker');
let clicker: Clicker = new Clicker('12434', 'testClicker');
expect(clicker.getName()).toEqual('testClicker');
});
});
Expand Down
9 changes: 5 additions & 4 deletions app/pages/clickerList/clickerList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ClickerList } from '../../../app/pages/clickerList/clickerList';
import { ClickerList } from './clickerList';

let clickerList = null;
let clickerList: ClickerList = null;

export function main() {
export function main(): void {
'use strict';

describe('ClickerList', () => {

beforeEach(function() {
beforeEach(() => {
clickerList = new ClickerList(null, null);
});

Expand Down

0 comments on commit 3d3650f

Please sign in to comment.