forked from smartcontractkit/chainlink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add util methods to get/set session storage
- Loading branch information
Showing
7 changed files
with
84 additions
and
1 deletion.
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
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"}') | ||
}) | ||
}) | ||
}) |
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,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"}') | ||
}) | ||
}) | ||
}) |
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,5 @@ | ||
import * as storage from 'utils/storage' | ||
|
||
export const get = () => storage.get('session') | ||
|
||
export const set = obj => storage.set('session', obj) |
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,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)) | ||
} |
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
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