From 2b308d8b2881ddc561c6326afa3f297e0aaf2576 Mon Sep 17 00:00:00 2001 From: Justin DuJardin Date: Sat, 2 Jan 2016 16:12:12 -0800 Subject: [PATCH] feat(test): add componentSanityCheck helper for simple component sanity - simply runs the component through the instantiation and destroy lifecycle phases. - allows easy basic testing of component init/destroy for components that do not do much or have no behavioral spec yet. - when running on sauce this will become more valuable. - add sanity checks for some missing components. --- test/components/checkbox/checkbox_spec.ts | 6 +++ test/components/input/input_spec.ts | 8 +++ test/components/list/list_spec.ts | 18 +++++++ test/components/switch/switch_spec.ts | 6 +++ test/util.ts | 64 +++++++++++++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 test/components/checkbox/checkbox_spec.ts create mode 100644 test/components/input/input_spec.ts create mode 100644 test/components/list/list_spec.ts create mode 100644 test/components/switch/switch_spec.ts diff --git a/test/components/checkbox/checkbox_spec.ts b/test/components/checkbox/checkbox_spec.ts new file mode 100644 index 00000000..d227e4aa --- /dev/null +++ b/test/components/checkbox/checkbox_spec.ts @@ -0,0 +1,6 @@ +import {componentSanityCheck} from "../../util"; + +export function main() { + componentSanityCheck('Checkbox', 'md-checkbox', ``); +} + diff --git a/test/components/input/input_spec.ts b/test/components/input/input_spec.ts new file mode 100644 index 00000000..c487d6f6 --- /dev/null +++ b/test/components/input/input_spec.ts @@ -0,0 +1,8 @@ +import {componentSanityCheck} from "../../util"; + +export function main() { + let template = ``; + componentSanityCheck('Input Container', 'md-input-container', template); + componentSanityCheck('Input', 'input[md-input]', template); +} + diff --git a/test/components/list/list_spec.ts b/test/components/list/list_spec.ts new file mode 100644 index 00000000..e999f839 --- /dev/null +++ b/test/components/list/list_spec.ts @@ -0,0 +1,18 @@ +import {componentSanityCheck} from "../../util"; + +export function main() { + let template = ` + + + +
+

Title

+

Secondary text

+
+
+
+ `; + componentSanityCheck('List', 'md-list', template); + componentSanityCheck('List Item', 'md-list-item', template); +} + diff --git a/test/components/switch/switch_spec.ts b/test/components/switch/switch_spec.ts new file mode 100644 index 00000000..6520bf72 --- /dev/null +++ b/test/components/switch/switch_spec.ts @@ -0,0 +1,6 @@ +import {componentSanityCheck} from "../../util"; + +export function main() { + componentSanityCheck('Switch', 'md-switch', ``); +} + diff --git a/test/util.ts b/test/util.ts index 6c07db02..8dd334d7 100644 --- a/test/util.ts +++ b/test/util.ts @@ -1,5 +1,23 @@ import {DebugElement} from "angular2/core"; import {DOM} from "angular2/src/platform/dom/dom_adapter"; +import {ComponentFixture} from "angular2/testing"; +import {Component} from "angular2/core"; +import {View} from "angular2/core"; +import { + AsyncTestCompleter, + TestComponentBuilder, + beforeEach, + beforeEachProviders, + describe, + expect, + inject, + it, +} from 'angular2/testing_internal'; +import {MATERIAL_PROVIDERS} from "../ng2-material/all"; +import {provide} from "angular2/core"; +import {UrlResolver} from "angular2/compiler"; +import {TestUrlResolver} from "./test_url_resolver"; +import {MATERIAL_DIRECTIVES} from "../ng2-material/all"; /** Gets a child DebugElement by tag name. */ export function findChildByTag(parent: DebugElement, tagName: string): DebugElement { @@ -34,3 +52,49 @@ export function findChildById(parent: DebugElement, id: string): DebugElement { return debugEl.nativeElement.id.toLowerCase() === id.toLowerCase(); }); } + + +export function componentSanityCheck(name: string, selector:string, template: string) { + @Component({selector: 'test-app'}) + @View({ + directives: [MATERIAL_DIRECTIVES], + template: template + }) + class TestComponent { + } + + describe(name, () => { + let builder: TestComponentBuilder; + + function setup(): Promise { + return builder.createAsync(TestComponent) + .then((fixture: ComponentFixture) => { + fixture.detectChanges(); + return fixture; + }) + .catch(console.error.bind(console)); + } + + beforeEachProviders(() => [ + MATERIAL_PROVIDERS, + provide(UrlResolver, {useValue: new TestUrlResolver()}), + ]); + beforeEach(inject([TestComponentBuilder], (tcb) => { + builder = tcb; + })); + + describe(selector, () => { + it('should instantiate component without fail', inject([AsyncTestCompleter], (async) => { + setup().then(() => async.done()); + })); + it('should destroy component without fail', inject([AsyncTestCompleter], (async) => { + setup().then((api:ComponentFixture) => { + api.destroy(); + async.done(); + }); + })); + }); + + }); + +}