Skip to content

Commit

Permalink
Merge pull request #128 from dotcs/implement-some-tests
Browse files Browse the repository at this point in the history
Add some simple tests to demonstrate testing directives and components
  • Loading branch information
PatrickJS committed Nov 11, 2015
2 parents aff8584 + b5434ec commit b3065e0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {ROUTER_DIRECTIVES} from 'angular2/router';
@Directive({
selector: '[x-large]' // using [ ] means selecting attributes
})
class XLarge {
export class XLarge {
constructor(element: ElementRef) {
// simple DOM manipulation to set font size to x-large
// `nativeElement` is the direct reference to the DOM element
Expand Down
61 changes: 58 additions & 3 deletions test/app/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,64 @@
/// <reference path="../../src/typings/_custom.d.ts" />

// Select BrowserDomAdapter.
// see https://github.com/AngularClass/angular2-webpack-starter/issues/124
var domAdapter = require('angular2/src/core/dom/browser_adapter').BrowserDomAdapter;
domAdapter.makeCurrent();

// Import necessary wrappers for Jasmine
import {
beforeEachProviders,
describe,
expect,
iit,
inject,
it,
injectAsync,
fakeAsync,
TestComponentBuilder,
tick
} from 'angular2/testing';
import { Component, provide} from 'angular2/angular2';
import {MockBackend, BaseRequestOptions, Http} from 'angular2/http';

// Load the implementations that should be tested
import { App, XLarge } from '../../src/app/app';

// Create a test component to test directives
@Component({
template: '',
directives: [XLarge]
})
class TestComponent {
}

describe('x-large directive', () => {
it('should sent font-size to x-large', injectAsync([TestComponentBuilder], (tcb) => {
return tcb.overrideTemplate(TestComponent, '<div x-large>Content</div>')
.createAsync(TestComponent).then((fixture: any) => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement.children[0];
expect(compiled.style.fontSize).toBe('x-large');
});
}));

});

describe('App', () => {
// provide our implementations or mocks to the dependency injector
beforeEachProviders(() => [
App,
BaseRequestOptions,
MockBackend,
provide(Http, {useFactory:
function(backend, defaultOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]})
]);

it('should also be able to test', () => {
expect(true).toBe(true);
});
it('should have a title', inject([App], (app) => {
expect(app.title).toEqual('Angular 2');
}));

});
16 changes: 16 additions & 0 deletions test/injector.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
it,
describe,
expect,
inject
} from 'angular2/testing';
import {
APP_ID
} from 'angular2/angular2';


describe('default test injector', () => {
it('should provide default id', inject([APP_ID], (id) => {
expect(id).toBe('a');
}));
});
5 changes: 5 additions & 0 deletions test/sanity_test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('sanity checks', () => {
it('should also be able to test', () => {
expect(true).toBe(true);
});
});

0 comments on commit b3065e0

Please sign in to comment.