Skip to content

Commit 80c192b

Browse files
committed
Port more tests to Mocha
1 parent 0ec5a58 commit 80c192b

File tree

2 files changed

+92
-101
lines changed

2 files changed

+92
-101
lines changed

test/unit/remotestorage-suite.js

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,6 @@ define(['require', 'tv4', './build/eventhandling', './build/util'],
7575
global.log = require('./build/log');
7676
global.Dropbox = require('./build/dropbox');
7777
global.RemoteStorage = require('./build/remotestorage').RemoteStorage;
78-
79-
global.Discover = function(userAddress) {
80-
var pending = Promise.defer();
81-
if (userAddress === "someone@somewhere") {
82-
pending.reject('in this test, discovery fails for that address');
83-
}
84-
return pending.promise;
85-
};
86-
8778
global.localStorage = {};
8879
global.RemoteStorage.prototype.remote = new FakeRemote();
8980
test.done();
@@ -93,7 +84,7 @@ define(['require', 'tv4', './build/eventhandling', './build/util'],
9384
var remoteStorage = new RemoteStorage({cache: false, disableFeatures: ['WireClient'] });
9485
env.rs = remoteStorage;
9586
config.cordovaRedirectUri = undefined;
96-
remoteStorage.on('ready', test.done );
87+
remoteStorage.on('ready', test.done);
9788
},
9889

9990
tests: [
@@ -145,7 +136,7 @@ define(['require', 'tv4', './build/eventhandling', './build/util'],
145136
},
146137

147138
{
148-
desc: "#get #put #delete not emmitting Error when getting 200",
139+
desc: "#get #put #delete not emitting Error when getting 200",
149140
run: function(env, test) {
150141
var success = true;
151142
var c = 0;
@@ -170,70 +161,6 @@ define(['require', 'tv4', './build/eventhandling', './build/util'],
170161
}
171162
},
172163

173-
{
174-
desc: "#connect throws unauthorized when userAddress doesn't contain an @ or URL",
175-
run: function(env, test) {
176-
env.rs.on('error', function(e) {
177-
test.assert(e instanceof RemoteStorage.DiscoveryError, true);
178-
});
179-
env.rs.connect('somestring');
180-
}
181-
},
182-
183-
{
184-
desc: "#connect accepts URLs for the userAddress",
185-
run: function(env, test) {
186-
env.rs.on('error', function(e) {
187-
test.fail('URL userAddress was not accepted.');
188-
});
189-
190-
env.rs.remote = new FakeRemote(false);
191-
env.rs.remote.configure = function (options) {
192-
test.assert(options.userAddress, 'https://personal.ho.st');
193-
}
194-
env.rs.connect('https://personal.ho.st');
195-
}
196-
},
197-
198-
{
199-
desc: "#connect adds missing https:// to URLs",
200-
run: function(env, test) {
201-
env.rs.on('error', function(e) {
202-
test.fail('URL userAddress was not accepted.');
203-
});
204-
205-
env.rs.remote = new FakeRemote(false);
206-
env.rs.remote.configure = function (options) {
207-
test.assert(options.userAddress, 'https://personal.ho.st');
208-
}
209-
env.rs.connect('personal.ho.st');
210-
}
211-
},
212-
213-
{
214-
desc: "#connect sets the backend to remotestorage",
215-
run: function(env, test) {
216-
global.localStorage = {
217-
storage: {},
218-
setItem: function(key, value) {
219-
this.storage[key] = value;
220-
},
221-
getItem: function(key) {
222-
return this.storage[key];
223-
},
224-
removeItem: function(key) {
225-
delete this.storage[key];
226-
}
227-
};
228-
229-
env.rs = new RemoteStorage();
230-
env.rs.remote = new FakeRemote(false);
231-
232-
env.rs.connect('[email protected]');
233-
test.assert(localStorage.getItem('remotestorage:backend'), 'remotestorage');
234-
}
235-
},
236-
237164
{
238165
desc: "#reconnect with remotestorage backend clears the token and connects again",
239166
run: function(env, test) {
@@ -401,30 +328,6 @@ define(['require', 'tv4', './build/eventhandling', './build/util'],
401328
}
402329
},
403330

404-
{
405-
desc: "#connect throws DiscoveryError on empty href",
406-
run: function(env, test) {
407-
env.rs.on('error', function(e) {
408-
if (e instanceof RemoteStorage.DiscoveryError) {
409-
test.done();
410-
}
411-
});
412-
env.rs.connect('someone@somewhere');
413-
}
414-
},
415-
416-
{
417-
desc: "#connect throws DiscoveryError on timeout of RemoteStorage.Discover",
418-
run: function(env, test) {
419-
config.discoveryTimeout = 500;
420-
env.rs.on('error', function(e) {
421-
test.assertAnd(e instanceof RemoteStorage.DiscoveryError, true);
422-
test.done();
423-
});
424-
env.rs.connect("someone@timeout");
425-
}
426-
},
427-
428331
{
429332
desc: "maxAge defaults to false when not connected",
430333
run: function(env, test) {

test/unit/remotestorage.test.mjs

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,30 @@ import chaiAsPromised from 'chai-as-promised';
44
import sinon from 'sinon';
55
import fetchMock from 'fetch-mock';
66

7-
import { Dropbox } from '../../build/dropbox.js';
7+
import Dropbox from '../../build/dropbox.js';
8+
import { EventHandling } from '../../build/eventhandling.js';
89
import { RemoteStorage } from '../../build/remotestorage.js';
10+
import { applyMixins } from '../../build/util.js';
911

1012
chai.use(chaiAsPromised);
1113

14+
class FakeRemote {
15+
constructor (connected) {
16+
this.fakeRemote = true;
17+
this.connected = (typeof connected === 'boolean') ? connected : true;
18+
this.configure = function() {};
19+
this.stopWaitingForToken = function() {
20+
if (!this.connected) { this._emit('not-connected'); }
21+
};
22+
this.addEvents(['connected', 'disconnected', 'not-connected']);
23+
}
24+
}
25+
applyMixins(FakeRemote, [ EventHandling ]);
26+
1227
describe("RemoteStorage", function() {
1328
const sandbox = sinon.createSandbox();
1429

1530
afterEach(function() {
16-
fetchMock.reset();
1731
sandbox.restore();
1832
});
1933

@@ -51,6 +65,80 @@ describe("RemoteStorage", function() {
5165
});
5266
});
5367

68+
describe("#connect", function() {
69+
before(function() {
70+
fetchMock.mock(/acct\:timeout@example\.com/, 200, {
71+
delay: 1000
72+
});
73+
fetchMock.mock(/personal\.ho\.st/, 200);
74+
fetchMock.mock(/acct\:user@ho\.st/, 200);
75+
});
76+
77+
beforeEach(function() {
78+
this.rs = new RemoteStorage({
79+
cache: false,
80+
discoveryTimeout: 500
81+
});
82+
});
83+
84+
it("throws DiscoveryError when userAddress doesn't contain an @ or URL", function(done) {
85+
this.rs.on('error', function(e) {
86+
expect(e).to.be.an.instanceof(RemoteStorage.DiscoveryError);
87+
expect(e.message).to.match(/Not a valid user address/);
88+
done();
89+
});
90+
91+
this.rs.connect('somestring');
92+
});
93+
94+
it("throws DiscoveryError on timeout of RemoteStorage.Discover", function(done) {
95+
this.rs.on('error', function(e) {
96+
expect(e).to.be.an.instanceof(RemoteStorage.DiscoveryError);
97+
expect(e.message).to.match(/No storage information found/);
98+
done();
99+
});
100+
101+
this.rs.connect("[email protected]");
102+
});
103+
104+
it("accepts URLs for the userAddress", function(done) {
105+
this.rs.on('error', function(/* err */) {
106+
throw new Error('URL userAddress was not accepted.');
107+
});
108+
109+
this.rs.remote = new FakeRemote(false);
110+
this.rs.remote.configure = function (options) {
111+
expect(options.userAddress).to.equal('https://personal.ho.st');
112+
done();
113+
};
114+
115+
this.rs.connect('https://personal.ho.st');
116+
});
117+
118+
it("adds missing https:// to URLs", function(done) {
119+
this.rs.on('error', function(/* err */) {
120+
throw new Error('URL userAddress was not accepted.');
121+
});
122+
123+
this.rs.remote = new FakeRemote(false);
124+
this.rs.remote.configure = function (options) {
125+
expect(options.userAddress).to.equal('https://personal.ho.st');
126+
done();
127+
};
128+
129+
this.rs.connect('personal.ho.st');
130+
});
131+
132+
it("sets the backend to remotestorage", function() {
133+
this.rs.remote = new FakeRemote(false);
134+
this.rs.backend = undefined;
135+
136+
this.rs.connect('[email protected]');
137+
138+
expect(this.rs.backend).to.equal('remotestorage');
139+
});
140+
});
141+
54142
describe("#setApiKeys", function() {
55143
before(function() {
56144
this.dropboxRsInit = Dropbox._rs_init;

0 commit comments

Comments
 (0)