-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #450 from openhealthcare/418-patientlist-loader
418 patientlist loader
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
opal/static/js/opaltest/patientlists.loader.service.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}); | ||
}); | ||
}) |