diff options
author | Vincent Petry <pvince81@owncloud.com> | 2016-03-23 10:53:40 +0100 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2016-03-23 10:53:40 +0100 |
commit | 06e78564009fbc72d3f393c767c3995297b3f7a7 (patch) | |
tree | 07f3c66c92a2c08a05a52c813d36d74891041096 /core/js/tests | |
parent | d00f95578bc6812a10fa991c6df315a77ede38fb (diff) | |
download | nextcloud-server-06e78564009fbc72d3f393c767c3995297b3f7a7.tar.gz nextcloud-server-06e78564009fbc72d3f393c767c3995297b3f7a7.zip |
Adjust core unit tests for unload/reload cases
Diffstat (limited to 'core/js/tests')
-rw-r--r-- | core/js/tests/specs/coreSpec.js | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 774c2fdc72f..f18ecbc1a44 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -20,6 +20,15 @@ */ describe('Core base tests', function() { + afterEach(function() { + // many tests call window.initCore so need to unregister global events + // ideally in the future we'll need a window.unloadCore() function + $(document).off('ajaxError.main'); + $(document).off('unload.main'); + $(document).off('beforeunload.main'); + OC._userIsNavigatingAway = false; + OC._reloadCalled = false; + }); describe('Base values', function() { it('Sets webroots', function() { expect(OC.webroot).toBeDefined(); @@ -925,9 +934,10 @@ describe('Core base tests', function() { }); }); describe('global ajax errors', function() { - var reloadStub, ajaxErrorStub; + var reloadStub, ajaxErrorStub, clock; beforeEach(function() { + clock = sinon.useFakeTimers(); reloadStub = sinon.stub(OC, 'reload'); // unstub the error processing method ajaxErrorStub = OC._processAjaxError; @@ -936,15 +946,17 @@ describe('Core base tests', function() { }); afterEach(function() { reloadStub.restore(); - $(document).off('ajaxError'); + clock.restore(); }); - it('reloads current page in case of auth error', function () { + it('reloads current page in case of auth error', function() { var dataProvider = [ [200, false], [400, false], + [0, true], [401, true], [302, true], + [303, true], [307, true] ]; @@ -953,9 +965,13 @@ describe('Core base tests', function() { var expectedCall = dataProvider[i][1]; reloadStub.reset(); + OC._reloadCalled = false; $(document).trigger(new $.Event('ajaxError'), xhr); + // trigger timers + clock.tick(1000); + if (expectedCall) { expect(reloadStub.calledOnce).toEqual(true); } else { @@ -963,6 +979,27 @@ describe('Core base tests', function() { } } }); - }) + it('reload only called once in case of auth error', function() { + var xhr = { status: 401 }; + + $(document).trigger(new $.Event('ajaxError'), xhr); + $(document).trigger(new $.Event('ajaxError'), xhr); + + // trigger timers + clock.tick(1000); + + expect(reloadStub.calledOnce).toEqual(true); + }); + it('does not reload the page if the user was navigating away', function() { + var xhr = { status: 0 }; + OC._userIsNavigatingAway = true; + clock.tick(100); + + $(document).trigger(new $.Event('ajaxError'), xhr); + + clock.tick(1000); + expect(reloadStub.notCalled).toEqual(true); + }); + }); }); |