-
Notifications
You must be signed in to change notification settings - Fork 87
/
servicesTest.js
195 lines (164 loc) · 6.06 KB
/
servicesTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
describe('services', function() {
"use strict";
var $httpBackend, $q, $rootScope;
var columns, episodeData, records, list_schema, mockWindow;
var profile = {
readonly : false,
can_extract: true,
can_see_pid: function(){return true; }
};
beforeEach(function() {
module('opal');
columns = {
"fields": {
'demographics': {
name: "demographics",
single: true,
fields: [
{name: 'first_name', type: 'string'},
{name: 'surname', type: 'string'},
{name: 'date_of_birth', type: 'date'},
]
},
"diagnosis": {
name: "diagnosis",
single: false,
sort: 'date_of_diagnosis',
fields: [
{name: 'date_of_diagnosis', type: 'date'},
{name: 'condition', type: 'string'},
{name: 'provisional', type: 'boolean'},
]
}
}
};
records = columns.fields;
episodeData = {
id: 123,
date_of_admission: "19/11/2013",
active: true,
discharge_date: null,
date_of_episode: null,
tagging: [{
mine: true,
tropical: true
}],
demographics: [{
id: 101,
first_name: 'John',
surname: 'Smith',
date_of_birth: '31/071980',
hospital_number: '555'
}],
location: [{
category: 'Inepisode',
hospital: 'UCH',
ward: 'T10',
bed: '15',
date_of_admission: '01/08/2013',
}],
diagnosis: [{
id: 102,
condition: 'Dengue',
provisional: true,
date_of_diagnosis: '20/04/2007'
}, {
id: 103,
condition: 'Malaria',
provisional: false,
date_of_diagnosis: '19/03/2006'
}]
};
});
describe('extractSchemaLoader', function(){
var mock, extractSchemaLoader;
beforeEach(function(){
mock = { alert: jasmine.createSpy() };
module(function($provide) {
$provide.value('$window', mock);
});
module('opal.services', function($provide) {
$provide.value('UserProfile', function(){ return profile; });
});
inject(function($injector){
extractSchemaLoader = $injector.get('extractSchemaLoader');
$httpBackend = $injector.get('$httpBackend');
$rootScope = $injector.get('$rootScope');
$q = $injector.get('$q');
});
});
it('should fetch the schema', function(){
var result;
$httpBackend.whenGET('/api/v0.1/extract-schema/').respond(columns);
extractSchemaLoader.then(
function(r){ result = r; }
);
$rootScope.$apply();
$httpBackend.flush();
expect(result.columns).toEqual(columns);
});
it('should alert if the http request errors', function(){
var result;
$httpBackend.whenGET('/api/v0.1/extract-schema/').respond(500, 'NO');
extractSchemaLoader.then( function(r){ result = r; } );
$rootScope.$apply();
$httpBackend.flush();
expect(mock.alert).toHaveBeenCalledWith(
'Extract schema could not be loaded');
});
});
describe('episodeLoader', function() {
var episodeLoader, $httpBackend;
beforeEach(function() {
module(function($provide) {
$provide.value('UserProfile', function(){ return profile; });
});
inject(function($injector) {
episodeLoader = $injector.get('episodeLoader');
$httpBackend = $injector.get('$httpBackend');
$rootScope = $injector.get('$rootScope');
$route = $injector.get('$route');
});
});
xit('should resolve to a single episode', function() {
// TODO unskip this
// Skipping this, because I can't work out how to set $route.current
// so that episodeLoader can access it.
var promise = episodeLoader();
var episode;
$route.current = {params: {id: 123}};
$httpBackend.whenGET('/schema/').respond(columns);
// TODO trailing slash?
$httpBackend.whenGET('/episode/123').respond(episodeData);
promise.then(function(value) {
episode = value;
});
$httpBackend.flush();
$rootScope.$apply();
expect(episode.id).toBe(123);
});
});
describe('UserProfile', function(){
var mock, $httpBackend, UserProfile;
beforeEach(function(){
mock = { alert: jasmine.createSpy() };
module(function($provide){
$provide.value('$window', mock);
});
inject(function($injector){
UserProfile = $injector.get('UserProfile');
$q = $injector.get('$q');
$httpBackend = $injector.get('$httpBackend');
$rootScope = $injector.get('$rootScope');
});
});
it('should alert if the HTTP request errors', function(){
var result;
$httpBackend.expectGET('/api/v0.1/userprofile/');
$httpBackend.whenGET('/api/v0.1/userprofile/').respond(500, 'NO');
$rootScope.$apply();
$httpBackend.flush();
expect(mock.alert).toHaveBeenCalledWith('UserProfile could not be loaded');
});
});
});