Add simplified storage API
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.storage.test.js
1 ( function ( mw ) {
2 QUnit.module( 'mediawiki.storage: normal case.', {
3 setup: function () {
4 this.sandbox.stub( mw.storage, 'isLocalStorageSupported', true );
5 this.spy = this.sandbox.spy( localStorage, 'setItem' );
6 this.sandbox.stub( localStorage, 'getItem' )
7 .withArgs( 'foo' ).returns( 'test' )
8 .withArgs( 'bar' ).returns( null );
9 }
10 } );
11
12 QUnit.test( 'set/get with localStorage', 4, function ( assert ) {
13 mw.storage.set( 'foo', 'test' );
14 assert.strictEqual( this.spy.calledOnce, true, 'Check localStorage called.' );
15 assert.strictEqual( this.spy.calledWith( 'foo', 'test' ), true,
16 'Check prefixed.' );
17 assert.strictEqual( mw.storage.get( 'foo' ), 'test', 'Check value gets stored.' );
18 assert.strictEqual( mw.storage.get( 'bar' ), null, 'Unset values are null.' );
19 } );
20
21 QUnit.module( 'mediawiki.storage: localStorage does not exist', {
22 setup: function () {
23 this.sandbox.stub( mw.storage, 'isLocalStorageSupported', false );
24 this.sandbox.stub( localStorage, 'setItem' ).throws();
25 }
26 } );
27
28 QUnit.test( 'set/get without localStorage', 3, function ( assert ) {
29 assert.strictEqual( mw.storage.set( 'foo', 'test' ), false,
30 'When localStorage not available save fails.' );
31
32 assert.strictEqual( mw.storage.remove( 'foo', 'test' ), false,
33 'When localStorage not available remove fails.' );
34
35 assert.strictEqual( mw.storage.get( 'foo' ), false,
36 'Inability to retrieve values return false to differentiate from null (not set).' );
37 } );
38
39 QUnit.module( 'mediawiki.storage: localStorage exhausted', {
40 setup: function () {
41 this.sandbox.stub( mw.storage, 'isLocalStorageSupported', true );
42 this.sandbox.stub( localStorage, 'setItem' ).throws();
43 this.sandbox.stub( localStorage, 'getItem' ).returns( null );
44 }
45 } );
46
47 QUnit.test( 'set/get without localStorage', 2, function ( assert ) {
48 assert.strictEqual( mw.storage.set( 'foo', 'test' ), false,
49 'When localStorage not available inform user with false.' );
50 assert.strictEqual( mw.storage.get( 'foo' ), null, 'No value registered.' );
51 } );
52
53 }( mediaWiki ) );