Last active
November 17, 2015 06:20
-
-
Save ovrmrw/29d39e89e03e44920a1a to your computer and use it in GitHub Desktop.
Angular2, TypeScript, Jasmine sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.heroes {list-style-type: none; margin-left: 1em; padding: 0; width: 10em;} | |
.heroes li { cursor: pointer; position: relative; left: 0; transition: all 0.2s ease; } | |
.heroes li:hover {color: #369; background-color: #EEE; left: .2em;} | |
.heroes .badge { | |
font-size: small; | |
color: white; | |
padding: 0.1em 0.7em; | |
background-color: #369; | |
line-height: 1em; | |
position: relative; | |
left: -1px; | |
top: -1px; | |
} | |
.selected { background-color: #EEE; color: #369; } | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>{{title}}</h1> | |
<h2>My Heroes</h2> | |
<ul class="heroes"> | |
<li *ng-for="#hero of heroes" [ng-class]="getSelectedClass(hero)" (click)="onSelect(hero)"> | |
<span class="badge">{{hero.id}}</span> {{hero.name}} | |
</li> | |
</ul> | |
<div *ng-if="selectedHero"> | |
<h2>{{selectedHero.name}} details!</h2> | |
<div> | |
<label>id: </label>{{selectedHero.id}}</span> | |
</div> | |
<div> | |
<label>name: </label> | |
<input type="text" [(ng-model)]="selectedHero.name" placeholder="name" /> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {bootstrap, provide, ComponentRef} from 'angular2/angular2'; | |
import {AppComponent} from './app'; | |
import {HeroService} from './hero-service'; | |
describe("Jasmine BeforeEach Test", () => { | |
let ac: AppComponent; | |
beforeEach(done => { // get the instance of AppComponent. | |
bootstrap(AppComponent, [provide(HeroService, { | |
useValue: { getHeroes: () => [{ name: 'mock1' }, { name: 'mock2' }] } | |
})]) // injecting a mock provider for the tests. | |
.then(result => result.instance) // result.instance is the instance of AppComponent. | |
.then(i => { | |
ac = i; // getting the instance to outside of beforeEach to test below. | |
done(); | |
}); | |
}); | |
it("First Hero's name", () => expect(ac.heroes[0].name).toEqual('mock1')); | |
it("Second Hero's name", () => expect(ac.heroes[1].name).toEqual('mock2')); | |
it("Hero's number", () => expect(ac.heroes.length).toEqual(2)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {bootstrap, Component} from 'angular2/angular2'; | |
import {HeroService} from './hero-service'; | |
@Component({ | |
selector: 'my-app', | |
templateUrl: 'app/app.html', | |
styleUrls: ['app/app.css'], | |
}) | |
export class AppComponent { | |
title: string = 'Tour of Heroes'; | |
selectedHero: Hero; | |
heroes: Hero[]; | |
constructor(heroService: HeroService) { | |
this.heroes = heroService.getHeroes(); | |
} | |
onSelect(hero: Hero) { | |
this.selectedHero = hero; | |
} | |
getSelectedClass(hero: Hero) { | |
return { 'selected': hero === this.selectedHero }; | |
} | |
} | |
bootstrap(AppComponent, [HeroService]) | |
.then(result => { | |
console.log(result.instance); | |
console.log(JSON.stringify(result.instance)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class HeroService { | |
heroes: Hero[]; | |
constructor() { | |
this.heroes = HEROES; | |
} | |
getHeroes() { | |
return this.heroes; | |
} | |
} | |
var HEROES: Hero[] = [ | |
{ id: 11, name: "Mr. Nice" }, | |
{ "id": 12, "name": "Narco" }, | |
{ "id": 13, "name": "Bombasto" }, | |
{ "id": 14, "name": "Celeritas" }, | |
{ "id": 15, "name": "Magneta" }, | |
{ "id": 16, "name": "RubberMan" }, | |
{ "id": 17, "name": "Dynama" }, | |
{ "id": 18, "name": "Dr IQ" }, | |
{ "id": 19, "name": "Magma" }, | |
{ "id": 20, "name": "Tornado" }, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Angular 2 QuickStart</title> | |
<script src="../node_modules/systemjs/dist/system.src.js"></script> | |
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script> | |
<link rel="stylesheet" href="../node_modules/jasmine-core/lib/jasmine-core/jasmine.css"> | |
<script src="../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script> | |
<script src="../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script> | |
<script src="../node_modules/jasmine-core/lib/jasmine-core/boot.js"></script> | |
<script> | |
System.config({ | |
packages: {'app': {defaultExtension: 'js'}} | |
}); | |
System.import('app/app.spec').then(window.onload); | |
</script> | |
</head> | |
<body> | |
<my-app>loading...</my-app> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment