-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2924: Catch SecurityError when accessing localStorage
- Loading branch information
1 parent
ad6c6fe
commit 113fa06
Showing
3 changed files
with
105 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
issue_key: 2924 | ||
show_in_stores: false | ||
platforms: | ||
- web | ||
en: Fix crashes if local storage is not available |
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,75 @@ | ||
import { fireEvent, render } from '@testing-library/react' | ||
import React from 'react' | ||
|
||
import Button from '../../components/base/Button' | ||
import useLocalStorage from '../useLocalStorage' | ||
|
||
describe('useLocalStorage', () => { | ||
const key = 'my_storage_key' | ||
const MockComponent = () => { | ||
const { value, updateLocalStorageItem } = useLocalStorage({ key, initialValue: 0 }) | ||
return ( | ||
<div> | ||
{value} | ||
<Button label='increment' onClick={() => updateLocalStorageItem(value + 1)}> | ||
Increment | ||
</Button> | ||
</div> | ||
) | ||
} | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
localStorage.clear() | ||
}) | ||
|
||
it('should correctly set initial value and update value', () => { | ||
const { getByText } = render(<MockComponent />) | ||
|
||
expect(getByText(0)).toBeTruthy() | ||
expect(localStorage.getItem(key)).toBe('0') | ||
|
||
fireEvent.click(getByText('Increment')) | ||
|
||
expect(getByText(1)).toBeTruthy() | ||
expect(localStorage.getItem(key)).toBe('1') | ||
|
||
fireEvent.click(getByText('Increment')) | ||
fireEvent.click(getByText('Increment')) | ||
fireEvent.click(getByText('Increment')) | ||
|
||
expect(getByText(4)).toBeTruthy() | ||
expect(localStorage.getItem(key)).toBe('4') | ||
}) | ||
|
||
it('should not use initial value if already set', () => { | ||
localStorage.setItem(key, '10') | ||
const { getByText } = render(<MockComponent />) | ||
|
||
expect(getByText(10)).toBeTruthy() | ||
expect(localStorage.getItem(key)).toBe('10') | ||
|
||
fireEvent.click(getByText('Increment')) | ||
|
||
expect(getByText(11)).toBeTruthy() | ||
expect(localStorage.getItem(key)).toBe('11') | ||
}) | ||
|
||
it('should continue to work even if local storage is not usable', () => { | ||
localStorage.getItem = () => { | ||
throw new Error('SecurityError') | ||
} | ||
localStorage.setItem = () => { | ||
throw new Error('SecurityError') | ||
} | ||
const { getByText } = render(<MockComponent />) | ||
|
||
expect(getByText(0)).toBeTruthy() | ||
expect(localStorage.getItem(key)).toBe('0') | ||
|
||
fireEvent.click(getByText('Increment')) | ||
|
||
expect(getByText(1)).toBeTruthy() | ||
expect(localStorage.getItem(key)).toBe('1') | ||
}) | ||
}) |
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