Merge pull request #3040 from vector-im/rav/fix_test_fail

Hopefully, fix intermittent test failure
This commit is contained in:
David Baker 2017-01-24 13:48:01 +00:00 committed by GitHub
commit 536167a6c8

View File

@ -94,13 +94,13 @@ describe('loading:', function () {
loadCompletePromise = loadCompleteDefer.promise; loadCompletePromise = loadCompleteDefer.promise;
function onNewScreen(screen) { function onNewScreen(screen) {
console.log("newscreen "+screen); console.log(Date.now() + " newscreen "+screen);
if (!appLoaded) { if (!appLoaded) {
lastLoadedScreen = screen; lastLoadedScreen = screen;
} else { } else {
var hash = '#/' + screen; var hash = '#/' + screen;
windowLocation.hash = hash; windowLocation.hash = hash;
console.log("browser URI now "+ windowLocation); console.log(Date.now() + " browser URI now "+ windowLocation);
} }
} }
@ -122,22 +122,22 @@ describe('loading:', function () {
); );
function routeUrl(location, matrixChat) { function routeUrl(location, matrixChat) {
console.log("Routing URL "+location); console.log(Date.now() + " Routing URL "+location);
var fragparts = parseQsFromFragment(location); var fragparts = parseQsFromFragment(location);
matrixChat.showScreen(fragparts.location.substring(1), matrixChat.showScreen(fragparts.location.substring(1),
fragparts.params); fragparts.params);
} }
// pause for a cycle, then simulate the window.onload handler // pause for a cycle, then simulate the window.onload handler
q.delay(0).then(() => { window.setTimeout(() => {
console.log("simulating window.onload"); console.log(Date.now() + " simulating window.onload");
routeUrl(windowLocation, matrixChat); routeUrl(windowLocation, matrixChat);
appLoaded = true; appLoaded = true;
if (lastLoadedScreen) { if (lastLoadedScreen) {
onNewScreen(lastLoadedScreen); onNewScreen(lastLoadedScreen);
lastLoadedScreen = null; lastLoadedScreen = null;
} }
}).done(); }, 0);
} }
describe("Clean load with no stored credentials:", function() { describe("Clean load with no stored credentials:", function() {
@ -209,14 +209,11 @@ describe('loading:', function () {
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' }); httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
httpBackend.when('GET', '/sync').respond(200, {}); httpBackend.when('GET', '/sync').respond(200, {});
return httpBackend.flush(); return httpBackend.flush();
}).then(() => {
// Wait for another trip around the event loop for the UI to update
return q.delay(1);
}).then(() => { }).then(() => {
// once the sync completes, we should have a room view // once the sync completes, we should have a room view
return awaitRoomView(matrixChat);
}).then(() => {
httpBackend.verifyNoOutstandingExpectation(); httpBackend.verifyNoOutstandingExpectation();
ReactTestUtils.findRenderedComponentWithType(
matrixChat, sdk.getComponent('structures.RoomView'));
expect(windowLocation.hash).toEqual("#/room/!room:id"); expect(windowLocation.hash).toEqual("#/room/!room:id");
// and the localstorage should have been updated // and the localstorage should have been updated
@ -243,10 +240,8 @@ describe('loading:', function () {
loadApp(); loadApp();
q.delay(1).then(() => { return awaitSyncingSpinner(matrixChat).then(() => {
// we expect a spinner // we got a sync spinner - let the sync complete
assertAtSyncingSpinner(matrixChat);
return httpBackend.flush(); return httpBackend.flush();
}).then(() => { }).then(() => {
// once the sync completes, we should have a directory // once the sync completes, we should have a directory
@ -266,16 +261,14 @@ describe('loading:', function () {
uriFragment: "#/room/!room:id", uriFragment: "#/room/!room:id",
}); });
q.delay(1).then(() => { return awaitSyncingSpinner(matrixChat).then(() => {
// we expect a spinner // we got a sync spinner - let the sync complete
assertAtSyncingSpinner(matrixChat);
return httpBackend.flush(); return httpBackend.flush();
}).then(() => { }).then(() => {
// once the sync completes, we should have a room view // once the sync completes, we should have a room view
return awaitRoomView(matrixChat);
}).then(() => {
httpBackend.verifyNoOutstandingExpectation(); httpBackend.verifyNoOutstandingExpectation();
ReactTestUtils.findRenderedComponentWithType(
matrixChat, sdk.getComponent('structures.RoomView'));
expect(windowLocation.hash).toEqual("#/room/!room:id"); expect(windowLocation.hash).toEqual("#/room/!room:id");
}).done(done, done); }).done(done, done);
@ -300,12 +293,9 @@ describe('loading:', function () {
return httpBackend.flush(); return httpBackend.flush();
}).then(() => { }).then(() => {
// Wait for another trip around the event loop for the UI to update return awaitSyncingSpinner(matrixChat);
return q.delay(1);
}).then(() => { }).then(() => {
// now we should have a spinner with a logout link // we got a sync spinner - let the sync complete
assertAtSyncingSpinner(matrixChat);
httpBackend.when('GET', '/sync').respond(200, {}); httpBackend.when('GET', '/sync').respond(200, {});
return httpBackend.flush(); return httpBackend.flush();
}).then(() => { }).then(() => {
@ -338,12 +328,8 @@ describe('loading:', function () {
return httpBackend.flush(); return httpBackend.flush();
}).then(() => { }).then(() => {
// Wait for another trip around the event loop for the UI to update return awaitSyncingSpinner(matrixChat);
return q.delay(1);
}).then(() => { }).then(() => {
// now we should have a spinner with a logout link
assertAtSyncingSpinner(matrixChat);
httpBackend.when('GET', '/sync').check(function(req) { httpBackend.when('GET', '/sync').check(function(req) {
expect(req.path).toMatch(new RegExp("^https://homeserver/")); expect(req.path).toMatch(new RegExp("^https://homeserver/"));
}).respond(200, {}); }).respond(200, {});
@ -377,22 +363,15 @@ describe('loading:', function () {
return httpBackend.flush(); return httpBackend.flush();
}).then(() => { }).then(() => {
// Wait for another trip around the event loop for the UI to update return awaitSyncingSpinner(matrixChat);
return q.delay(1);
}).then(() => { }).then(() => {
// now we should have a spinner with a logout link
assertAtSyncingSpinner(matrixChat);
httpBackend.when('GET', '/sync').respond(200, {}); httpBackend.when('GET', '/sync').respond(200, {});
return httpBackend.flush(); return httpBackend.flush();
}).then(() => {
// Wait for another trip around the event loop for the UI to update
return q.delay(1);
}).then(() => { }).then(() => {
// once the sync completes, we should have a room view // once the sync completes, we should have a room view
return awaitRoomView(matrixChat);
}).then(() => {
httpBackend.verifyNoOutstandingExpectation(); httpBackend.verifyNoOutstandingExpectation();
ReactTestUtils.findRenderedComponentWithType(
matrixChat, sdk.getComponent('structures.RoomView'));
expect(windowLocation.hash).toEqual("#/room/!room:id"); expect(windowLocation.hash).toEqual("#/room/!room:id");
}).done(done, done); }).done(done, done);
}); });
@ -448,6 +427,32 @@ function assertAtLoadingSpinner(matrixChat) {
// we've got login creds, and are waiting for the sync to finish. // we've got login creds, and are waiting for the sync to finish.
// the page includes a logout link. // 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) { function assertAtSyncingSpinner(matrixChat) {
var domComponent = ReactDOM.findDOMNode(matrixChat); var domComponent = ReactDOM.findDOMNode(matrixChat);
expect(domComponent.className).toEqual("mx_MatrixChat_splash"); expect(domComponent.className).toEqual("mx_MatrixChat_splash");
@ -458,3 +463,30 @@ function assertAtSyncingSpinner(matrixChat) {
matrixChat, 'a'); matrixChat, 'a');
expect(logoutLink.text).toEqual("Logout"); 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();
}