Skip to content
This repository was archived by the owner on Oct 26, 2025. It is now read-only.

Commit 0d6f950

Browse files
committed
Merge pull request #96 from reneolivo/missing-tests
Missing tests for example directive and service
2 parents 18f4a2b + 5cdd53d commit 0d6f950

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

app/js/directives/example.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ function ExampleDirective() {
44

55
return {
66
restrict: 'EA',
7-
link: (scope, element) => {
7+
templateUrl: 'directives/example.html',
8+
scope: {
9+
title: '@',
10+
message: '@exampleDirective'
11+
},
12+
link: (scope, element, attrs) => {
813
element.on('click', () => {
9-
console.log('element clicked');
14+
alert('Element clicked: ' + scope.message);
1015
});
1116
}
1217
};
13-
1418
}
1519

1620
export default {
1721
name: 'exampleDirective',
1822
fn: ExampleDirective
19-
};
23+
};

app/views/directives/example.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="example-directive">
2+
<h1>{{title}}</h1>
3+
</div>

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"scripts": {
7575
"pretest": "npm install",
7676
"test": "karma start test/karma.conf.js",
77-
"preprotractor": "npm run update-webdriver",
78-
"protractor": "npm run protractor test/protractor.conf.js"
77+
"preprotractor": "npm install && webdriver-manager update",
78+
"protractor": "protractor test/protractor.conf.js"
7979
}
8080
}

test/protractor.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports.config = {
88

99
allScriptsTimeout: 11000,
1010

11-
baseUrl: 'http://localhost:' + gulpConfig.UIPort + '/',
11+
baseUrl: 'http://localhost:' + gulpConfig.browserPort + '/',
1212

1313
directConnect: true,
1414

@@ -31,4 +31,4 @@ exports.config = {
3131
'e2e/**/*.js'
3232
]
3333

34-
};
34+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*global angular, module, browser*/
2+
3+
'use strict';
4+
5+
describe('Unit: ExampleDirective', function() {
6+
7+
var element, scope;
8+
9+
beforeEach(function() {
10+
spyOn(window, 'alert');
11+
angular.mock.module('app');
12+
13+
angular.mock.inject(function($compile, $rootScope) {
14+
scope = $rootScope;
15+
element = angular.element('<div example-directive="{{message}}" title="{{title}}">Sample Directive</div>');
16+
scope.title = "A sample title";
17+
scope.message = "It doesn't hurt.";
18+
$compile(element)(scope);
19+
scope.$digest();
20+
});
21+
});
22+
23+
it('should bind itself to the element', function() {
24+
element.triggerHandler('click');
25+
expect(window.alert).toHaveBeenCalledWith("Element clicked: It doesn't hurt.");
26+
});
27+
28+
it('should update its bindings', function() {
29+
scope.message = "It hurts a bit.";
30+
scope.$digest();
31+
element.triggerHandler('click');
32+
expect(window.alert).toHaveBeenCalledWith("Element clicked: It hurts a bit.");
33+
});
34+
35+
it('should bind a title property to its template', function() {
36+
expect(element.find('h1').text()).toBe('A sample title');
37+
});
38+
39+
});

test/unit/services/example_spec.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
describe('Unit: ExampleService', function() {
66

7-
var service;
7+
var http, service;
88

99
beforeEach(function() {
1010
// instantiate the app module
1111
angular.mock.module('app');
1212

1313
// mock the service
14-
angular.mock.inject(function(ExampleService) {
14+
angular.mock.inject(function($httpBackend, ExampleService) {
15+
http = $httpBackend;
1516
service = ExampleService;
1617
});
1718
});
@@ -20,4 +21,15 @@ describe('Unit: ExampleService', function() {
2021
expect(service).toBeDefined();
2122
});
2223

23-
});
24+
it('should retrieve data', function(done) {
25+
http.expect('GET', 'apiPath').respond(201, {data: 1234});
26+
27+
service.get().then(function(result) {
28+
expect(result).toEqual({data: 1234});
29+
}, function(error) {
30+
expect(error).toBeUndefined();
31+
}).then(done);
32+
33+
http.flush();
34+
});
35+
});

0 commit comments

Comments
 (0)