Skip to content

Commit 10795fe

Browse files
committed
adding unit tests
1 parent 8d3d358 commit 10795fe

File tree

6 files changed

+108
-8
lines changed

6 files changed

+108
-8
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
# angularDemo
1+
# Synopsis
2+
3+
A darksky API consumer UI
4+
5+
# Installation
6+
7+
npm start
8+
9+
# Tests
10+
11+
karma start
12+
13+
# License
14+
15+
MIT

app/main/main.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="main-view">
22
<p>Select a city to display the weather</p>
33
<div class="locations-list" ng-if="locations">
4-
<location ng-repeat="location in locations" tooltip={ {location.tooltip}} label={{location.label}} icon={{location.icon}} ng-click="displayWeather($index)"></location>
4+
<location ng-repeat="location in locations" tooltip={{location.tooltip}} label={{location.label}} icon={{location.icon}} ng-click="displayWeather($index)"></location>
55
</div>
66
</div>

karma.conf.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ module.exports = function (config) {
2121
'./app.js',
2222
'./app/main/main.js',
2323
'./app/main/locationsService.js',
24-
'./test/main/locationsService.spec.js'
24+
'./app/forecast/forecast.js',
25+
'./app/components/Components.js',
26+
'./app/components/location/locationDirective.js',
27+
'./test/**/*.js'
2528
],
2629

2730

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
describe('Location directive', function () {
2+
3+
beforeEach(module('Components'));
4+
5+
var element;
6+
var outerScope;
7+
var innerScope;
8+
9+
// Before each test load our darkskyForecast.main module
10+
beforeEach(inject(function ($rootScope, $compile) {
11+
element = angular.element('<location tooltip={{loc.tooltip}} label={{loc.label}} icon={{loc.icon}} ng-click="weatherCallback()"></location>');
12+
outerScope = $rootScope;
13+
$compile(element)(outerScope);
14+
innerScope = element.isolateScope();
15+
outerScope.$digest();
16+
}));
17+
18+
describe('HTML element', function () {
19+
beforeEach(function () {
20+
outerScope.$apply(function () {
21+
outerScope.loc = {
22+
tooltip: "Timisoara",
23+
label: "Timisoara",
24+
icon: "timisoara",
25+
id: "timisoara"
26+
};
27+
});
28+
})
29+
it('should be rendered correctly', function () {
30+
expect(element[0].title).toBe('Timisoara');
31+
expect(element[0].children[0].src).toContain('timisoara.png');
32+
expect(element[0].children[1].innerHTML).toBe('Timisoara');
33+
});
34+
});
35+
36+
describe('when the directive is clicked', function () {
37+
var spy = jasmine.createSpy('weatherCallback');
38+
beforeEach(function () {
39+
outerScope.$apply(function () {
40+
outerScope.weatherCallback = spy;
41+
});
42+
});
43+
beforeEach(function () {
44+
var event = document.createEvent("MouseEvent");
45+
event.initMouseEvent("click", true, true);
46+
element[0].dispatchEvent(event);
47+
});
48+
it('the callback should be called', function () {
49+
expect(spy).toHaveBeenCalled();
50+
});
51+
});
52+
});

test/forecast/forecast.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
describe('Forecast controller', function () {
2+
beforeEach(module('darkskyForecast.forecast'));
3+
4+
var scope, $routeParams, createCtrl;
5+
6+
beforeEach(inject(function ($rootScope, _$routeParams_, $controller) {
7+
scope = $rootScope.$new();
8+
$routeParams = _$routeParams_;
9+
createCtrl = function () {
10+
return $controller('ForecastCtrl', {
11+
$scope: scope,
12+
_$routeParams_: $routeParams
13+
})
14+
};
15+
var localStorage = window.localStorage;
16+
spyOn(localStorage, 'setItem').and.callFake(function (key, val) {
17+
return store[key] = value;
18+
});
19+
}));
20+
21+
it('should have the location data on scope', function () {
22+
localStorage['timisoara'] = JSON.stringify({
23+
tooltip: "Timisoara",
24+
label: "Timisoara",
25+
icon: "timisoara",
26+
id: "timisoara"
27+
});
28+
$routeParams.location = 'timisoara';
29+
var ctrl = createCtrl();
30+
expect(scope.location.label).toEqual('Timisoara');
31+
});
32+
});

test/main/locationsService.spec.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
describe('Locations provider', function () {
2+
// Before each test load our darkskyForecast.main module
3+
beforeEach(module('darkskyForecast.main'));
4+
25
var locationsService;
36
var $httpBackend;
47

5-
// Before each test load our api.users module
6-
beforeEach(module('darkskyForecast.main'));
7-
8-
// Before each test set our injected Users factory (_Users_) to our local Users variable
98
beforeEach(inject(function (_locationsService_, _$httpBackend_) {
109
locationsService = _locationsService_;
1110
$httpBackend = _$httpBackend_;
1211
}));
1312

14-
it("when loading the locations", function () {
13+
it("should load the locations", function () {
1514
$httpBackend.whenGET('app/main/locations.json').respond({
1615
locations: [{
1716
tooltip: "Timisoara",

0 commit comments

Comments
 (0)