Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some simple tests to demonstrate testing directives and components #128

Merged
merged 1 commit into from
Nov 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think we can move this spec.bundle.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, see #129


// 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);
});
});