Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hopefully, fix intermittent test failure #3040

Merged
merged 2 commits into from
Jan 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 73 additions & 41 deletions test/app-tests/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ describe('loading:', function () {
loadCompletePromise = loadCompleteDefer.promise;

function onNewScreen(screen) {
console.log("newscreen "+screen);
console.log(Date.now() + " newscreen "+screen);
if (!appLoaded) {
lastLoadedScreen = screen;
} else {
var hash = '#/' + screen;
windowLocation.hash = hash;
console.log("browser URI now "+ windowLocation);
console.log(Date.now() + " browser URI now "+ windowLocation);
}
}

Expand All @@ -122,22 +122,22 @@ describe('loading:', function () {
);

function routeUrl(location, matrixChat) {
console.log("Routing URL "+location);
console.log(Date.now() + " Routing URL "+location);
var fragparts = parseQsFromFragment(location);
matrixChat.showScreen(fragparts.location.substring(1),
fragparts.params);
}

// pause for a cycle, then simulate the window.onload handler
q.delay(0).then(() => {
console.log("simulating window.onload");
window.setTimeout(() => {
console.log(Date.now() + " simulating window.onload");
routeUrl(windowLocation, matrixChat);
appLoaded = true;
if (lastLoadedScreen) {
onNewScreen(lastLoadedScreen);
lastLoadedScreen = null;
}
}).done();
}, 0);
}

describe("Clean load with no stored credentials:", function() {
Expand Down Expand Up @@ -209,14 +209,11 @@ describe('loading:', function () {
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
httpBackend.when('GET', '/sync').respond(200, {});
return httpBackend.flush();
}).then(() => {
// Wait for another trip around the event loop for the UI to update
return q.delay(1);
}).then(() => {
// once the sync completes, we should have a room view
return awaitRoomView(matrixChat);
}).then(() => {
httpBackend.verifyNoOutstandingExpectation();
ReactTestUtils.findRenderedComponentWithType(
matrixChat, sdk.getComponent('structures.RoomView'));
expect(windowLocation.hash).toEqual("#/room/!room:id");

// and the localstorage should have been updated
Expand All @@ -243,10 +240,8 @@ describe('loading:', function () {

loadApp();

q.delay(1).then(() => {
// we expect a spinner
assertAtSyncingSpinner(matrixChat);

return awaitSyncingSpinner(matrixChat).then(() => {
// we got a sync spinner - let the sync complete
return httpBackend.flush();
}).then(() => {
// once the sync completes, we should have a directory
Expand All @@ -266,16 +261,14 @@ describe('loading:', function () {
uriFragment: "#/room/!room:id",
});

q.delay(1).then(() => {
// we expect a spinner
assertAtSyncingSpinner(matrixChat);

return awaitSyncingSpinner(matrixChat).then(() => {
// we got a sync spinner - let the sync complete
return httpBackend.flush();
}).then(() => {
// once the sync completes, we should have a room view
return awaitRoomView(matrixChat);
}).then(() => {
httpBackend.verifyNoOutstandingExpectation();
ReactTestUtils.findRenderedComponentWithType(
matrixChat, sdk.getComponent('structures.RoomView'));
expect(windowLocation.hash).toEqual("#/room/!room:id");
}).done(done, done);

Expand All @@ -300,12 +293,9 @@ describe('loading:', function () {

return httpBackend.flush();
}).then(() => {
// Wait for another trip around the event loop for the UI to update
return q.delay(1);
return awaitSyncingSpinner(matrixChat);
}).then(() => {
// now we should have a spinner with a logout link
assertAtSyncingSpinner(matrixChat);

// we got a sync spinner - let the sync complete
httpBackend.when('GET', '/sync').respond(200, {});
return httpBackend.flush();
}).then(() => {
Expand Down Expand Up @@ -338,12 +328,8 @@ describe('loading:', function () {

return httpBackend.flush();
}).then(() => {
// Wait for another trip around the event loop for the UI to update
return q.delay(1);
return awaitSyncingSpinner(matrixChat);
}).then(() => {
// now we should have a spinner with a logout link
assertAtSyncingSpinner(matrixChat);

httpBackend.when('GET', '/sync').check(function(req) {
expect(req.path).toMatch(new RegExp("^https://homeserver/"));
}).respond(200, {});
Expand Down Expand Up @@ -377,22 +363,15 @@ describe('loading:', function () {

return httpBackend.flush();
}).then(() => {
// Wait for another trip around the event loop for the UI to update
return q.delay(1);
return awaitSyncingSpinner(matrixChat);
}).then(() => {
// now we should have a spinner with a logout link
assertAtSyncingSpinner(matrixChat);

httpBackend.when('GET', '/sync').respond(200, {});
return httpBackend.flush();
}).then(() => {
// Wait for another trip around the event loop for the UI to update
return q.delay(1);
}).then(() => {
// once the sync completes, we should have a room view
return awaitRoomView(matrixChat);
}).then(() => {
httpBackend.verifyNoOutstandingExpectation();
ReactTestUtils.findRenderedComponentWithType(
matrixChat, sdk.getComponent('structures.RoomView'));
expect(windowLocation.hash).toEqual("#/room/!room:id");
}).done(done, done);
});
Expand Down Expand Up @@ -448,6 +427,32 @@ function assertAtLoadingSpinner(matrixChat) {

// we've got login creds, and are waiting for the sync to finish.
// the page includes a logout link.
function awaitSyncingSpinner(matrixChat, retryLimit, retryCount) {
if (retryLimit === undefined) {
retryLimit = 5;
}
if (retryCount === undefined) {
retryCount = 0;
}

if (matrixChat.state.loading) {
console.log(Date.now() + " Awaiting sync spinner: still loading.");
if (retryCount >= retryLimit) {
throw new Error("MatrixChat still not loaded after " +
retryCount + " tries");
}
return q.delay(0).then(() => {
return awaitSyncingSpinner(matrixChat, retryLimit, retryCount + 1);
});
}

console.log(Date.now() + " Awaiting sync spinner: load complete.");

// state looks good, check the rendered output
assertAtSyncingSpinner(matrixChat);
return q();
}

function assertAtSyncingSpinner(matrixChat) {
var domComponent = ReactDOM.findDOMNode(matrixChat);
expect(domComponent.className).toEqual("mx_MatrixChat_splash");
Expand All @@ -458,3 +463,30 @@ function assertAtSyncingSpinner(matrixChat) {
matrixChat, 'a');
expect(logoutLink.text).toEqual("Logout");
}

function awaitRoomView(matrixChat, retryLimit, retryCount) {
if (retryLimit === undefined) {
retryLimit = 5;
}
if (retryCount === undefined) {
retryCount = 0;
}

if (!matrixChat.state.ready) {
console.log(Date.now() + " Awaiting room view: not ready yet.");
if (retryCount >= retryLimit) {
throw new Error("MatrixChat still not ready after " +
retryCount + " tries");
}
return q.delay(0).then(() => {
return awaitRoomView(matrixChat, retryLimit, retryCount + 1);
});
}

console.log(Date.now() + " Awaiting room view: now ready.");

// state looks good, check the rendered output
ReactTestUtils.findRenderedComponentWithType(
matrixChat, sdk.getComponent('structures.RoomView'));
return q();
}