resources: Strip '$' and 'mw' from file closures
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.storage.test.js
1 ( function () {
2 QUnit.module( 'mediawiki.storage' );
3
4 QUnit.test( 'set/get with storage support', function ( assert ) {
5 var stub = {
6 setItem: this.sandbox.spy(),
7 getItem: this.sandbox.stub()
8 };
9 stub.getItem.withArgs( 'foo' ).returns( 'test' );
10 stub.getItem.returns( null );
11 this.sandbox.stub( mw.storage, 'store', stub );
12
13 mw.storage.set( 'foo', 'test' );
14 assert.ok( stub.setItem.calledOnce );
15
16 assert.strictEqual( mw.storage.get( 'foo' ), 'test', 'Check value gets stored.' );
17 assert.strictEqual( mw.storage.get( 'bar' ), null, 'Unset values are null.' );
18 } );
19
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.
25 var stub = {
26 getItem: this.sandbox.stub(),
27 removeItem: this.sandbox.stub(),
28 setItem: this.sandbox.stub()
29 };
30 stub.getItem.throws();
31 stub.setItem.throws();
32 stub.removeItem.throws();
33 this.sandbox.stub( mw.storage, 'store', stub );
34
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 );
38 } );
39
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;
48
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 );
52
53 mw.storage.store = old;
54 } );
55
56 }() );