2 QUnit
.module( 'mediawiki.storage' );
4 QUnit
.test( 'set/get with storage support', function ( assert
) {
6 setItem
: this.sandbox
.spy(),
7 getItem
: this.sandbox
.stub()
9 stub
.getItem
.withArgs( 'foo' ).returns( 'test' );
10 stub
.getItem
.returns( null );
11 this.sandbox
.stub( mw
.storage
, 'store', stub
);
13 mw
.storage
.set( 'foo', 'test' );
14 assert
.ok( stub
.setItem
.calledOnce
);
16 assert
.strictEqual( mw
.storage
.get( 'foo' ), 'test', 'Check value gets stored.' );
17 assert
.strictEqual( mw
.storage
.get( 'bar' ), null, 'Unset values are null.' );
20 QUnit
.test( 'set/get with storage methods disabled', function ( assert
) {
21 // This covers browsers where storage is disabled
22 // (quota full, or security/privacy settings).
23 // On most browsers, these interface will be accessible with
24 // their methods throwing.
26 getItem
: this.sandbox
.stub(),
27 removeItem
: this.sandbox
.stub(),
28 setItem
: this.sandbox
.stub()
30 stub
.getItem
.throws();
31 stub
.setItem
.throws();
32 stub
.removeItem
.throws();
33 this.sandbox
.stub( mw
.storage
, 'store', stub
);
35 assert
.strictEqual( mw
.storage
.get( 'foo' ), false );
36 assert
.strictEqual( mw
.storage
.set( 'foo', 'test' ), false );
37 assert
.strictEqual( mw
.storage
.remove( 'foo', 'test' ), false );
40 QUnit
.test( 'set/get with storage object disabled', function ( assert
) {
41 // On other browsers, these entire object is disabled.
42 // `'localStorage' in window` would be true (and pass feature test)
43 // but trying to read the object as window.localStorage would throw
44 // an exception. Such case would instantiate SafeStorage with
45 // undefined after the internal try/catch.
46 var old
= mw
.storage
.store
;
47 mw
.storage
.store
= undefined;
49 assert
.strictEqual( mw
.storage
.get( 'foo' ), false );
50 assert
.strictEqual( mw
.storage
.set( 'foo', 'test' ), false );
51 assert
.strictEqual( mw
.storage
.remove( 'foo', 'test' ), false );
53 mw
.storage
.store
= old
;