X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fqunit%2Fsuites%2Fresources%2Fmediawiki%2Fmediawiki.storage.test.js;h=fab7e1fef1a24af9cea872e7aca46dc23fe6bf8f;hb=add9bd191fd4d16250b301208600854cb52f6b33;hp=c25641db483e04f7fe07c0d90db6d55ca1d40dd2;hpb=607a84af5f027c1a942cb5b2cd46a4958c5af7b0;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js index c25641db48..fab7e1fef1 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js @@ -1,53 +1,56 @@ -( function ( mw ) { - QUnit.module( 'mediawiki.storage: normal case.', { - setup: function () { - this.sandbox.stub( mw.storage, 'isLocalStorageSupported', true ); - this.spy = this.sandbox.spy( localStorage, 'setItem' ); - this.sandbox.stub( localStorage, 'getItem' ) - .withArgs( 'foo' ).returns( 'test' ) - .withArgs( 'bar' ).returns( null ); - } - } ); +( function () { + QUnit.module( 'mediawiki.storage' ); + + QUnit.test( 'set/get with storage support', function ( assert ) { + var stub = { + setItem: this.sandbox.spy(), + getItem: this.sandbox.stub() + }; + stub.getItem.withArgs( 'foo' ).returns( 'test' ); + stub.getItem.returns( null ); + this.sandbox.stub( mw.storage, 'store', stub ); - QUnit.test( 'set/get with localStorage', 4, function ( assert ) { mw.storage.set( 'foo', 'test' ); - assert.strictEqual( this.spy.calledOnce, true, 'Check localStorage called.' ); - assert.strictEqual( this.spy.calledWith( 'foo', 'test' ), true, - 'Check prefixed.' ); + assert.ok( stub.setItem.calledOnce ); + assert.strictEqual( mw.storage.get( 'foo' ), 'test', 'Check value gets stored.' ); assert.strictEqual( mw.storage.get( 'bar' ), null, 'Unset values are null.' ); } ); - QUnit.module( 'mediawiki.storage: localStorage does not exist', { - setup: function () { - this.sandbox.stub( mw.storage, 'isLocalStorageSupported', false ); - this.sandbox.stub( localStorage, 'setItem' ).throws(); - } + QUnit.test( 'set/get with storage methods disabled', function ( assert ) { + // This covers browsers where storage is disabled + // (quota full, or security/privacy settings). + // On most browsers, these interface will be accessible with + // their methods throwing. + var stub = { + getItem: this.sandbox.stub(), + removeItem: this.sandbox.stub(), + setItem: this.sandbox.stub() + }; + stub.getItem.throws(); + stub.setItem.throws(); + stub.removeItem.throws(); + this.sandbox.stub( mw.storage, 'store', stub ); + + assert.strictEqual( mw.storage.get( 'foo' ), false ); + assert.strictEqual( mw.storage.set( 'foo', 'test' ), false ); + assert.strictEqual( mw.storage.remove( 'foo', 'test' ), false ); } ); - QUnit.test( 'set/get without localStorage', 3, function ( assert ) { - assert.strictEqual( mw.storage.set( 'foo', 'test' ), false, - 'When localStorage not available save fails.' ); - - assert.strictEqual( mw.storage.remove( 'foo', 'test' ), false, - 'When localStorage not available remove fails.' ); + QUnit.test( 'set/get with storage object disabled', function ( assert ) { + // On other browsers, these entire object is disabled. + // `'localStorage' in window` would be true (and pass feature test) + // but trying to read the object as window.localStorage would throw + // an exception. Such case would instantiate SafeStorage with + // undefined after the internal try/catch. + var old = mw.storage.store; + mw.storage.store = undefined; - assert.strictEqual( mw.storage.get( 'foo' ), false, - 'Inability to retrieve values return false to differentiate from null (not set).' ); - } ); - - QUnit.module( 'mediawiki.storage: localStorage exhausted', { - setup: function () { - this.sandbox.stub( mw.storage, 'isLocalStorageSupported', true ); - this.sandbox.stub( localStorage, 'setItem' ).throws(); - this.sandbox.stub( localStorage, 'getItem' ).returns( null ); - } - } ); + assert.strictEqual( mw.storage.get( 'foo' ), false ); + assert.strictEqual( mw.storage.set( 'foo', 'test' ), false ); + assert.strictEqual( mw.storage.remove( 'foo', 'test' ), false ); - QUnit.test( 'set/get without localStorage', 2, function ( assert ) { - assert.strictEqual( mw.storage.set( 'foo', 'test' ), false, - 'When localStorage not available inform user with false.' ); - assert.strictEqual( mw.storage.get( 'foo' ), null, 'No value registered.' ); + mw.storage.store = old; } ); -}( mediaWiki ) ); +}() );