storage: Fix UserSettings#hasIsEnabled

mw.storage#get doesn't take a default value to return if the underlying
storage is disabled or the key is missing. In the former case it'll
return false and in the latter it'll return null, i.e. in neither case
will it return undefined.

Bug: T157700
Change-Id: I3f653c11468e17b64765e85ebb3b8f03e311352a
This commit is contained in:
Sam Smith 2017-03-01 10:36:59 +00:00
parent e4ea8df42e
commit f54f92402c
4 changed files with 22 additions and 8 deletions

Binary file not shown.

Binary file not shown.

View file

@ -45,7 +45,9 @@ module.exports = function ( storage ) {
* @return {Boolean}
*/
hasIsEnabled: function () {
return storage.get( IS_ENABLED_KEY, undefined ) !== undefined;
var value = storage.get( IS_ENABLED_KEY );
return Boolean( value ) !== false;
},
/**

View file

@ -9,11 +9,7 @@
}
} );
QUnit.test( '#getIsEnabled should return true if the storage is empty', 1, function ( assert ) {
assert.ok( this.userSettings.getIsEnabled() );
} );
QUnit.test( '#getIsEnabled should return false if Link Previews have been disabled', 2, function ( assert ) {
QUnit.test( '#getIsEnabled should return false if Page Previews have been disabled', 2, function ( assert ) {
this.userSettings.setIsEnabled( false );
assert.notOk( this.userSettings.getIsEnabled() );
@ -28,8 +24,13 @@
);
} );
QUnit.test( '#hasIsEnabled', 2, function ( assert ) {
assert.notOk( this.userSettings.hasIsEnabled() );
QUnit.test( '#hasIsEnabled', 3, function ( assert ) {
var getStub;
assert.notOk(
this.userSettings.hasIsEnabled(),
'#hasIsEnabled should return false if the storage is empty.'
);
// ---
@ -39,6 +40,17 @@
this.userSettings.hasIsEnabled(),
'#hasIsEnabled should return true even if "isEnabled" has been set to falsy.'
);
// ---
getStub = this.sandbox.stub( this.storage, 'get' ).returns( false );
assert.notOk(
this.userSettings.hasIsEnabled(),
'#hasIsEnabled should return false if the storage is disabled.'
);
getStub.restore();
} );
QUnit.test( '#getPreviewCount should return the count as a number', function ( assert ) {