Skip to content

Commit

Permalink
Move boilerplate to beforeEachCompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
lathonez committed Oct 30, 2016
1 parent 0e4318c commit a5fef51
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 48 deletions.
21 changes: 9 additions & 12 deletions src/components/clickerButton/clickerButton.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { TestUtils } from '../../test';
import { ClickerButton } from './clickerButton';
import { ClickerMock } from '../../models/clicker.mock';
import { ComponentFixture, async } from '@angular/core/testing';
import { TestUtils } from '../../test';
import { ClickerButton } from './clickerButton';
import { ClickerMock } from '../../models/clicker.mock';

let fixture: ComponentFixture<ClickerButton> = null;
let instance: any = null;

describe('ClickerButton', () => {

beforeEach(async(() => {
return TestUtils.configureIonicTestingModule([ClickerButton])
.compileComponents().then(() => {
fixture = TestBed.createComponent(ClickerButton);
instance = fixture.debugElement.componentInstance;
instance.clicker = new ClickerMock();
});
}));
beforeEach(async(() => TestUtils.beforeEachCompiler([ClickerButton]).then(compiled => {
fixture = compiled.fixture;
instance = compiled.instance;
instance.clicker = new ClickerMock();
})));

afterEach(() => {
fixture.destroy();
Expand Down
25 changes: 11 additions & 14 deletions src/components/clickerForm/clickerForm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { FormBuilder } from '@angular/forms';
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { TestUtils } from '../../test';
import { ClickerForm } from './clickerForm';
import { FormBuilder } from '@angular/forms';
import { ComponentFixture, async } from '@angular/core/testing';
import { TestUtils } from '../../test';
import { ClickerForm } from './clickerForm';

let fixture: ComponentFixture<ClickerForm> = null;
let instance: any = null;

describe('ClickerForm', () => {

beforeEach(async(() => {
return TestUtils.configureIonicTestingModule([ClickerForm])
.compileComponents().then(() => {
fixture = TestBed.createComponent(ClickerForm);
instance = fixture.debugElement.componentInstance;
instance.clicker = { name: 'TEST CLICKER' };
instance.clicker.getCount = function(): number { return 10; };
fixture.autoDetectChanges(true);
});
}));
beforeEach(async(() => TestUtils.beforeEachCompiler([ClickerForm]).then(compiled => {
fixture = compiled.fixture;
instance = compiled.instance;
instance.clicker = { name: 'TEST CLICKER' };
instance.clicker.getCount = function(): number { return 10; };
fixture.autoDetectChanges(true);
})));

afterEach(() => {
fixture.destroy();
Expand Down
19 changes: 8 additions & 11 deletions src/pages/clickerList/clickerList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { TestUtils } from '../../test';
import { ClickerList } from './clickerList';
import { ClickerButton, ClickerForm } from '../../components';
import { ComponentFixture, async } from '@angular/core/testing';
import { TestUtils } from '../../test';
import { ClickerList } from './clickerList';
import { ClickerButton, ClickerForm } from '../../components';

let fixture: ComponentFixture<ClickerList> = null;
let instance: any = null;

describe('ClickerList', () => {

beforeEach(async(() => {
return TestUtils.configureIonicTestingModule([ClickerList, ClickerForm, ClickerButton])
.compileComponents().then(() => {
fixture = TestBed.createComponent(ClickerList);
instance = fixture.debugElement.componentInstance;
});
}));
beforeEach(async(() => TestUtils.beforeEachCompiler([ClickerList, ClickerForm, ClickerButton]).then(compiled => {
fixture = compiled.fixture;
instance = compiled.instance;
})));

afterEach(() => {
fixture.destroy();
Expand Down
18 changes: 7 additions & 11 deletions src/pages/page2/page2.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { TestUtils } from '../../test';
import { Page2 } from './page2';
import { ComponentFixture, async } from '@angular/core/testing';
import { TestUtils } from '../../test';
import { Page2 } from './page2';

let fixture: ComponentFixture<Page2> = null;
let instance: any = null;

describe('Pages: Page2', () => {

beforeEach(async(() => {
return TestUtils.configureIonicTestingModule([Page2])
.compileComponents().then(() => {
fixture = TestBed.createComponent(Page2);
instance = fixture.debugElement.componentInstance;
fixture.detectChanges();
});
}));
beforeEach(async(() => TestUtils.beforeEachCompiler([Page2]).then(compiled => {
fixture = compiled.fixture;
instance = compiled.instance;
})));

afterEach(() => {
fixture.destroy();
Expand Down
11 changes: 11 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ Promise.all([

export class TestUtils {

public static beforeEachCompiler(components: Array<any>): Promise<{fixture: any, instance: any}> {
return TestUtils.configureIonicTestingModule(components)
.compileComponents().then(() => {
let fixture: any = TestBed.createComponent(components[0]);
return {
fixture: fixture,
instance: fixture.debugElement.componentInstance,
};
});
}

public static configureIonicTestingModule(components: Array<any>): typeof TestBed {
return TestBed.configureTestingModule({
declarations: [
Expand Down

0 comments on commit a5fef51

Please sign in to comment.