Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmackay committed Nov 15, 2016
1 parent d8e1cac commit c6d923f
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 188 deletions.
5 changes: 4 additions & 1 deletion app/main/posts/modify/location.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ function PostLocationDirective($http, L, Geocoding, Maps, _, Notify, $window) {

function clear() {
$scope.model = null;
marker.remove();
if (marker) {
map.removeLayer(marker);
marker = null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,71 +1,76 @@
var L = require('leaflet');
// Load leaflet plugins here too
require('imports?L=leaflet!leaflet.markercluster');
require('imports?L=leaflet!leaflet.locatecontrol/src/L.Control.Locate');

describe('post location directive', function () {

var $rootScope,
$scope,
isolateScope,
Notify,
element,
Geocoding;
Geocoding,
Maps,
map,
marker;

beforeEach(function () {
fixture.setBase('mocked_backend/api/v3');

Maps = {
createMap: function () {}
};

var testApp = makeTestApp();

testApp.directive('postLocation', require('app/main/posts/modify/post-location.directive'));
testApp.directive('postLocation', require('app/main/posts/modify/location.directive'))
.service('Leaflet', () => {
return L;
})
.service('Maps', () => {
return Maps;
})
.value('Geocoding', {
search: function (searchLocationTerm) {}
})
;

angular.mock.module('testApp');
});



beforeEach(function () {
angular.mock.module(function ($provide) {

$provide.value('Geocoding', {
search: function (searchLocationTerm) {}
});

$provide.value('leafletData', {
getMap: function (mapName) {
return {
then: function (callback) {
return angular.element();
}
};
}
});

$provide.value('Leaflet', {
control: {
locate: function (opts) {
return {
addTo: function (m) {}
};
}
}
});
});
});

beforeEach(angular.mock.inject(function (_$rootScope_, $compile, _Notify_, _Geocoding_) {
beforeEach(angular.mock.inject(function (_$rootScope_, $compile, _Notify_, _Geocoding_, _Maps_, $q) {
$rootScope = _$rootScope_;
$scope = _$rootScope_.$new();

Notify = _Notify_;
Geocoding = _Geocoding_;

$scope.post = {};
$scope.model = {};
element = '<post-location attribute="attribute" key="key" model="model"></post-location>';
map = L.map(document.createElement('div'));
marker = L.marker([7,7]);
spyOn(Maps, 'createMap').and.returnValue($q.when(map));
spyOn(map, 'setView').and.callThrough();
spyOn(L, 'marker').and.returnValue(marker);
spyOn(marker, 'setLatLng').and.callThrough();
spyOn(marker, 'addTo').and.callThrough();

$scope = _$rootScope_.$new();
$scope.model = {
lat: 3,
lon: 4
};

element = '<post-location attribute="attribute" key="key" model="model" id="1"></post-location>';
element = $compile(element)($scope);
$scope.$digest();
isolateScope = element.children().scope();

}));

describe('test directive functions', function () {
it('should init map', function () {
expect(Maps.createMap).toHaveBeenCalled();
expect(L.marker).toHaveBeenCalled();
expect(marker.addTo).toHaveBeenCalledWith(map);
});

it('should not clear search location term for failed searches', function () {
isolateScope.$apply(function () {
isolateScope.searchLocationTerm = 'Lorem';
Expand Down Expand Up @@ -101,11 +106,7 @@ describe('post location directive', function () {
expect(isolateScope.searchLocationTerm).toEqual('');
});

it('should call updateLatLon, updateMarkerPosition and centerMapTo on successful search', function () {
spyOn(isolateScope, 'updateLatLon');
spyOn(isolateScope, 'updateMarkerPosition');
spyOn(isolateScope, 'centerMapTo');

it('should call update model, marker position and map center', function () {
spyOn(Geocoding, 'search').and.callFake(function (kupi) {
return {
then: function (callback) {
Expand All @@ -116,28 +117,22 @@ describe('post location directive', function () {

isolateScope.searchLocation();

expect(isolateScope.updateLatLon).toHaveBeenCalledWith(1, 2);
expect(isolateScope.updateMarkerPosition).toHaveBeenCalledWith(1, 2);
expect(isolateScope.centerMapTo).toHaveBeenCalledWith(1, 2);
expect(isolateScope.model.lat).toEqual(1);
expect(isolateScope.model.lon).toEqual(2);
expect(marker.setLatLng).toHaveBeenCalledWith([1, 2]);
expect(map.setView).toHaveBeenCalledWith([1, 2], 8);
});

it('should clear scope model, center and markers when Clear button is pressed', function () {
it('should clear scope model, and remove the marker', function () {

isolateScope.$apply(function () {
isolateScope.model = { lat: 100, lng: 200 };
isolateScope.initialCenter = { lat: 100, lng: 200 };
isolateScope.center = { lat: 20, lng: 30 };

isolateScope.markers = {
m1 : { lat: 40, lng: 50 }
};
});

isolateScope.clear();

expect(isolateScope.model).toBeNull();
expect(isolateScope.center).toEqual(isolateScope.initialCenter);
expect(isolateScope.markers).toEqual({});
expect(marker.remove).toBeCalled;
});
});

Expand Down
92 changes: 81 additions & 11 deletions test/unit/main/post/views/post-view-map.directive.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
var L = require('leaflet');
describe('post view map directive', function () {

var $rootScope,
$scope,
$compile,
isolateScope,
Notify,
Maps,
PostEndpoint,
map,
geojson,
markers,
element;

beforeEach(function () {
Expand All @@ -13,31 +20,94 @@ describe('post view map directive', function () {
var testApp = makeTestApp();

testApp.directive('postViewMap', require('app/main/posts/views/post-view-map.directive'))
.value('$filter', function () {
return function () {};
})
.value('PostEntity', {});
.value('Leaflet', L)
;

angular.mock.module('testApp');
});

beforeEach(angular.mock.inject(function (_$rootScope_, _$compile_, _Notify_, _Maps_, _PostEndpoint_, $q) {
$rootScope = _$rootScope_;
$compile = _$compile_;
Notify = _Notify_;
Maps = _Maps_;
PostEndpoint = _PostEndpoint_;

map = L.map(document.createElement('div'), {
center: [0,1],
zoom: 5,
layers: [L.tileLayer('http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png')]
});
spyOn(Maps, 'createMap').and.returnValue($q.when(map));
var lgeoJson = L.geoJson;
spyOn(L, 'geoJson').and.callFake(function (args) {
geojson = lgeoJson(args);
spyOn(geojson, 'addTo').and.callThrough();

beforeEach(angular.mock.inject(function (_$rootScope_, $compile, _Notify_) {
$rootScope = _$rootScope_;
$scope = _$rootScope_.$new();
return geojson;
});
var markerClusterGroup = L.markerClusterGroup;
spyOn(L, 'markerClusterGroup').and.callFake(function (args) {
markers = markerClusterGroup(args);
spyOn(markers, 'addTo').and.callThrough();

Notify = _Notify_;
return markers;
});
spyOn(PostEndpoint, 'geojson').and.callThrough();

$scope = _$rootScope_.$new();
$scope.isLoading = true;
$scope.filters = {};

element = '<post-view-map filters="filters" is-loading="isLoading"></post-view-map>';
}));

it('should create a map', function () {
element = $compile(element)($scope);
$rootScope.$digest();
isolateScope = element.isolateScope();
}));

it('should load initial values', function () {
expect(isolateScope.isLoading).toBe(false);
expect(Maps.createMap).toHaveBeenCalled();
});

it('should load geojson data and add it to the map', function () {
map.options.clustering = false;

element = $compile(element)($scope);
$rootScope.$digest();
isolateScope = element.isolateScope();

expect(PostEndpoint.geojson).toHaveBeenCalled();
expect(L.geoJson).toHaveBeenCalled();
expect(geojson.addTo).toHaveBeenCalledWith(map);
});

it('should load geojson data and add clusters to the map', function () {
map.options.clustering = true;

element = $compile(element)($scope);
$rootScope.$digest();
isolateScope = element.isolateScope();

expect(PostEndpoint.geojson).toHaveBeenCalled();
expect(L.geoJson).toHaveBeenCalled();
expect(L.markerClusterGroup).toHaveBeenCalled();
expect(markers.addTo).toHaveBeenCalledWith(map);
});

it('reload geojson when filters change', function () {
element = $compile(element)($scope);
$rootScope.$digest();
isolateScope = element.isolateScope();

isolateScope.$apply(function () {
isolateScope.filters = {
status : 'published'
};
});

expect(L.geoJson).toHaveBeenCalled();
expect(L.geoJson.calls.count()).toEqual(2);
expect(geojson.addTo).toHaveBeenCalledWith(map);
});
});
2 changes: 1 addition & 1 deletion test/unit/mock/mock-modules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('ushahidi.mock', [])
.provider('$translate', require('./services/translate.js'))
.service('leafletData', require('./services/third_party/leaflet-data.js'))
.service('Leaflet', require('./services/third_party/leaflet.js'))
.service('d3', require('./services/third_party/d3.js'))
.service('moment', require('./services/third_party/moment.js'))

Expand Down
Loading

0 comments on commit c6d923f

Please sign in to comment.