From 4d05602c803a6645d6c0e9404a60ed380f0329ee Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Mon, 2 Dec 2013 22:08:34 -0800 Subject: [PATCH] fix(client): redirect to redirect_url after all messages are sent --- client/karma.js | 17 +++++++++++------ test/client/karma.spec.js | 3 +-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/client/karma.js b/client/karma.js index 0969ab86b..37e9552e6 100644 --- a/client/karma.js +++ b/client/karma.js @@ -12,6 +12,7 @@ var Karma = function(socket, context, navigator, location) { var queryParams = util.parseQueryParams(location.search); var browserId = queryParams.id || util.generateId('manual-'); var returnUrl = queryParams.return_url || null; + var currentTransport; var resultsBufferLimit = 1; var resultsBuffer = []; @@ -129,11 +130,15 @@ var Karma = function(socket, context, navigator, location) { // tests could run in the same event loop, we wouldn't notice. setTimeout(function() { socket.emit('complete', result || {}); + clearContext(); + + // Redirect to the return_url, however we need to give the browser some time, + // so that all the messages are sent. + // TODO(vojta): can we rather get notification from socket.io? if (returnUrl) { - socket.disconnect(); - location.href = returnUrl; - } else { - clearContext(); + setTimeout(function() { + location.href = returnUrl; + }, (currentTransport === 'websocket' || currentTransport === 'flashsocket') ? 0 : 3000); } }, 0); }; @@ -197,10 +202,10 @@ var Karma = function(socket, context, navigator, location) { // report browser name, id socket.on('connect', function() { - var transport = socket.socket.transport.name; + currentTransport = socket.socket.transport.name; // TODO(vojta): make resultsBufferLimit configurable - if (transport === 'websocket' || transport === 'flashsocket') { + if (currentTransport === 'websocket' || currentTransport === 'flashsocket') { resultsBufferLimit = 1; } else { resultsBufferLimit = 50; diff --git a/test/client/karma.spec.js b/test/client/karma.spec.js index d7e14e2d5..b6c151d25 100644 --- a/test/client/karma.spec.js +++ b/test/client/karma.spec.js @@ -229,7 +229,7 @@ describe('Karma', function() { }); - it('should disconnect navigate the client to return_url if specified', function() { + it('should navigate the client to return_url if specified', function() { windowLocation.search = '?id=567&return_url=http://return.com'; socket = new MockSocket(); k = new Karma(socket, {}, windowNavigator, windowLocation); @@ -237,7 +237,6 @@ describe('Karma', function() { spyOn(socket, 'disconnect'); k.complete(); - expect(socket.disconnect).toHaveBeenCalled(); expect(windowLocation.href).toBe('http://return.com'); }); });