From 8a6a86d6a4c4b1f09828699db325cc137718eda2 Mon Sep 17 00:00:00 2001 From: thibautbremand Date: Sat, 5 Aug 2023 15:45:27 +0200 Subject: [PATCH] Update login.test.ts So it does not override global chrome object --- packages/extension/src/utils/login.test.ts | 36 +++++++++------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/packages/extension/src/utils/login.test.ts b/packages/extension/src/utils/login.test.ts index 7ccf08e60..205312e0c 100644 --- a/packages/extension/src/utils/login.test.ts +++ b/packages/extension/src/utils/login.test.ts @@ -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) }); @@ -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') );