@equinor/echo-base
Advanced tools
Comparing version 0.6.2 to 0.6.3
import { parseJsonWithDate } from './jsonParseUtils'; | ||
export var storage = { | ||
setItem: function (key, data) { | ||
if (typeof data === 'string') { | ||
localStorage.setItem(key, data); | ||
} | ||
else { | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
} | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
}, | ||
@@ -11,0 +6,0 @@ getItem: function (key) { |
@@ -7,8 +7,3 @@ "use strict"; | ||
setItem: function (key, data) { | ||
if (typeof data === 'string') { | ||
localStorage.setItem(key, data); | ||
} | ||
else { | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
} | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
}, | ||
@@ -15,0 +10,0 @@ getItem: function (key) { |
{ | ||
"name": "@equinor/echo-base", | ||
"version": "0.6.2", | ||
"version": "0.6.3", | ||
"module": "esm/index.js", | ||
@@ -34,4 +34,4 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@types/node": "^17.0.12", | ||
"typedoc": "^0.22.11", | ||
"@types/node": "^17.0.20", | ||
"typedoc": "^0.22.12", | ||
"typescript": "^4.5.5" | ||
@@ -38,0 +38,0 @@ }, |
import { storage } from '../../utils/storage'; | ||
describe('Storage', () => { | ||
function mockLocalStorage<T>(mockObj: T): T { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(window as any).__defineGetter__('localStorage', () => mockObj); | ||
return mockObj; | ||
beforeEach(() => { | ||
Object.defineProperty(window, 'localStorage', { | ||
value: { | ||
setItem: jest.fn(), | ||
getItem: jest.fn(), | ||
removeItem: jest.fn() | ||
} | ||
}); | ||
}); | ||
function mockLocalStorage() { | ||
let valueInLocalStorage: string; | ||
(localStorage.getItem as jest.Mock).mockImplementationOnce(() => valueInLocalStorage); | ||
(localStorage.setItem as jest.Mock).mockImplementationOnce( | ||
(key: string, value: string) => (valueInLocalStorage = value) | ||
); | ||
} | ||
describe('EchoLocalStorage', () => { | ||
it('getItem string from local storage', () => { | ||
const localStorage = mockLocalStorage({ | ||
getItem: jest.fn(() => 'bar') | ||
}); | ||
const result = storage.getItem<string>('foo'); | ||
expect(result).toEqual('bar'); | ||
expect(localStorage.getItem).toHaveBeenCalledTimes(1); | ||
it('should get the correct string from localStorage', () => { | ||
// given | ||
const valueToSet = 'bar'; | ||
mockLocalStorage(); | ||
storage.setItem<string>('foo', valueToSet); | ||
// when | ||
const actual = storage.getItem<string>('foo'); | ||
// then | ||
expect(actual).toEqual(valueToSet); | ||
}); | ||
it('getItem from local storage', () => { | ||
const item = { item: 'bar' }; | ||
const localStorage = mockLocalStorage({ | ||
getItem: jest.fn(() => JSON.stringify(item)) | ||
}); | ||
const result = storage.getItem<{ item: string }>('foo'); | ||
it('should get the correct object from localStorage', () => { | ||
// given | ||
const valueToSet = { foo: 'bar', aDate: new Date() }; | ||
mockLocalStorage(); | ||
storage.setItem<{ foo: string }>('foo', valueToSet); | ||
expect(result).toEqual(item); | ||
expect(localStorage.getItem).toHaveBeenCalledTimes(1); | ||
// when | ||
const actual = storage.getItem<{ foo: string }>('foo'); | ||
// then | ||
expect(actual).toEqual(valueToSet); | ||
}); | ||
it('getItem from local storage as undefined', () => { | ||
const localStorage = mockLocalStorage({ | ||
getItem: jest.fn(() => undefined) | ||
}); | ||
const result = storage.getItem('foo'); | ||
it('should get the correct boolean from localStorage', () => { | ||
// given | ||
const valueToSet = true; | ||
mockLocalStorage(); | ||
storage.setItem<boolean>('foo', valueToSet); | ||
expect(undefined).toEqual(result); | ||
expect(localStorage.getItem).toHaveBeenCalledTimes(1); | ||
// when | ||
const actual = storage.getItem<boolean>('foo'); | ||
// then | ||
expect(actual).toEqual(valueToSet); | ||
}); | ||
it('removeItem has been called with key', () => { | ||
const localStorage = mockLocalStorage({ | ||
removeItem: jest.fn() | ||
}); | ||
storage.removeItem('foo'); | ||
it('should get the correct number from localStorage', () => { | ||
// given | ||
const valueToSet = 1234; | ||
mockLocalStorage(); | ||
storage.setItem<number>('foo', valueToSet); | ||
expect(localStorage.removeItem).toBeCalledWith('foo'); | ||
expect(localStorage.removeItem).toHaveBeenCalledTimes(1); | ||
// when | ||
const actual = storage.getItem<number>('foo'); | ||
// then | ||
expect(actual).toEqual(valueToSet); | ||
}); | ||
it('setItem has been called with key and string', () => { | ||
const localStorage = mockLocalStorage({ | ||
setItem: jest.fn() | ||
}); | ||
storage.setItem<string>('foo', 'bar'); | ||
it('should get the correct date from localStorage', () => { | ||
// given | ||
const valueToSet = new Date(); | ||
mockLocalStorage(); | ||
storage.setItem<Date>('foo', valueToSet); | ||
expect(localStorage.setItem).toBeCalledWith('foo', 'bar'); | ||
expect(localStorage.setItem).toHaveBeenCalledTimes(1); | ||
// when | ||
const actual = storage.getItem<Date>('foo'); | ||
// then | ||
expect(actual).toEqual(valueToSet); | ||
}); | ||
it('setItem has been called with key and data', () => { | ||
const data = { item: 'bar' }; | ||
const localStorage = mockLocalStorage({ | ||
setItem: jest.fn() | ||
}); | ||
storage.setItem('foo', data); | ||
it('should call localStorage.removeItem with the given key', () => { | ||
storage.removeItem('foo'); | ||
expect(localStorage.setItem).toBeCalledWith('foo', JSON.stringify(data)); | ||
expect(localStorage.setItem).toHaveBeenCalledTimes(1); | ||
expect(localStorage.removeItem).toHaveBeenCalledWith('foo'); | ||
}); | ||
}); | ||
}); |
@@ -6,7 +6,3 @@ import { EchoLocalStorage } from '../types/storage'; | ||
setItem: <T>(key: string, data: T) => { | ||
if (typeof data === 'string') { | ||
localStorage.setItem(key, data); | ||
} else { | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
} | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
}, | ||
@@ -13,0 +9,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
5459
0
269702