Skip to content

Commit aa07266

Browse files
committed
Test that we handle stored mx_last_room_id correctly
* Do we show the last room correctly on resume? * Do we show the home page if we have no last room? * If we follow a login link, and re-log-in, do we correctly vape the mx_last_room_id (element-hq#4283)
1 parent f2b89ac commit aa07266

1 file changed

Lines changed: 95 additions & 29 deletions

File tree

‎test/app-tests/loading.js‎

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -222,33 +222,7 @@ describe('loading:', function () {
222222
// Wait for another trip around the event loop for the UI to update
223223
return q.delay(1);
224224
}).then(() => {
225-
// we expect a single <Login> component
226-
let login = ReactTestUtils.findRenderedComponentWithType(
227-
matrixChat, sdk.getComponent('structures.login.Login'));
228-
httpBackend.when('POST', '/login').check(function(req) {
229-
console.log(req);
230-
expect(req.data.type).toEqual('m.login.password');
231-
expect(req.data.identifier.type).toEqual('m.id.user');
232-
expect(req.data.identifier.user).toEqual('user');
233-
expect(req.data.password).toEqual('pass');
234-
}).respond(200, {
235-
user_id: '@user:id',
236-
device_id: 'DEVICE_ID',
237-
access_token: 'access_token',
238-
});
239-
login.onPasswordLogin("user", undefined, undefined, "pass")
240-
return httpBackend.flush();
241-
}).then(() => {
242-
// Wait for another trip around the event loop for the UI to update
243-
return q.delay(1);
244-
}).then(() => {
245-
// we expect a spinner
246-
ReactTestUtils.findRenderedComponentWithType(
247-
matrixChat, sdk.getComponent('elements.Spinner'));
248-
249-
httpBackend.when('GET', '/pushrules').respond(200, {});
250-
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
251-
return expectAndAwaitSync();
225+
return completeLogin(matrixChat);
252226
}).then(() => {
253227
// once the sync completes, we should have a room view
254228
return awaitRoomView(matrixChat);
@@ -271,9 +245,30 @@ describe('loading:', function () {
271245
localStorage.setItem("mx_is_url", "http://localhost" );
272246
localStorage.setItem("mx_access_token", "access_token");
273247
localStorage.setItem("mx_user_id", "@me:localhost");
248+
localStorage.setItem("mx_last_room_id", "!last_room:id");
249+
});
250+
251+
it('shows the last known room by default', function() {
252+
httpBackend.when('GET', '/pushrules').respond(200, {});
253+
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
254+
255+
loadApp();
256+
257+
return awaitSyncingSpinner(matrixChat).then(() => {
258+
// we got a sync spinner - let the sync complete
259+
return expectAndAwaitSync();
260+
}).then(() => {
261+
// once the sync completes, we should have a room view
262+
return awaitRoomView(matrixChat);
263+
}).then(() => {
264+
httpBackend.verifyNoOutstandingExpectation();
265+
expect(windowLocation.hash).toEqual("#/room/!last_room:id");
266+
});
274267
});
275268

276269
it('shows a home page by default if we have no joined rooms', function(done) {
270+
localStorage.removeItem("mx_last_room_id");
271+
277272
httpBackend.when('GET', '/pushrules').respond(200, {});
278273
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
279274

@@ -309,7 +304,37 @@ describe('loading:', function () {
309304
httpBackend.verifyNoOutstandingExpectation();
310305
expect(windowLocation.hash).toEqual("#/room/!room:id");
311306
}).done(done, done);
307+
});
308+
309+
describe('/#/login link:', function() {
310+
beforeEach(function() {
311+
httpBackend.when('GET', '/pushrules').respond(200, {});
312+
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
313+
314+
loadApp({
315+
uriFragment: "#/login",
316+
});
317+
318+
return expectAndAwaitSync().then(() => {
319+
return q.delay(1);
320+
});
321+
});
322+
323+
it('shows a login view', function() {
324+
// we expect a single <Login> component
325+
const login = ReactTestUtils.findRenderedComponentWithType(
326+
matrixChat, sdk.getComponent('structures.login.Login')
327+
);
328+
});
312329

330+
it('shows the homepage after login', function() {
331+
return completeLogin(matrixChat).then(() => {
332+
// we should see a home page if we previously had no room
333+
ReactTestUtils.findRenderedComponentWithType(
334+
matrixChat, sdk.getComponent('structures.HomePage'));
335+
expect(windowLocation.hash).toEqual("#/home");
336+
});
337+
});
313338
});
314339
});
315340

@@ -450,6 +475,45 @@ describe('loading:', function () {
450475
}).done(done, done);
451476
});
452477
});
478+
479+
480+
// check that we have a Login component, send a 'user:pass' login,
481+
// and await the HTTP requests.
482+
function completeLogin(matrixChat) {
483+
// we expect a single <Login> component
484+
const login = ReactTestUtils.findRenderedComponentWithType(
485+
matrixChat, sdk.getComponent('structures.login.Login'));
486+
487+
httpBackend.when('POST', '/login').check(function(req) {
488+
console.log(req);
489+
expect(req.data.type).toEqual('m.login.password');
490+
expect(req.data.identifier.type).toEqual('m.id.user');
491+
expect(req.data.identifier.user).toEqual('user');
492+
expect(req.data.password).toEqual('pass');
493+
}).respond(200, {
494+
user_id: '@user:id',
495+
device_id: 'DEVICE_ID',
496+
access_token: 'access_token',
497+
});
498+
login.onPasswordLogin("user", undefined, undefined, "pass");
499+
500+
return httpBackend.flush().then(() => {
501+
// Wait for another trip around the event loop for the UI to update
502+
return q.delay(1);
503+
}).then(() => {
504+
// we expect a spinner
505+
ReactTestUtils.findRenderedComponentWithType(
506+
matrixChat, sdk.getComponent('elements.Spinner'));
507+
508+
httpBackend.when('GET', '/pushrules').respond(200, {});
509+
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
510+
return expectAndAwaitSync().catch((e) => {
511+
throw new Error("Never got /sync after login: did the client start?");
512+
});
513+
}).then(() => {
514+
httpBackend.verifyNoOutstandingExpectation();
515+
});
516+
}
453517
});
454518

455519
// assert that we are on the loading page
@@ -465,7 +529,7 @@ function assertAtLoadingSpinner(matrixChat) {
465529
// the page includes a logout link.
466530
function awaitSyncingSpinner(matrixChat, retryLimit, retryCount) {
467531
if (retryLimit === undefined) {
468-
retryLimit = 5;
532+
retryLimit = 10;
469533
}
470534
if (retryCount === undefined) {
471535
retryCount = 0;
@@ -477,7 +541,9 @@ function awaitSyncingSpinner(matrixChat, retryLimit, retryCount) {
477541
throw new Error("MatrixChat still not loaded after " +
478542
retryCount + " tries");
479543
}
480-
return q.delay(0).then(() => {
544+
// loading can take quite a long time, because we delete the
545+
// indexedDB store.
546+
return q.delay(5).then(() => {
481547
return awaitSyncingSpinner(matrixChat, retryLimit, retryCount + 1);
482548
});
483549
}

0 commit comments

Comments
 (0)