Skip to content

Commit

Permalink
remove nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed Apr 21, 2017
1 parent 6ec762c commit 5f352b9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pathway/static/js/pathway/services/pathway.js
Expand Up @@ -55,8 +55,18 @@ angular.module('opal.services').service('Pathway', function(
}
});

var cleanedEditing = _.omit(editing, function(v, k, o){
if(_.isArray(v)){
v = _.compact(v);
return !v.length;
}
else{
return !v;
}
});

// cast the item to the fields for the server
var toSave = _.mapObject(editing, function(val, key){
var toSave = _.mapObject(cleanedEditing, function(val, key){
var result;
if(_.isArray(val)){
result = _.map(val, function(x){
Expand Down
70 changes: 69 additions & 1 deletion pathway/static/js/pathwaytest/pathway.service.test.js
Expand Up @@ -217,7 +217,75 @@ describe('Pathway', function() {
$httpBackend.expectPOST('/pathway/add_patient/sav', expected).respond("success");
pathwayScope.$apply();
$httpBackend.flush();
expect(result).toEqual('success')
expect(result).toEqual('success');
});

it("should remove _client from arrays if it exists", function(){
var editing = {something: [{
interesting: true,
_client: {id: 1},
}]};
var expected = {something: [{
interesting: true,
}]};
var result;
var response = pathway.pathwayPromise;
response.then(function(x){
result = x;
});
pathway.finish(editing);
$httpBackend.expectPOST('/pathway/add_patient/sav', expected).respond("success");
pathwayScope.$apply();
$httpBackend.flush();
expect(result).toEqual('success');
});

it("should remove _client from single items if they exists", function(){
var editing = {something: {
interesting: true,
_client: {id: 1},
}};
var expected = {something: [{
interesting: true,
}]};
var result;
var response = pathway.pathwayPromise;
response.then(function(x){
result = x;
});
pathway.finish(editing);
$httpBackend.expectPOST('/pathway/add_patient/sav', expected).respond("success");
pathwayScope.$apply();
$httpBackend.flush();
expect(result).toEqual('success');
});

it("should remove nulls single items if they exists", function(){
var editing = {something: null};
var result;
var response = pathway.pathwayPromise;
response.then(function(x){
result = x;
});
pathway.finish(editing);
$httpBackend.expectPOST('/pathway/add_patient/sav', {}).respond("success");
pathwayScope.$apply();
$httpBackend.flush();
expect(result).toEqual('success');
});

it("should remove nulls arrays if they exists", function(){
var editing = {something: [null]};
var result;
var response = pathway.pathwayPromise;
response.then(function(x){
result = x;
});
pathway.finish(editing);
$httpBackend.expectPOST('/pathway/add_patient/sav', {}).respond("success");
pathwayScope.$apply();
$httpBackend.flush();
expect(result).toEqual('success');
});
});
});

0 comments on commit 5f352b9

Please sign in to comment.