Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willdoran committed Aug 16, 2016
1 parent 2271702 commit 100410f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 157 deletions.
92 changes: 9 additions & 83 deletions app/post/modify/post-edit.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ function (

// Validate required fields for each task that needs to be validated
_.each(tasks_to_validate, function (task) {
_.each(task.attributes, function (attribute) {
var required_attributes = _.where(task.attributes, {required: true});

_.each(required_attributes, function (attribute) {
if (attribute.input === 'checkbox') {
var checkboxValidity = false;
_.each(attribute.options, function (option) {
Expand All @@ -63,96 +65,20 @@ function (
}
});
isPostValid = checkboxValidity;
form['values_' + attribute.id].$dirty = !checkboxValidity;
} else {

if (_.isUndefined(form['values_' + attribute.id]) || form['values_' + attribute.id].$invalid) {
form['values_' + attribute.id].$dirty = true;
if (!_.isUndefined(form['values_' + attribute.id])) {
form['values_' + attribute.id].$dirty = true;
}

isPostValid = false;
}
}
});
});
return isPostValid;
},
canSavePost: function (post, form, stages, attributes) {
var valid = true;
var errors = [];
if (post.status === 'published') {
// first check if stages required have been marked complete
var requiredStages = _.where(stages, {required: true}) ;

valid = _.reduce(requiredStages, function (isValid, stage) {
// if this stage isn't complete, add to errors
if (_.indexOf(post.completed_stages, stage.id) === -1) {
errors.push($translate.instant('post.modify.incomplete_step', { stage: stage.label }));
return false;
}
return isValid;
}, valid);

if (errors.length) {
Notify.errorsPretranslated(errors);
return valid;
}

valid = _.reduce(post.completed_stages, function (isValid, stageId) {
return PostEditService.isStageValid(stageId, form, stages, attributes) && isValid;
}, valid);
}

return valid;
},
isFirstStage: function (stages, stageId) {
if (!_.isEmpty(stages)) {
return stageId === stages[0].id;
}
return false;
},
isStageValid: function (stageId, form, stages, attributes) {
if (PostEditService.isFirstStage(stages, stageId)) {

// The first stage is assumed to contain the title, content, and the tags
// - these are not stored in attributes and do not have a 'required' field
// thus, if any of these are invalid, the first stage is not ready to complete

// Return if form isn't initialized yet
if (!form) {
return false;
}

if (form.title.$invalid) {
return false;
}

if (!form.content || form.content.$invalid) {
return false;
}

if (form.tags && form.tags.$invalid) {
return false;
}
}
// now checking all other post attributes that are required
return _.chain(attributes)
.where({form_stage_id : stageId, required: true})
.reduce(function (isValid, attr) {
// checkbox validity needs to be handled differently
// because it has multiple inputs identified via the options
if (attr.input === 'checkbox') {
var checkboxValidity = false;
_.each(attr.options, function (option) {
if (!_.isUndefined(form['values_' + attr.id + '_' + option]) && !form['values_' + attr.id + '_' + option].$invalid) {
checkboxValidity = isValid;
}
});
return checkboxValidity;
} else {
if (_.isUndefined(form['values_' + attr.id]) || form['values_' + attr.id].$invalid) {
return false;
}
return isValid;
}
}, true)
.value();
}
};

Expand Down
12 changes: 1 addition & 11 deletions app/post/modify/post-tabs.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ function PostVerticalTabsController(
_
) {
$scope.setVisibleStage = setVisibleStage;
$scope.isStageValid = isStageValid;
$scope.isFirstStage = isFirstStage;
$scope.stageIsComplete = stageIsComplete;
$scope.toggleStageCompletion = toggleStageCompletion;

Expand All @@ -49,14 +47,6 @@ function PostVerticalTabsController(
$scope.visibleStage = stageId;
}

function isFirstStage(stageId) {
return PostEditService.isFirstStage($scope.stages, stageId);
}

function isStageValid(stageId) {
return PostEditService.isStageValid(stageId, $scope.form, $scope.stages, $scope.attributes);
}

function stageIsComplete(stageId) {
return _.includes($scope.post.completed_stages, stageId);
}
Expand All @@ -67,7 +57,7 @@ function PostVerticalTabsController(
if (_.includes($scope.post.completed_stages, stageId)) {
$scope.post.completed_stages = _.without($scope.post.completed_stages, stageId);

} else if ($scope.isStageValid(stageId)) {
} else {
$scope.post.completed_stages.push(stageId);
}
}
Expand Down
2 changes: 1 addition & 1 deletion mocked_backend/api/v3/attributes.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"instructions": null,
"input": "text",
"type": "varchar",
"required": true,
"required": false,
"default": null,
"priority": 5,
"options": [
Expand Down
64 changes: 64 additions & 0 deletions mocked_backend/api/v3/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"count": 3,
"results": [
{
"id": 1,
"url": "http://192.168.33.110/api/v3/form_stages/1",
"form_id": 0,
"label": "Main",
"priority": 0,
"icon": null,
"type": "post",
"required": false,
"allowed_privileges": [
"read",
"create",
"update",
"delete",
"search"
]
},
{
"id": 2,
"url": "http://192.168.33.110/api/v3/form_stages/2",
"form_id": 1,
"label": "2nd step",
"priority": 1,
"icon": null,
"type": "standard",
"required": false,
"allowed_privileges": [
"read",
"create",
"update",
"delete",
"search"
]
},
{
"id": 3,
"url": "http://192.168.33.110/api/v3/form_stages/3",
"form_id": 2,
"label": "3rd step",
"priority": 2,
"icon": null,
"type": "standard",
"required": false,
"allowed_privileges": [
"read",
"create",
"update",
"delete",
"search"
]
}
],
"limit": null,
"offset": 0,
"order": "asc",
"orderby": "priority",
"curr": "http://192.168.33.110/api/v3/forms/1/stages?order=asc&orderby=priority&offset=0",
"next": "http://192.168.33.110/api/v3/forms/1/stages?order=asc&orderby=priority&offset=0",
"prev": "http://192.168.33.110/api/v3/forms/1/stages?order=asc&orderby=priority&offset=0",
"total_count": 3
}
3 changes: 1 addition & 2 deletions server/www/templates/posts/modify/post-tabs.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ <h3 class="form-sheet-title" translate="app.tasks">Tasks</h3>
<input
class="tgl"
id="switchT"
type="checkbox"
ng-disabled="! (isStageValid(visibleStage) || stageIsComplete(visibleStage))"
type="checkbox"
ng-checked="stageIsComplete(visibleStage)"
ng-click="toggleStageCompletion(visibleStage)">
<label class="tgl-btn" for="switchT"></label>
Expand Down
Loading

0 comments on commit 100410f

Please sign in to comment.