Skip to content

Commit 8e17754

Browse files
committed
Add angular specs
1 parent 6fa6acb commit 8e17754

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

Diff for: JasmineIntroduction/SpecRunner.html

+26-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,34 @@
1111
<script src="lib/jasmine-2.3.4/jasmine-html.js"></script>
1212
<script src="lib/jasmine-2.3.4/boot.js"></script>
1313

14+
<script src="node_modules/jquery/dist/jquery.js""></script>
15+
<script src="node_modules/angular/angular.js""></script>
16+
<script src="node_modules/angular-mocks/angular-mocks.js""></script>
17+
18+
<script src="demoController.js"></script>
19+
1420
<script src="app.spec.js"></script>
21+
<script src="angular.spec.js"></script>
22+
23+
<script>
24+
angular.module("DemoApp", [])
25+
.constant("InvalidLastName", "Muster")
26+
.controller("DemoController", DemoController);
27+
</script>
1528
</head>
1629

17-
<body>
30+
<body ng-app="DemoApp" ng-controller="DemoController as vm">
31+
<h1>Angular Demo App</h1>
32+
<p>
33+
First Name: <input type="input" ng-model="vm.firstName" /><br/>
34+
Last Name: <input type="input" ng-model="vm.lastName" /><br/>
35+
Details: {{ vm.details.address }} <button type="button" ng-click="vm.getDetails()">Get Details</button>
36+
</p>
37+
<p ng-show="!vm.isValid">
38+
The values you entered are invalid!
39+
</p>
40+
<p ng-show="vm.restApiError">
41+
Server cannot be reached!
42+
</p>
1843
</body>
1944
</html>

Diff for: JasmineIntroduction/angular.spec.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// <reference path="typings/tsd.d.ts" />
2+
/// <reference path="./demoController.ts" />
3+
4+
// Setup module for unit testing. Note that we are mocking
5+
// "InvalidLastName" here.
6+
angular.module("DemoTest", [])
7+
.constant("InvalidLastName", "Muster2")
8+
.controller("DemoController", DemoController);
9+
10+
describe('DemoController', () => {
11+
// Specify that we use "DemoTest" module for testing
12+
beforeEach(angular.mock.module('DemoTest'));
13+
14+
// Setup dependency injection for each test
15+
var controller : DemoController;
16+
var $scope: any;
17+
var $httpBackend: ng.IHttpBackendService;
18+
beforeEach(inject(function(_$controller_: ng.IControllerService, _$rootScope_: ng.IRootScopeService, _$httpBackend_: ng.IHttpBackendService){
19+
$scope = <ng.IScope>_$rootScope_.$new();
20+
$httpBackend = _$httpBackend_;
21+
controller = <DemoController>_$controller_('DemoController', { $scope: $scope });
22+
}));
23+
24+
describe('validity status', () => {
25+
it('is originally valid', () => {
26+
expect(controller.isValid).toBeTruthy();
27+
});
28+
29+
it('turns to false if invalid data is entered', () => {
30+
// Set last name and initiate digest cycle so that $watch
31+
// will be fired.
32+
controller.lastName = "Muster2";
33+
$scope.$digest();
34+
35+
expect(controller.isValid).toBeFalsy();
36+
});
37+
});
38+
39+
describe('customer details', () => {
40+
it('queries server for details', () => {
41+
var dummyDetails : ICustomerDetails = { address: "Anywhere 99, Somewhere" };
42+
43+
// Use $http mockup service to simulate response of web api
44+
$httpBackend.expectGET("https://myserver.com/api/getCustomerDetails")
45+
.respond(200, dummyDetails);
46+
47+
controller.getDetails();
48+
$httpBackend.flush();
49+
expect(controller.details.address).toBe(dummyDetails.address);
50+
51+
// Verify that all expected requests appeared
52+
$httpBackend.verifyNoOutstandingExpectation();
53+
$httpBackend.verifyNoOutstandingRequest();
54+
});
55+
});
56+
});

Diff for: JasmineIntroduction/demoController.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path="typings/tsd.d.ts" />
2+
3+
interface ICustomerDetails {
4+
address: string;
5+
}
6+
7+
class DemoController {
8+
public firstName: string;
9+
public lastName: string;
10+
public isValid: boolean;
11+
public details: ICustomerDetails;
12+
public restApiError: boolean;
13+
14+
constructor($scope: ng.IScope, InvalidLastName: string, private $http: ng.IHttpService) {
15+
this.firstName = "Tom";
16+
this.lastName = "Turbo";
17+
this.isValid = true;
18+
this.restApiError = false;
19+
this.details = null;
20+
21+
$scope.$watch(
22+
"vm.lastName",
23+
() => this.isValid = this.lastName !== InvalidLastName);
24+
}
25+
26+
getDetails() {
27+
this.$http.get<ICustomerDetails>("https://myserver.com/api/getCustomerDetails")
28+
.then(
29+
data => this.details = data.data,
30+
err => this.restApiError = true);
31+
}
32+
}

Diff for: JasmineIntroduction/tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"node_modules", "lib"
1010
],
1111
"files": [
12-
"app.spec.ts"
12+
"app.spec.ts",
13+
"angular.spec.ts",
14+
"demoController.ts"
1315
]
1416
}

Diff for: JasmineIntroduction/tsd.json

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
"installed": {
88
"jasmine/jasmine.d.ts": {
99
"commit": "62eedc3121a5e28c50473d2e4a9cefbcb9c3957f"
10+
},
11+
"angularjs/angular.d.ts": {
12+
"commit": "62eedc3121a5e28c50473d2e4a9cefbcb9c3957f"
13+
},
14+
"jquery/jquery.d.ts": {
15+
"commit": "62eedc3121a5e28c50473d2e4a9cefbcb9c3957f"
16+
},
17+
"angularjs/angular-mocks.d.ts": {
18+
"commit": "62eedc3121a5e28c50473d2e4a9cefbcb9c3957f"
1019
}
1120
}
1221
}

0 commit comments

Comments
 (0)