-
Notifications
You must be signed in to change notification settings - Fork 117
/
connectorTest.js
107 lines (94 loc) · 3.36 KB
/
connectorTest.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
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2017 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
describe('Connector', function() {
describe('#checkIsOnline()', function() {
before(function() {
return background(function() {
sinon.stub(Zotero.HTTP, 'request');
});
});
after(function() {
return background(function() {
Zotero.HTTP.request.restore();
});
});
it('responds with true when Zotero is online', Promise.coroutine(function*() {
let status = yield background(function() {
Zotero.HTTP.request.resolves({status: 200, getResponseHeader: () => 'application/json', responseText: '{}'});
return Zotero.Connector.checkIsOnline();
});
assert.isOk(status);
}));
it('responds with false when Zotero is offline', Promise.coroutine(function*() {
let status = yield background(function() {
Zotero.HTTP.request.resolves({status: 0});
return Zotero.Connector.checkIsOnline();
});
assert.isNotOk(status);
}));
it('throws when Zotero responds with a non-200 status', Promise.coroutine(function* () {
try {
yield background(function() {
Zotero.HTTP.request.resolves({status: 500, getResponseHeader: () => '', responseText: 'Error'});
return Zotero.Connector.checkIsOnline();
});
} catch (e) {
return
}
throw new Error('Expected error not thrown');
}));
});
describe('#getSelectedCollection()', function() {
it('throws if Zotero is offline', Promise.coroutine(function* () {
try {
yield background(function() {
Zotero.Connector.isOnline = false;
return Zotero.Connector.getSelectedCollection()
});
} catch (e) {
assert.equal(e.status, 0);
return;
}
throw new Error('Error not thrown');
}));
it('gets an SSE result if SSE available', Promise.coroutine(function*() {
let s = yield background(function() {
Zotero.Connector.isOnline = true;
Zotero.Connector.SSE.available = true;
Zotero.Connector.selected = {collection: 'selected'};
return Zotero.Connector.getSelectedCollection()
});
assert.equal(s, 'selected');
}));
it('calls Zotero if SSE unavailable', Promise.coroutine(function*() {
let call = yield background(function() {
Zotero.Connector.isOnline = true;
Zotero.Connector.SSE.available = false;
Zotero.Connector.selected = {collection: 'selected'};
sinon.stub(Zotero.Connector, 'callMethod');
return Zotero.Connector.getSelectedCollection().then(function() {
let call = Zotero.Connector.callMethod.lastCall;
Zotero.Connector.callMethod.restore();
return call
});
});
assert.equal(call.args[0], 'getSelectedCollection');
}));
});
});