jenkins-bot has submitted this change and it was merged.

Change subject: QUnit: Add tests for settings.js
......................................................................


QUnit: Add tests for settings.js

Change-Id: Ia0f2340cc50383d030cddb942fdccca86a93e247
---
A tests/qunit/mobile.settings/test_settings.js
1 file changed, 140 insertions(+), 0 deletions(-)

Approvals:
  Jdlrobson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/tests/qunit/mobile.settings/test_settings.js 
b/tests/qunit/mobile.settings/test_settings.js
new file mode 100644
index 0000000..b67540b
--- /dev/null
+++ b/tests/qunit/mobile.settings/test_settings.js
@@ -0,0 +1,140 @@
+( function ( M, $ ) {
+
+       var settings = M.require( 'settings' ),
+               browser = M.require( 'browser' );
+
+       QUnit.module( 'MobileFrontend settings', {
+               setup: function () {
+                       var cookieCache = {},
+                               localStorageCache = {};
+
+                       // Stub cookie and localStorage
+                       this.sandbox.stub( $, 'cookie', function ( key, value ) 
{
+                               if ( value !== undefined ) {
+                                       // setter
+                                       cookieCache[key] = value;
+                               } else {
+                                       // getter
+                                       // $.cookie returns null for missing 
items
+                                       return cookieCache[key] || null;
+                               }
+                       } );
+                       this.sandbox.stub( $, 'removeCookie', function ( key ) {
+                               delete cookieCache[key];
+                       } );
+
+                       this.sandbox.stub( localStorage, 'setItem', function ( 
key, value ) {
+                               localStorageCache[key] = value;
+                       } );
+                       this.sandbox.stub( localStorage, 'getItem', function ( 
key ) {
+                               // localStorage returns null for missing items
+                               return localStorageCache[key] || null;
+                       } );
+                       this.sandbox.stub( localStorage, 'removeItem', function 
( key ) {
+                               delete localStorageCache[key];
+                       } );
+               },
+               tearDown: function () {
+                       this.sandbox.restore();
+               }
+       } );
+
+       QUnit.test( 'check cookies', 2, function ( assert ) {
+               assert.strictEqual(
+                       settings.cookiesEnabled(),
+                       true,
+                       'Cookies are enabled.'
+               );
+
+               $.cookie.restore();
+               this.sandbox.stub( $, 'cookie' );
+               assert.strictEqual(
+                       settings.cookiesEnabled(),
+                       false,
+                       'Cookies are disabled.'
+               );
+       } );
+
+       QUnit.test( 'localstorage', 3, function ( assert ) {
+               assert.strictEqual(
+                       settings.get( 'test_key' ),
+                       null,
+                       'Initially test_key is not in settings.'
+               );
+
+               settings.save( 'test_key', 'yep' );
+               assert.strictEqual(
+                       settings.get( 'test_key', 'yep' ),
+                       'yep',
+                       'test_key has the correct value.'
+               );
+
+               settings.remove( 'test_key' );
+               assert.strictEqual(
+                       settings.get( 'test_key' ),
+                       null,
+                       'test_key has been correctly removed.'
+               );
+       } );
+
+       QUnit.test( 'cookie fallback', 3, function ( assert ) {
+               assert.strictEqual(
+                       settings.get( 'test_key', true ),
+                       null,
+                       'Initially test_key is not in settings.'
+               );
+
+               settings.save( 'test_key', 'yep', true );
+               assert.strictEqual(
+                       settings.get( 'test_key', true ),
+                       'yep',
+                       'test_key has the correct value.'
+               );
+
+               settings.remove( 'test_key', true );
+               assert.strictEqual(
+                       settings.get( 'test_key', true ),
+                       null,
+                       'test_key has been correctly removed.'
+               );
+       } );
+
+       QUnit.module( 'MobileFrontend settings - localStorage', {
+               setup: function () {
+                       var localStorageCache = {};
+
+                       this.sandbox.stub( browser, 'supportsLocalStorage' 
).returns( true );
+                       this.sandbox.stub( localStorage, 'setItem' ).throws();
+                       this.sandbox.stub( localStorage, 'getItem', function ( 
key ) {
+                               // localStorage returns null for missing items
+                               return localStorageCache[key] || null;
+                       } );
+                       this.sandbox.stub( localStorage, 'removeItem', function 
( key ) {
+                               delete localStorageCache[key];
+                       } );
+
+               },
+               tearDown: function () {
+                       this.sandbox.restore();
+               }
+       } );
+       QUnit.test( 'without cookies or localStorage', 3, function ( assert ) {
+               assert.throws( function () {
+                       settings.set( 'test_key', 'yep' );
+               }, 'Throw an exception when localStorage is not available.' );
+
+               assert.strictEqual(
+                       settings.get( 'test_key' ),
+                       null,
+                       'Initially test_key is not in settings.'
+               );
+
+               settings.remove( 'test_key' );
+               assert.strictEqual(
+                       settings.get( 'test_key' ),
+                       null,
+                       'test_key has been correctly removed (even if it 
didn\'t exist in the first place'
+               );
+       } );
+
+}( mw.mobileFrontend, jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/229020
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia0f2340cc50383d030cddb942fdccca86a93e247
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Bmansurov <[email protected]>
Gerrit-Reviewer: Bmansurov <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Phuedx <[email protected]>
Gerrit-Reviewer: Robmoen <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to