Skip to content

Commit

Permalink
Fix "getItem" is not a function
Browse files Browse the repository at this point in the history
  • Loading branch information
vsaienko committed Sep 3, 2019
1 parent d8f37d4 commit 6227eaf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
40 changes: 38 additions & 2 deletions __tests__/SafeStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ describe('SafeStorage', () => {
expect(safeLocalStorage.isNativeStorageUsed).toBe(true)
expect(safeLocalStorage.isInMemoryStorageUsed).toBe(false)
expect(safeLocalStorage._options).toMatchSnapshot()

safeLocalStorage.setItem('test-1', 423)
safeLocalStorage.setItem('test-2', 481)
expect(safeLocalStorage.getItem('test-1')).toBe('423')
expect(safeLocalStorage.getItem('test-3')).toBe(null)
expect(safeLocalStorage.key(0)).toBe('test-1')

safeLocalStorage.removeItem('test-1')
expect(safeLocalStorage.key(0)).toBe('test-2')
expect(safeLocalStorage.length).toBe(1)

safeLocalStorage.clear()
expect(safeLocalStorage.length).toBe(0)
})

test('Use native sessionStorage', () => {
Expand All @@ -47,6 +60,19 @@ describe('SafeStorage', () => {
expect(SafeStorage.isNativeStorageSupported('sessionStorage')).toBe(true)
expect(safeSessionStorage.isNativeStorageUsed).toBe(true)
expect(safeSessionStorage.isInMemoryStorageUsed).toBe(false)

safeSessionStorage.setItem('test-1', 423)
safeSessionStorage.setItem('test-2', 481)
expect(safeSessionStorage.getItem('test-1')).toBe('423')
expect(safeSessionStorage.getItem('test-3')).toBe(null)
expect(safeSessionStorage.key(0)).toBe('test-1')

safeSessionStorage.removeItem('test-1')
expect(safeSessionStorage.key(0)).toBe('test-2')
expect(safeSessionStorage.length).toBe(1)

safeSessionStorage.clear()
expect(safeSessionStorage.length).toBe(0)
})

test('Pass custom error message', () => {
Expand All @@ -57,15 +83,15 @@ describe('SafeStorage', () => {
})

test('Handle AccessDenied error', () => {
const errorMessage =
const domExceptionMessage =
'Uncaught DOMException: Failed to read the "localStorage" property from "Window": Access is denied for this document.'

delete global.window

global.window = new Proxy(previousWindow, {
get(obj, prop) {
if (['localStorage', 'sessionStorage'].includes(prop)) {
throw new Error(errorMessage)
throw new Error(domExceptionMessage)
}

if (prop in obj) {
Expand All @@ -81,11 +107,21 @@ describe('SafeStorage', () => {
expect(safeLocalStorage.isInMemoryStorageUsed).toBe(false)
expect(safeLocalStorage.isNativeStorageUsed).toBe(false)
expect(SafeStorage.isNativeStorageSupported('localStorage')).toBe(false)
expect(console.warn).toHaveBeenLastCalledWith(
'Looks like you\'ve disabled "localStorage". Enable it to avoid this warning.'
)

const safeSessionStorage = createSafeSessionStorage()

expect(safeSessionStorage.isInMemoryStorageUsed).toBe(true)
expect(safeSessionStorage.isNativeStorageUsed).toBe(false)
expect(SafeStorage.isNativeStorageSupported('sessionStorage')).toBe(false)
expect(console.warn).toHaveBeenLastCalledWith(
'Looks like you\'ve disabled "sessionStorage". Enable it to avoid this warning.'
)

const errorMessage = 'Custom error message'
createSafeLocalStorage({ errorMessage })
expect(console.warn).toHaveBeenLastCalledWith(errorMessage)
})
})
24 changes: 24 additions & 0 deletions src/SafeStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,28 @@ export default class SafeStorage {
get isInMemoryStorageUsed() {
return Boolean(this._isInMemoryStorageUsed)
}

getItem(key) {
return this._storage.getItem(key)
}

setItem(key, value) {
return this._storage.setItem(key, value)
}

removeItem(key) {
return this._storage.removeItem(key)
}

key(index) {
return this._storage.key(index)
}

clear() {
return this._storage.clear()
}

get length() {
return this._storage.length
}
}

0 comments on commit 6227eaf

Please sign in to comment.