-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use chrome.storage.local instead of localStorage
- Loading branch information
1 parent
3575e00
commit 433eb0d
Showing
12 changed files
with
92 additions
and
54 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
File renamed without changes.
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 |
---|---|---|
@@ -1,62 +1,63 @@ | ||
// Mock the chrome storage object and its methods | ||
import { STORAGE_REMEMBER_SESSION } from '../constants'; | ||
import { loadRememberSessionState, saveRememberSessionState } from './login'; | ||
|
||
// Mock the localStorage object and its methods | ||
const localStorageMock = { | ||
getItem: jest.fn(), | ||
setItem: jest.fn(), | ||
clear: jest.fn(), | ||
removeItem: jest.fn(), | ||
length: 0, | ||
key: jest.fn() | ||
const chrome = { | ||
storage: { | ||
local: { | ||
get: jest.fn(), | ||
set: jest.fn() | ||
} | ||
} | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.spyOn(window, 'localStorage', 'get').mockImplementation(() => localStorageMock); | ||
}); | ||
|
||
afterEach(() => { | ||
// Reset the mock after each test | ||
localStorageMock.getItem.mockReset(); | ||
localStorageMock.setItem.mockReset(); | ||
}); | ||
// Make sure the chrome object is available globally | ||
global.chrome = chrome as any; | ||
|
||
describe('saveRememberSessionState', () => { | ||
test('should save the remember session state to local storage', () => { | ||
const state = true; | ||
saveRememberSessionState(state); | ||
expect(localStorage.setItem).toHaveBeenCalledWith( | ||
STORAGE_REMEMBER_SESSION, | ||
JSON.stringify(state) | ||
); | ||
beforeEach(() => { | ||
// Clear all instances and calls to constructor and all methods: | ||
chrome.storage.local.set.mockClear(); | ||
}); | ||
|
||
test('should throw an error if saving to local storage fails', () => { | ||
const error = new Error('Error saving to local storage'); | ||
localStorage.setItem = jest.fn(() => { | ||
throw error; | ||
test('should save the remember session state to chrome storage', async () => { | ||
const state = true; | ||
saveRememberSessionState(state); | ||
expect(chrome.storage.local.set).toHaveBeenCalledWith({ | ||
[STORAGE_REMEMBER_SESSION]: JSON.stringify(state) | ||
}); | ||
expect(() => saveRememberSessionState(true)).toThrowError(error); | ||
}); | ||
}); | ||
|
||
describe('loadRememberSessionState', () => { | ||
test('should load the remember session state from local storage', () => { | ||
beforeEach(() => { | ||
// Clear all instances and calls to constructor and all methods: | ||
chrome.storage.local.get.mockClear(); | ||
}); | ||
|
||
test('should load the remember session state from chrome storage', async () => { | ||
const state = true; | ||
localStorage.getItem = jest.fn(() => JSON.stringify(state)); | ||
expect(loadRememberSessionState()).toEqual(state); | ||
chrome.storage.local.get.mockResolvedValue({ | ||
[STORAGE_REMEMBER_SESSION]: JSON.stringify(state) | ||
}); | ||
|
||
const loadedState = await loadRememberSessionState(); | ||
expect(loadedState).toEqual(state); | ||
}); | ||
|
||
test('should return false if no state is found in local storage', () => { | ||
localStorage.getItem = jest.fn(() => null); | ||
expect(loadRememberSessionState()).toEqual(false); | ||
test('should return false if no state is found in chrome storage', async () => { | ||
chrome.storage.local.get.mockResolvedValue({}); | ||
|
||
const loadedState = await loadRememberSessionState(); | ||
expect(loadedState).toEqual(false); | ||
}); | ||
|
||
test('should return false if an error occurs while loading the state', () => { | ||
const error = new Error('Error loading state from local storage'); | ||
localStorageMock.getItem = jest.fn(() => { | ||
throw error; | ||
}); | ||
expect(loadRememberSessionState()).toEqual(false); | ||
test('should return false if an error occurs while loading the state', async () => { | ||
chrome.storage.local.get.mockRejectedValue( | ||
new Error('Error loading state from chrome storage') | ||
); | ||
|
||
const loadedState = await loadRememberSessionState(); | ||
expect(loadedState).toEqual(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
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,23 @@ | ||
/* | ||
* Manages the storage in Chrome storage | ||
*/ | ||
|
||
export const saveInChromeStorage = (key: string, value: string) => { | ||
try { | ||
chrome.storage.local.set({ [key]: value }); | ||
} catch (e) { | ||
throw e; | ||
} | ||
}; | ||
|
||
export const loadFromChromeStorage = (key: string) => { | ||
try { | ||
return chrome.storage.local.get(key); | ||
} catch (e) { | ||
throw e; | ||
} | ||
}; | ||
|
||
export const generateKey = () => { | ||
return (Date.now() + Math.random()).toString(); | ||
}; |
4 changes: 4 additions & 0 deletions
4
packages/extension/src/utils/storage.ts → packages/extension/src/utils/storageLocal.ts
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
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