From a5fef5197d48f8be3d6d7771bdcc996950fcb66b Mon Sep 17 00:00:00 2001 From: Stephen Hazleton Date: Sun, 30 Oct 2016 17:04:29 +1100 Subject: [PATCH] Move boilerplate to beforeEachCompiler --- .../clickerButton/clickerButton.spec.ts | 21 +++++++--------- .../clickerForm/clickerForm.spec.ts | 25 ++++++++----------- src/pages/clickerList/clickerList.spec.ts | 19 ++++++-------- src/pages/page2/page2.spec.ts | 18 ++++++------- src/test.ts | 11 ++++++++ 5 files changed, 46 insertions(+), 48 deletions(-) diff --git a/src/components/clickerButton/clickerButton.spec.ts b/src/components/clickerButton/clickerButton.spec.ts index f14df92..3e75c1e 100644 --- a/src/components/clickerButton/clickerButton.spec.ts +++ b/src/components/clickerButton/clickerButton.spec.ts @@ -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 = 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(); diff --git a/src/components/clickerForm/clickerForm.spec.ts b/src/components/clickerForm/clickerForm.spec.ts index c87940a..7e3a66a 100644 --- a/src/components/clickerForm/clickerForm.spec.ts +++ b/src/components/clickerForm/clickerForm.spec.ts @@ -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 = 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(); diff --git a/src/pages/clickerList/clickerList.spec.ts b/src/pages/clickerList/clickerList.spec.ts index d6da601..12f7afe 100644 --- a/src/pages/clickerList/clickerList.spec.ts +++ b/src/pages/clickerList/clickerList.spec.ts @@ -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 = 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(); diff --git a/src/pages/page2/page2.spec.ts b/src/pages/page2/page2.spec.ts index 0dbd274..32b21fa 100644 --- a/src/pages/page2/page2.spec.ts +++ b/src/pages/page2/page2.spec.ts @@ -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 = 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(); diff --git a/src/test.ts b/src/test.ts index 5447a9a..45da939 100644 --- a/src/test.ts +++ b/src/test.ts @@ -41,6 +41,17 @@ Promise.all([ export class TestUtils { + public static beforeEachCompiler(components: Array): 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): typeof TestBed { return TestBed.configureTestingModule({ declarations: [