-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(elements): add check if localStorage is available (#1073)
- Loading branch information
1 parent
4b2358d
commit 9e57c0a
Showing
4 changed files
with
76 additions
and
2 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
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,13 @@ | ||
/** | ||
* Test if localStorage exists and is enabled | ||
*/ | ||
export function isLocalStorageAvailable() { | ||
const test = 'test'; | ||
try { | ||
localStorage.setItem(test, test); | ||
localStorage.removeItem(test); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { isLocalStorageAvailable } from '../../../src/lib/browser'; | ||
import sinon from 'sinon'; | ||
import { expect } from 'chai'; | ||
|
||
describe(isLocalStorageAvailable.name, () => { | ||
let setItemStub: sinon.SinonStub<Parameters<Storage['setItem']>, ReturnType<Storage['setItem']>>; | ||
let removeItemStub: sinon.SinonStub<Parameters<Storage['removeItem']>, ReturnType<Storage['removeItem']>>; | ||
|
||
beforeEach(() => { | ||
setItemStub = sinon.stub(localStorage, 'setItem'); | ||
removeItemStub = sinon.stub(localStorage, 'removeItem'); | ||
}); | ||
|
||
it(`should be false if setItem throws`, () => { | ||
setItemStub.throws(new Error('Quota exceeded')); | ||
|
||
expect(isLocalStorageAvailable()).to.be.false; | ||
}); | ||
|
||
it(`should be false if removeItem throws`, () => { | ||
removeItemStub.throws(new Error('Quota exceeded')); | ||
|
||
expect(isLocalStorageAvailable()).to.be.false; | ||
}); | ||
|
||
it(`should be false if localStorage is undefined`, () => { | ||
sinon.replaceGetter(window, 'localStorage', () => (undefined as unknown) as Storage); | ||
|
||
expect(isLocalStorageAvailable()).to.be.false; | ||
}); | ||
|
||
it('should be true if localStorage works', () => { | ||
expect(isLocalStorageAvailable()).to.be.true; | ||
}); | ||
}); |