Skip to content

Commit

Permalink
Merge pull request #450 from openhealthcare/418-patientlist-loader
Browse files Browse the repository at this point in the history
418 patientlist loader
  • Loading branch information
fredkingham committed Feb 25, 2016
2 parents 8f4dfbc + 73a3e78 commit 7ddddcd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions opal/core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class OpalApplication(object):
"js/opal/services/record_editor.js",
"js/opal/services/copy_to_category.js",
"js/opal/services/episode_detail.js",
"js/opal/services/patientlist_loader.js",
"js/search/services/paginator.js"
],
'opal.controllers': [
Expand Down
30 changes: 30 additions & 0 deletions opal/static/js/opal/services/patientlist_loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
angular.module('opal.services')
.factory('patientListLoader', function($q, $window,
$http,
$route,
Episode,
recordLoader) {
return function() {
"use strict";

var deferred = $q.defer();
var params = $route.current.params;
var target = '/api/v0.1/patientlist/' + params.list;

var getEpisodesPromise = $http.get(target);

$q.all([recordLoader, getEpisodesPromise]).then(function(results){
// record loader updates the global scope
var episodesResult = results[1];
var episodes = {};
_.each(episodesResult.data, function(resource) {
episodes[resource.id] = new Episode(resource);
});
deferred.resolve({status: 'success', data: episodes});
}, function(resource) {
deferred.resolve({status: 'error', data: resource.data});
});

return deferred.promise;
};
});
51 changes: 51 additions & 0 deletions opal/static/js/opaltest/patientlists.loader.service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
describe('PatientListLoaderTest', function(){
"use strict";

var patientListLoader, Episode, $route, $rootScope, $httpBackend;

beforeEach(function(){
module('opal');
inject(function($injector){
patientListLoader = $injector.get('patientListLoader');
Episode = $injector.get('Episode');
$route = $injector.get('$route');
$rootScope = $injector.get('$rootScope');
$httpBackend = $injector.get('$httpBackend');
});

$httpBackend.expectGET('/api/v0.1/userprofile/').respond({})
$httpBackend.expectGET('/api/v0.1/record/').respond({})
})

it('should fetch the episodes for a list', function(){
var result

var episodedata = {id: 1, demographics: [{patient_id: 1, name: 'Jane'}] };

$route.current = {params: {list: 'mylist'}}
$httpBackend.whenGET('/api/v0.1/patientlist/mylist').respond([episodedata])
patientListLoader().then(function(r){ result = r; })

$rootScope.$apply();
$httpBackend.flush();

expect(result.status).toEqual('success');
expect(result.data[1].id).toEqual(1);
expect(result.data[1].demographics[0].name).toEqual('Jane');
});


it('should resolve an error for nonexistant lists', function(){
var result

$route.current = {params: {list: 'mylist'}}
$httpBackend.whenGET('/api/v0.1/patientlist/mylist').respond(404, {error: "NOT FOUND"});
patientListLoader().then(function(r){ result = r; })

$rootScope.$apply();
$httpBackend.flush();

expect(result.status).toEqual('error');
expect(result.data).toEqual({error: "NOT FOUND"});
});
})

0 comments on commit 7ddddcd

Please sign in to comment.