Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit e186288

Browse files
committed
docs(testing): more testing samples and infrastructure setup
added wallaby config; revised karma-test-shim
1 parent 96137ef commit e186288

26 files changed

+1019
-175
lines changed

gulpfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ var _exampleBoilerplateFiles = [
7676
'styles.css',
7777
'tsconfig.json',
7878
'tslint.json',
79-
'typings.json'
79+
'typings.json',
80+
'wallaby.js'
8081
];
8182

8283
var _exampleDartWebBoilerPlateFiles = ['styles.css'];

public/docs/_examples/.gitignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
.editorconfig
2+
.idea
3+
.vscode
24
styles.css
35
typings
46
typings.json
7+
node_modules
8+
jspm_packages
59
*.js.map
610
package.json
711
karma.conf.js
812
karma-test-shim.js
913
tsconfig.json
1014
tslint.json
15+
wallaby.js
1116
npm-debug*.
12-
**/protractor.config.js
17+
protractor.config.js
1318
_test-output
19+
_temp
20+
21+
!**/*e2e-spec.js

public/docs/_examples/displaying-data/e2e-spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
describe('Displaying Data Tests', function () {
2-
32
var _title = "Tour of Heroes";
43
var _defaultHero = 'Windstorm'
54

@@ -15,7 +14,12 @@ describe('Displaying Data Tests', function () {
1514
expect(element(by.css('h2')).getText()).toContain(_defaultHero);
1615
});
1716

18-
it('should have many heroes', function () {
17+
it('should have heroes', function () {
18+
var heroEls = element.all(by.css('li'));
19+
expect(heroEls.count()).not.toBe(0, 'should have heroes');
20+
});
21+
22+
it('should display "there are many heroes!"', function () {
1923
expect(element(by.css('ul ~ p')).getText()).toContain('There are many heroes!');
2024
});
2125
});

public/docs/_examples/displaying-data/ts/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// #docregion imports
44
import {Component} from 'angular2/core';
55
// #enddocregion imports
6-
import {Hero} from './hero'
6+
import {Hero} from './hero';
77

88
@Component({
99
selector: 'my-app',

public/docs/_examples/karma-test-shim.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,29 @@ System.config({ packages: packages });
4141

4242
// Configure Angular for the browser and
4343
// with test versions of the platform providers
44-
System.import('angular2/testing')
45-
.then(function (testing) {
46-
return System.import('angular2/platform/testing/browser')
47-
.then(function (providers) {
48-
testing.setBaseTestProviders(
49-
providers.TEST_BROWSER_PLATFORM_PROVIDERS,
50-
providers.TEST_BROWSER_APPLICATION_PROVIDERS
51-
);
52-
});
44+
Promise.all([
45+
System.import('angular2/testing'),
46+
System.import('angular2/platform/testing/browser')
47+
])
48+
.then(function (results) {
49+
var testing = results[0];
50+
var browser = results[1];
51+
testing.setBaseTestProviders(
52+
browser.TEST_BROWSER_PLATFORM_PROVIDERS,
53+
browser.TEST_BROWSER_APPLICATION_PROVIDERS);
54+
55+
// Load all spec files
56+
// (e.g. 'base/app/hero.service.spec.js')
57+
return Promise.all(
58+
Object.keys(window.__karma__.files)
59+
.filter(onlySpecFiles)
60+
.map(function (moduleName) {
61+
moduleNames.push(moduleName);
62+
return System.import(moduleName);
63+
}));
5364
})
5465

55-
// Load all spec files
56-
// (e.g. 'base/app/hero.service.spec.js')
57-
.then(function () {
58-
return Promise.all(
59-
Object.keys(window.__karma__.files)
60-
.filter(onlySpecFiles)
61-
.map(function (moduleName) {
62-
moduleNames.push(moduleName);
63-
return System.import(moduleName);
64-
}));
65-
})
66-
67-
.then(success, fail);
66+
.then(success, fail);
6867

6968
////// Helpers //////
7069

public/docs/_examples/karma.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = function(config) {
22

3-
var appBase = 'app/'; // transpiled app JS files
4-
var appAssets ='base/app/'; // component assets fetched by Angular's compiler
3+
var appBase = 'app/'; // transpiled app JS files
4+
var appAssets ='/base/app/'; // component assets fetched by Angular's compiler
55

66
config.set({
77
basePath: '',

public/docs/_examples/lifecycle-hooks/ts/app/on-changes.component.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* tslint:disable:forin */
12
// #docregion
23
import {
34
Component, Input, ViewChild,
@@ -6,7 +7,7 @@ import {
67

78

89
class Hero {
9-
constructor(public name:string){}
10+
constructor(public name: string) {}
1011
}
1112

1213
@Component({
@@ -30,13 +31,13 @@ export class OnChangesComponent implements OnChanges {
3031
@Input() power: string;
3132
// #enddocregion inputs
3233

33-
changeLog:string[] = [];
34+
changeLog: string[] = [];
3435

3536
// #docregion ng-on-changes
3637
ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {
3738
for (let propName in changes) {
3839
let prop = changes[propName];
39-
let cur = JSON.stringify(prop.currentValue)
40+
let cur = JSON.stringify(prop.currentValue);
4041
let prev = JSON.stringify(prop.previousValue);
4142
this.changeLog.push(`${propName}: currentValue = ${cur}, previousValue = ${prev}`);
4243
}
@@ -50,13 +51,13 @@ export class OnChangesComponent implements OnChanges {
5051

5152
@Component({
5253
selector: 'on-changes-parent',
53-
templateUrl:'app/on-changes-parent.component.html',
54+
templateUrl: 'app/on-changes-parent.component.html',
5455
styles: ['.parent {background: Lavender;}'],
5556
directives: [OnChangesComponent]
5657
})
5758
export class OnChangesParentComponent {
58-
hero:Hero;
59-
power:string;
59+
hero: Hero;
60+
power: string;
6061
title = 'OnChanges';
6162
@ViewChild(OnChangesComponent) childView:OnChangesComponent;
6263

@@ -69,6 +70,6 @@ export class OnChangesParentComponent {
6970
this.hero = new Hero('Windstorm');
7071
// setting power only triggers onChanges if this value is different
7172
this.power = 'sing';
72-
this.childView && this.childView.reset();
73+
if (this.childView) { this.childView.reset(); }
7374
}
7475
}

public/docs/_examples/package.json

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
"name": "angular2-examples-master",
33
"version": "1.0.0",
44
"description": "Master package.json, the superset of all dependencies for all of the _example package.json files.",
5-
"main": "index.js",
65
"scripts": {
76
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
8-
"tsc": "tsc",
9-
"tsc:w": "tsc -w",
10-
"lite": "lite-server",
11-
"live": "live-server",
12-
"test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
13-
"build-and-test": "npm run tsc && npm run test",
7+
"e2e": "tsc && concurrently \"http-server\" \"protractor protractor.config.js\"",
148
"http-server": "tsc && http-server",
159
"http-server:e2e": "http-server",
10+
"lite": "lite-server",
11+
"postinstall": "typings install",
12+
"test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
13+
"tsc": "tsc",
14+
"tsc:w": "tsc -w",
1615
"typings": "typings",
17-
"postinstall": "typings install"
16+
"webdriver:update": "webdriver-manager update"
1817
},
1918
"keywords": [],
2019
"author": "",
@@ -37,14 +36,14 @@
3736
"typescript": "^1.8.10",
3837
"typings":"^0.7.12",
3938

39+
"canonical-path": "0.0.2",
4040
"http-server": "^0.9.0",
4141
"jasmine-core": "~2.4.1",
4242
"karma": "^0.13.22",
4343
"karma-chrome-launcher": "^0.2.3",
4444
"karma-cli": "^0.1.2",
4545
"karma-htmlfile-reporter": "^0.2.2",
4646
"karma-jasmine": "^0.3.8",
47-
"live-server": "^0.9.2",
4847
"protractor": "^3.2.2",
4948
"rimraf": "^2.5.2"
5049
},

public/docs/_examples/protractor.config.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
// TO RUN THE TESTS
2-
//
3-
// The first time, run:
1+
// FIRST TIME ONLY- run:
42
// ./node_modules/.bin/webdriver-manager update
5-
// Make sure the test server is running. Then do.
6-
// ./node_modules/.bin/protractor protractor.config.js
3+
//
4+
// Try: `npm run webdriver:update`
5+
//
6+
// AND THEN EVERYTIME ...
7+
// 1. Compile with `tsc`
8+
// 2. Make sure the test server (e.g., http-server: localhost:8080) is running.
9+
// 3. ./node_modules/.bin/protractor protractor.config.js
10+
//
11+
// To do all steps, try: `npm run e2e`
712

813
var fs = require('fs');
914
var path = require('canonical-path');
@@ -93,17 +98,13 @@ function itIf(cond, name, func) {
9398
}
9499
}
95100

96-
// Hack - because of bug with send keys
101+
// Hack - because of bug with protractor send keys
97102
function sendKeys(element, str) {
98103
return str.split('').reduce(function (promise, char) {
99-
return promise.then(function () {
100-
return element.sendKeys(char);
101-
});
104+
return promise.resolve(element.sendKeys(char));
102105
}, element.getAttribute('value'));
103-
// better to create a resolved promise here but ... don't know how with protractor;
104106
}
105107

106-
107108
function Reporter(options) {
108109
var _defaultOutputFile = path.resolve(process.cwd(), "../../", 'protractor-results.txt');
109110
options.outputFile = options.outputFile || _defaultOutputFile;

public/docs/_examples/quickstart/ts/package.1.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"version": "1.0.0",
44
"scripts": {
55
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
6+
"lite": "lite-server",
7+
"postinstall": "typings install",
68
"tsc": "tsc",
79
"tsc:w": "tsc -w",
8-
"lite": "lite-server",
9-
"typings": "typings",
10-
"postinstall": "typings install"
10+
"typings": "typings"
1111
},
1212
"license": "ISC",
1313
"dependencies": {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* tslint:disable:no-unused-variable */
2+
import { AppComponent } from './app.component';
3+
4+
import { By } from 'angular2/platform/browser';
5+
import { provide } from 'angular2/core';
6+
7+
import {
8+
beforeEach, beforeEachProviders,
9+
describe, ddescribe, xdescribe,
10+
expect, it, iit, xit,
11+
inject, injectAsync,
12+
ComponentFixture, TestComponentBuilder
13+
} from 'angular2/testing';
14+
15+
import { Hero, HeroService, MockHeroService } from './mock-hero.service';
16+
17+
import { Router, MockRouter,
18+
RouterLink, MockRouterLink,
19+
RouterOutlet, MockRouterOutlet} from './mock-router';
20+
21+
describe('AppComponent', () => {
22+
let fixture: ComponentFixture;
23+
let comp: AppComponent;
24+
25+
beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
26+
return tcb
27+
.overrideDirective(AppComponent, RouterLink, MockRouterLink)
28+
.overrideDirective(AppComponent, RouterOutlet, MockRouterOutlet)
29+
.overrideProviders(AppComponent, [
30+
provide(HeroService, {useClass: MockHeroService}),
31+
provide(Router, {useClass: MockRouter}),
32+
])
33+
.createAsync(AppComponent)
34+
.then(fix => {
35+
fixture = fix;
36+
comp = fixture.debugElement.componentInstance;
37+
});
38+
}));
39+
40+
it('can instantiate it', () => {
41+
expect(comp).not.toBeNull();
42+
});
43+
44+
it('can get title from template', () => {
45+
fixture.detectChanges();
46+
let titleEl = fixture.debugElement.query(By.css('h1')).nativeElement;
47+
expect(titleEl).toHaveText(comp.title);
48+
});
49+
50+
it('can get RouterLinks from template', () => {
51+
fixture.detectChanges();
52+
53+
let links = fixture.debugElement
54+
.queryAll(function (de) { return de.componentInstance instanceof MockRouterLink; })
55+
.map(de => <MockRouterLink> de.componentInstance);
56+
57+
expect(links.length).toEqual(2, 'should have 2 links');
58+
expect(links[0].routeParams[0]).toEqual('Dashboard', '1st link should go to Dashboard');
59+
expect(links[1].routeParams[0]).toEqual('Heroes', '1st link should go to Heroes');
60+
61+
let result = links[1].onClick();
62+
expect(result).toEqual(false, 'click should prevent default browser behavior');
63+
});
64+
65+
it('can click Heroes link in template', () => {
66+
fixture.detectChanges();
67+
68+
// Heroes RouterLink DebugElement
69+
let heroesDe = fixture.debugElement
70+
.queryAll(function (de) { return de.componentInstance instanceof MockRouterLink; })[1];
71+
72+
expect(heroesDe).not.toBeNull('should 2nd link');
73+
74+
let link = <MockRouterLink> heroesDe.componentInstance;
75+
expect(link.navigatedTo).toBeNull('link should not have navigate yet');
76+
77+
heroesDe.triggerEventHandler('click', null);
78+
79+
fixture.detectChanges();
80+
expect(link.navigatedTo[0]).toEqual('Heroes');
81+
82+
});
83+
});
84+

0 commit comments

Comments
 (0)