Skip to content

Commit

Permalink
Update login.test.ts
Browse files Browse the repository at this point in the history
So it does not override global chrome object
  • Loading branch information
ThibautBremand committed Aug 5, 2023
1 parent b7d2e68 commit 8a6a86d
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions packages/extension/src/utils/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
import { STORAGE_REMEMBER_SESSION } from '../constants';
import { loadRememberSessionState, saveRememberSessionState } from './login';
import { saveInChromeStorage, loadFromChromeStorage } from './storageChrome';

// Mock the chrome storage object and its methods
const chrome = {
storage: {
local: {
get: jest.fn(),
set: jest.fn()
}
}
};

// Make sure the chrome object is available globally
global.chrome = chrome as any;
jest.mock('./storageChrome', () => ({
saveInChromeStorage: jest.fn(),
loadFromChromeStorage: jest.fn()
}));

describe('saveRememberSessionState', () => {
beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
chrome.storage.local.set.mockClear();
saveInChromeStorage.mockClear();
});

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(saveInChromeStorage).toHaveBeenCalledWith(
STORAGE_REMEMBER_SESSION,
JSON.stringify(state)
);
});
});

describe('loadRememberSessionState', () => {
beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
chrome.storage.local.get.mockClear();
loadFromChromeStorage.mockClear();
});

test('should load the remember session state from chrome storage', async () => {
const state = true;
chrome.storage.local.get.mockResolvedValue({
(loadFromChromeStorage as jest.Mock).mockResolvedValue({
[STORAGE_REMEMBER_SESSION]: JSON.stringify(state)
});

Expand All @@ -46,14 +38,14 @@ describe('loadRememberSessionState', () => {
});

test('should return false if no state is found in chrome storage', async () => {
chrome.storage.local.get.mockResolvedValue({});
(loadFromChromeStorage as jest.Mock).mockResolvedValue({});

const loadedState = await loadRememberSessionState();
expect(loadedState).toEqual(false);
});

test('should return false if an error occurs while loading the state', async () => {
chrome.storage.local.get.mockRejectedValue(
(loadFromChromeStorage as jest.Mock).mockRejectedValue(
new Error('Error loading state from chrome storage')
);

Expand Down

0 comments on commit 8a6a86d

Please sign in to comment.