Skip to content

Commit

Permalink
Add util methods to get/set session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
rupurt committed Aug 1, 2018
1 parent 566ff72 commit 3d43385
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
21 changes: 21 additions & 0 deletions gui/__tests__/utils/sessionStorage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { get, set } from 'utils/sessionStorage'

describe('utils/sessionStorage', () => {
beforeEach(() => {
global.localStorage.clear()
})

describe('get', () => {
it('returns a JS object for JSON stored as "chainlink.session" in localStorage', () => {
global.localStorage.setItem('chainlink.session', '{"foo":"FOO"}')
expect(get()).toEqual({foo: 'FOO'})
})
})

describe('set', () => {
it('saves the JS object as JSON under the key "chainlink.session" in localStorage', () => {
set({foo: 'FOO'})
expect(global.localStorage.getItem('chainlink.session')).toEqual('{"foo":"FOO"}')
})
})
})
30 changes: 30 additions & 0 deletions gui/__tests__/utils/storage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { get, set } from 'utils/storage'

describe('utils/storage', () => {
beforeEach(() => {
global.localStorage.clear()
})

describe('get', () => {
it('returns a JS object for JSON keyed under "chainlink." in localStorage', () => {
global.localStorage.setItem('chainlink.foo', '{"foo":"FOO"}')
expect(get('foo')).toEqual({foo: 'FOO'})
})

it('returns an empty JS object when not valid JSON', () => {
global.localStorage.setItem('chainlink.foo', '{"foo"}')
expect(get('foo')).toEqual({})
})

it('returns an empty JS object when the key does not exist', () => {
expect(get('foo')).toEqual({})
})
})

describe('set', () => {
it('saves the JS object as JSON keyed under "chainlink." in localStorage', () => {
set('foo', {foo: 'FOO'})
expect(global.localStorage.getItem('chainlink.foo')).toEqual('{"foo":"FOO"}')
})
})
})
5 changes: 5 additions & 0 deletions gui/src/utils/sessionStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as storage from 'utils/storage'

export const get = () => storage.get('session')

export const set = obj => storage.set('session', obj)
14 changes: 14 additions & 0 deletions gui/src/utils/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const get = key => {
const localStorageItem = global.localStorage.getItem(`chainlink.${key}`)
let obj = {}

if (localStorageItem) {
try { return JSON.parse(localStorageItem) } catch (e) {}
}

return obj
}

export const set = (key, obj) => {
global.localStorage.setItem(`chainlink.${key}`, JSON.stringify(obj))
}
1 change: 1 addition & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import 'mock-local-storage'

configure({ adapter: new Adapter() })

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"jest-environment-enzyme": "^6.0.0",
"jest-silent-reporter": "^0.0.4",
"json-pretty-html": "^1.1.2",
"mock-local-storage": "^1.0.5",
"moment": "^2.20.1",
"numeral": "^2.0.6",
"query-string": "^6.1.0",
Expand Down
13 changes: 12 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2811,6 +2811,10 @@ copy-to-clipboard@^3:
dependencies:
toggle-selection "^1.0.3"

core-js@^0.8.3:
version "0.8.4"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-0.8.4.tgz#c22665f1e0d1b9c3c5e1b08dabd1f108695e4fcf"

core-js@^1.0.0:
version "1.2.7"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-1.2.7.tgz#652294c14651db28fa93bd2d5ff2983a4f08c636"
Expand Down Expand Up @@ -5167,7 +5171,7 @@ global-prefix@^1.0.1:
is-windows "^1.0.1"
which "^1.2.14"

global@^4.3.0, global@~4.3.0:
global@^4.3.0, global@^4.3.2, global@~4.3.0:
version "4.3.2"
resolved "https://registry.yarnpkg.com/global/-/global-4.3.2.tgz#e76989268a6c74c38908b1305b10fc0e394e9d0f"
dependencies:
Expand Down Expand Up @@ -7804,6 +7808,13 @@ mock-fs@^4.1.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/mock-fs/-/mock-fs-4.5.0.tgz#75245b966f7e3defe197b03454af9c5b355594b7"

mock-local-storage@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/mock-local-storage/-/mock-local-storage-1.0.5.tgz#899ff300027cefed47816e6dc539bb059fcce489"
dependencies:
core-js "^0.8.3"
global "^4.3.2"

moment@^2.15.2, moment@^2.20.1:
version "2.22.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.1.tgz#529a2e9bf973f259c9643d237fda84de3a26e8ad"
Expand Down

0 comments on commit 3d43385

Please sign in to comment.