Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmackay committed Aug 31, 2017
2 parents 8e81cfa + 4b0daff commit db6503e
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 21 deletions.
58 changes: 41 additions & 17 deletions app/main/posts/views/mode-context-form-filter.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ function ModeContextFormFilter($scope, FormEndpoint, PostEndpoint, TagEndpoint,
$scope.changeForms = changeForms;
activate();

$scope.$watch('filters', function () {
getPostStats(PostFilters.getFilters()).$promise.then(updateCounts);
}, true);

function activate() {
// Load forms
$scope.forms = FormEndpoint.queryFresh();
$scope.tags = TagEndpoint.queryFresh();
var postCountRequest = PostEndpoint.stats({ group_by: 'form', status: 'all', include_unmapped: true });
var postCountRequest = getPostStats($scope.filters);
$q.all([$scope.forms.$promise, postCountRequest.$promise, $scope.tags.$promise]).then(function (responses) {
if (!responses[1] || !responses[1].totals || !responses[1].totals[0]) {
return;
}
var values = responses[1].totals[0].values;
var tags = responses[2];

// adding children to tags
_.each(_.where(tags, { parent_id: null }), function (tag) {
if (tag && tag.children) {
Expand All @@ -47,12 +51,7 @@ function ModeContextFormFilter($scope, FormEndpoint, PostEndpoint, TagEndpoint,
}
});

angular.forEach($scope.forms, function (form) {
var value = _.findWhere(values, { id: form.id });
form.post_count = value ? value.total : 0;
});

$scope.forms.forEach(function (form, index) {
angular.forEach($scope.forms, function (form, index) {
var formTagIds = _.pluck(form.tags, 'id');
// assigning whole tag-object to forms
$scope.forms[index].tags = _.filter(_.map(tags, function (tag) {
Expand All @@ -75,15 +74,40 @@ function ModeContextFormFilter($scope, FormEndpoint, PostEndpoint, TagEndpoint,
return false;
}));
});
// Grab the count for form=null
var unknownValue = _.findWhere(values, { id: null });
if (unknownValue) {
$scope.unknown_post_count = unknownValue.total;
}
// Setting nb of unmapped posts
if (responses[1].unmapped) {
$scope.unmapped = responses[1].unmapped;
}

updateCounts(responses[1]);
});
}

function getPostStats(filters) {
var query = PostFilters.getQueryParams(filters);
var queryParams = _.extend({}, query, {
'group_by': 'form',
include_unmapped: true
});
// we want stats for all forms, not just the ones visible right now
if (queryParams.form) {
delete queryParams.form;
}
// deleting categories since they are selected in the sidebar and not in the filter-modal = might get confusing
if (queryParams.tags) {
delete queryParams.tags;
}
return PostEndpoint.stats(queryParams);
}

function updateCounts(stats) {
// assigning count of unknown-values
let unknown = _.findWhere(stats.totals[0].values, { id: null });
$scope.unknown_post_count = (unknown && unknown.total) ? unknown.total : 0;

// Setting nb of unmapped posts
$scope.unmapped = stats.unmapped ? stats.unmapped : 0;

// assigning count for all forms
_.each($scope.forms, function (form) {
let posts = _.findWhere(stats.totals[0].values, { id: form.id });
form.post_count = (posts && posts.total) ? posts.total : 0;
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
describe('mode-context-form-filter directive', function () {
var $rootScope,
$scope,
element,
isolateScope,
PostEndpoint,
FormEndpoint,
TagEndpoint,
$location,
PostSurveyService,
PostFilters;
beforeEach(function () {
var testApp;
fixture.setBase('mocked_backend/api/v3');
testApp = makeTestApp();
testApp.directive('modeContextFormFilter', require('app/main/posts/views/mode-context-form-filter.directive.js'));
angular.mock.module('testApp');
});

beforeEach(inject(function (_$rootScope_, $compile, _FormEndpoint_, _PostEndpoint_, _TagEndpoint_, _PostSurveyService_, _PostFilters_, _$location_) {
$rootScope = _$rootScope_;
$scope = _$rootScope_.$new();
PostEndpoint = _PostEndpoint_;
FormEndpoint = _FormEndpoint_;
TagEndpoint = _TagEndpoint_;
PostSurveyService = _PostSurveyService_;
PostFilters = _PostFilters_;
$location = _$location_;
spyOn(FormEndpoint, 'queryFresh').and.callThrough();
spyOn(TagEndpoint, 'queryFresh').and.callThrough();
spyOn(PostEndpoint, 'stats').and.callThrough();
spyOn(PostFilters, 'getQueryParams').and.callThrough();

$scope.filters = PostFilters.getDefaults();

element = '<mode-context-form-filter></mode-context-form-filter>';
element = $compile(element)($scope);
$scope.$digest();
isolateScope = element.scope();
// isolateScope.filters = {q: '', created_after: '', created_before: '', status: 'published', published_to: '', center_point: '', within_km: '1', current_stage: [], tags: [], form: [1,2], set: []};
}));
describe('test directive-functions', function () {
it('should fetch forms from endpoint', function () {
expect(FormEndpoint.queryFresh).toHaveBeenCalled();
});
it('should fetch tags from endpoint', function () {
expect(TagEndpoint.queryFresh).toHaveBeenCalled();
});
it('should fetch queryParams from service', function () {
expect(PostFilters.getQueryParams).toHaveBeenCalled();
});
it('should fetch post-stats from endpoint', function () {
expect(PostEndpoint.stats).toHaveBeenCalled();
});
it('should change the value of forms if filters are changed', function () {
spyOn(PostFilters, 'getFilters');
expect(PostFilters.getFilters).not.toHaveBeenCalled();
isolateScope.filters.status = 'archived';
$scope.$digest();
expect(PostFilters.getFilters).toHaveBeenCalled();
});
it('should change value of languageToggle on toggle', function () {
expect(isolateScope.showLanguage).toEqual(false);
isolateScope.languageToggle();
expect(isolateScope.showLanguage).toEqual(true);
});
it('should change form if a category is selected on a new form', function () {
isolateScope.forms = [{id: 1}, {id: 2}];
isolateScope.filters.tags = [1,2];
isolateScope.changeForms();
expect(isolateScope.filters.tags).toEqual([]);
});
it('should change filters when selecting show only', function () {
isolateScope.showOnly(2);
expect(isolateScope.filters.form.length).toEqual(1);
expect(isolateScope.filters.form[0]).toEqual(2);
});
it('should redirect to list-view', function () {
$location.path('/views/map');
isolateScope.goToUnmapped();
expect($location.path()).toEqual('/views/list');
});
it('should return the correct formatting for unmapped posts', function () {
isolateScope.unmapped = 1;
expect(isolateScope.getUnmapped()).toEqual('1 post');
isolateScope.unmapped = 2;
expect(isolateScope.getUnmapped()).toEqual('2 posts');
});
it('should hide forms that are not selected', function () {
isolateScope.hide(2);
expect(isolateScope.filters.form.indexOf(2)).toEqual(-1);
});
});
});
15 changes: 13 additions & 2 deletions test/unit/mock/services/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@ module.exports = [function () {
then: function (successCallback, failCallback) {
successCallback([{
name: 'test form',
id: 1
}]);
id: 1,
tags: [{
id: 1,
tag: 'Test-tag'
}]},
{
name: 'test form 2',
id: 2,
tags: [{
id: 1,
tag: 'Test-tag'
}]}
]);
}
}};
},
Expand Down
22 changes: 20 additions & 2 deletions test/unit/mock/services/post-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = [function () {
q: '',
created_after: '',
created_before: '',
status: 'published',
status: ['published'],
published_to: '',
center_point: '',
within_km: '1',
Expand All @@ -12,11 +12,29 @@ module.exports = [function () {
form: [],
set: []
};
var defaultFilters = {
q: '',
date_after: '',
date_before: '',
status: ['published', 'draft'],
published_to: '',
center_point: '',
has_location: 'all',
within_km: '1',
current_stage: [],
tags: [],
form: [1, 2],
set: [],
user: false
};

var filterMode = 'all';

return {
filterState: filterState,
getDefaults: function () {},
getDefaults: function () {
return defaultFilters;
},
getQueryParams: function (filters) {
return filters;
},
Expand Down

0 comments on commit db6503e

Please sign in to comment.