2020-01-01 13:55:28 +05:30
|
|
|
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
|
2017-08-17 22:00:37 +05:30
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
|
|
|
|
|
|
|
describe('AccessorUtilities', () => {
|
2020-01-01 13:55:28 +05:30
|
|
|
useLocalStorageSpy();
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
const testError = new Error('test error');
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
describe('canUseLocalStorage', () => {
|
2017-08-17 22:00:37 +05:30
|
|
|
it('should return `true` if access is safe', () => {
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(AccessorUtilities.canUseLocalStorage()).toBe(true);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should return `false` if access to .setItem isnt safe', () => {
|
2020-01-01 13:55:28 +05:30
|
|
|
window.localStorage.setItem.mockImplementation(() => {
|
2018-12-13 13:39:08 +05:30
|
|
|
throw testError;
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(AccessorUtilities.canUseLocalStorage()).toBe(false);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should set a test item if access is safe', () => {
|
2021-11-11 11:23:49 +05:30
|
|
|
AccessorUtilities.canUseLocalStorage();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(window.localStorage.setItem).toHaveBeenCalledWith('canUseLocalStorage', 'true');
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove the test item if access is safe', () => {
|
2021-11-11 11:23:49 +05:30
|
|
|
AccessorUtilities.canUseLocalStorage();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(window.localStorage.removeItem).toHaveBeenCalledWith('canUseLocalStorage');
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|