if preview count stored in localStorage is not a number override it

There are some cases when the preview count stored in local storage
evaluates to NaN. When this happens we should override the value
to zero, store it in localStorage, and return it.

Bug: T168371
Change-Id: Ic44b7c7b5b716f6a0859f33278d56d2d95bbfb3e
This commit is contained in:
Piotr Miazga 2017-08-17 18:29:56 +02:00
parent 08331ba199
commit 910dd0408f
4 changed files with 16 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View file

@ -66,21 +66,28 @@ export default function createUserSettings( storage ) {
* Gets the number of previews that the user has seen. * Gets the number of previews that the user has seen.
* *
* If the storage isn't available, then -1 is returned. * If the storage isn't available, then -1 is returned.
* If the value in storage is not a number it will override stored value to 0
* *
* @function * @function
* @name UserSettings#getPreviewCount * @name UserSettings#getPreviewCount
* @return {Number} * @return {Number}
*/ */
getPreviewCount: function () { getPreviewCount: function () {
var result = storage.get( PREVIEW_COUNT_KEY ); var result = storage.get( PREVIEW_COUNT_KEY ), count;
if ( result === false ) { if ( result === false ) {
return -1; return -1;
} else if ( result === null ) { } else if ( result === null ) {
return 0; return 0;
} }
count = parseInt( result, 10 );
return parseInt( result, 10 ); // stored number is not a zero, override it to zero and store new value
if ( Number.isNaN( count ) ) {
count = 0;
this.setPreviewCount( count );
}
return count;
}, },
/** /**

View file

@ -92,3 +92,10 @@ QUnit.test( '#setPreviewCount should store the count as a string', function ( as
assert.strictEqual( this.storage.get( 'ext.popups.core.previewCount' ), '222' ); assert.strictEqual( this.storage.get( 'ext.popups.core.previewCount' ), '222' );
} ); } );
QUnit.test( '#getPreviewCount should override value in storage when is not a number', function ( assert ) {
this.storage.set( 'ext.popups.core.previewCount', 'NaN' );
assert.strictEqual( this.userSettings.getPreviewCount(), 0 );
assert.strictEqual( this.storage.get( 'ext.popups.core.previewCount' ), '0' );
} );