Skip to content
This repository has been archived by the owner on Feb 2, 2019. It is now read-only.

Commit

Permalink
feat(test): add componentSanityCheck helper for simple component sanity
Browse files Browse the repository at this point in the history
 - 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.
  • Loading branch information
justindujardin committed Jan 3, 2016
1 parent f829bd8 commit 2b308d8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/components/checkbox/checkbox_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {componentSanityCheck} from "../../util";

export function main() {
componentSanityCheck('Checkbox', 'md-checkbox', `<md-checkbox checked="true"></md-checkbox>`);
}

8 changes: 8 additions & 0 deletions test/components/input/input_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {componentSanityCheck} from "../../util";

export function main() {
let template = `<md-input-container><input md-input type="text"></md-input-container>`;
componentSanityCheck('Input Container', 'md-input-container', template);
componentSanityCheck('Input', 'input[md-input]', template);
}

18 changes: 18 additions & 0 deletions test/components/list/list_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {componentSanityCheck} from "../../util";

export function main() {
let template = `
<md-list>
<md-list-item class="md-2-line">
<img/>
<div class="md-list-item-text">
<h3>Title</h3>
<p>Secondary text</p>
</div>
</md-list-item>
</md-list>
`;
componentSanityCheck('List', 'md-list', template);
componentSanityCheck('List Item', 'md-list-item', template);
}

6 changes: 6 additions & 0 deletions test/components/switch/switch_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {componentSanityCheck} from "../../util";

export function main() {
componentSanityCheck('Switch', 'md-switch', `<md-switch checked="true"></md-switch>`);
}

64 changes: 64 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<any> {
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();
});
}));
});

});

}

0 comments on commit 2b308d8

Please sign in to comment.