-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to save filter state
- Loading branch information
pavelbely
committed
Aug 8, 2019
1 parent
86d5c00
commit fa773be
Showing
6 changed files
with
255 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
|
||
const _prefix = 'html-reporter'; | ||
const _deprecatedKeysCollection = [ | ||
{deprecatedKey: '_gemini-replace-host', newKey: _getStorageKey('replace-host')} | ||
]; | ||
|
||
/** | ||
* Format Storage key | ||
* @param key | ||
* @returns {string} | ||
* @private | ||
*/ | ||
function _getStorageKey(key) { | ||
return `${_prefix}:${key}`; | ||
} | ||
|
||
/** | ||
* Helping to migrate on new storage keys | ||
* @private | ||
*/ | ||
export function updateDeprecatedKeys() { | ||
_deprecatedKeysCollection.forEach(({deprecatedKey, newKey}) => { | ||
if (window.localStorage.hasOwnProperty(deprecatedKey)) { | ||
window.localStorage.setItem(newKey, JSON.stringify(window.localStorage.getItem(deprecatedKey))); | ||
window.localStorage.removeItem(deprecatedKey); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Wrap localStorage#setItem method | ||
* Support value serialization | ||
* @param key | ||
* @param value | ||
*/ | ||
export function setItem(key, value) { | ||
window.localStorage.setItem(_getStorageKey(key), JSON.stringify(value)); | ||
} | ||
|
||
/** | ||
* Wrap localStorage#getItem method | ||
* Parse deserialize storage value | ||
* If key doesn't exist in storage return defaultValue | ||
* @param key | ||
* @param defaultValue | ||
* @returns {object|string} | ||
*/ | ||
export function getItem(key, defaultValue) { | ||
if (!hasItem(key)) { | ||
return defaultValue; | ||
} | ||
|
||
const item = window.localStorage.getItem(_getStorageKey(key)); | ||
|
||
try { | ||
return JSON.parse(item); | ||
} catch (e) { | ||
return item; | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether the key is contained in storage. | ||
* @param key | ||
* @returns {boolean} | ||
*/ | ||
export function hasItem(key) { | ||
return window.localStorage.hasOwnProperty(_getStorageKey(key)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
import * as localStorageWrapper from 'lib/static/modules/local-storage-wrapper'; | ||
import {mkStorage} from '../../../utils'; | ||
|
||
describe('lib/static/modules/local-storage-wrapper', () => { | ||
const prefix = 'html-reporter'; | ||
|
||
beforeEach(() => { | ||
global.window.localStorage = mkStorage(); | ||
}); | ||
|
||
afterEach(() => { | ||
global.window.localStorage = undefined; | ||
}); | ||
|
||
describe('updateDeprecatedKeys', () => { | ||
it('should update storage keys if deprecated', () => { | ||
global.window.localStorage.setItem('_gemini-replace-host', 'foo1'); | ||
global.window.localStorage.setItem('foo2', 'foo2'); | ||
|
||
localStorageWrapper.updateDeprecatedKeys(); | ||
|
||
assert.equal(global.window.localStorage.getItem(`${prefix}:replace-host`), '"foo1"'); | ||
assert.equal(global.window.localStorage.getItem('foo2'), 'foo2'); | ||
}); | ||
}); | ||
|
||
describe('setItem', () => { | ||
it('should convert value to json and set value to localStorage', () => { | ||
localStorageWrapper.setItem('foo', {bar: []}); | ||
|
||
assert.equal(global.window.localStorage.getItem(`${prefix}:foo`), '{"bar":[]}'); | ||
}); | ||
}); | ||
|
||
describe('getItem', () => { | ||
it('should return parsed value from localStorage', () => { | ||
const value = {bar: []}; | ||
|
||
localStorageWrapper.setItem('foo', value); | ||
|
||
assert.deepEqual(localStorageWrapper.getItem('foo'), value); | ||
}); | ||
|
||
it('should return default value if key doesn\'t exist in localStorage', () => { | ||
assert.equal(localStorageWrapper.getItem('bar', 'baz'), 'baz'); | ||
}); | ||
|
||
it('should return localStorage value if parsing occur an error', () => { | ||
const value = {bar: []}; | ||
|
||
localStorageWrapper.setItem('foo', value); | ||
global.window.localStorage.setItem(`${prefix}:foo`, '{foo}'); | ||
|
||
assert.equal(localStorageWrapper.getItem('foo'), '{foo}'); | ||
}); | ||
}); | ||
|
||
describe('hasItem', () => { | ||
it('should return true if key exist in localStorage', () => { | ||
localStorageWrapper.setItem('foo', 'bar'); | ||
|
||
assert.isTrue(localStorageWrapper.hasItem('foo')); | ||
}); | ||
|
||
it('should return false if key doesn\'t exist in localStorage', () => { | ||
assert.isFalse(localStorageWrapper.hasItem('foo')); | ||
assert.equal(localStorageWrapper.getItem('foo'), undefined); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
import actionNames from 'lib/static/modules/action-names'; | ||
import defaultState from 'lib/static/modules/default-state'; | ||
|
||
const proxyquire = require('proxyquire'); | ||
const reducer = proxyquire('lib/static/modules/reducer', { | ||
'./local-storage-wrapper': { | ||
setItem: sinon.stub(), | ||
getItem: sinon.stub() | ||
} | ||
}).default; | ||
|
||
describe('lib/static/modules/reducer', () => { | ||
describe('reducer', () => { | ||
describe('regression', () => { | ||
it('shouldn\'t change "Expand" filter when "Show all" or "Show only failed" state changing', function() { | ||
let newState = [ | ||
{type: actionNames.VIEW_EXPAND_RETRIES}, | ||
{type: actionNames.VIEW_SHOW_ALL} | ||
].reduce(reducer, defaultState); | ||
|
||
assert.equal(newState.view.expand, 'retries'); | ||
|
||
newState = [ | ||
{type: actionNames.VIEW_EXPAND_RETRIES}, | ||
{type: actionNames.VIEW_SHOW_FAILED} | ||
].reduce(reducer, defaultState); | ||
|
||
assert.equal(newState.view.expand, 'retries'); | ||
}); | ||
}); | ||
|
||
describe('VIEW_SHOW_ALL', () => { | ||
it('should change "viewMode" field on "all" value', () => { | ||
const action = {type: actionNames.VIEW_SHOW_ALL}; | ||
|
||
const newState = reducer(defaultState, action); | ||
|
||
assert.equal(newState.view.viewMode, 'all'); | ||
}); | ||
}); | ||
|
||
describe('VIEW_SHOW_FAILED', () => { | ||
it('should change "viewMode" field on "failed" value', () => { | ||
const action = {type: actionNames.VIEW_SHOW_FAILED}; | ||
|
||
const newState = reducer(defaultState, action); | ||
|
||
assert.equal(newState.view.viewMode, 'failed'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters