From 307f1b0c242e7b86d29d18631800db87dba310d3 Mon Sep 17 00:00:00 2001 From: Shafin Al Karim <129021108+shafin-deriv@users.noreply.github.com> Date: Mon, 6 Nov 2023 14:08:12 +0800 Subject: [PATCH] chore: Test case for session-storage.js (#11201) --- .../utils/__tests__/session-storage.spec.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packages/bot-web-ui/src/utils/__tests__/session-storage.spec.ts diff --git a/packages/bot-web-ui/src/utils/__tests__/session-storage.spec.ts b/packages/bot-web-ui/src/utils/__tests__/session-storage.spec.ts new file mode 100644 index 000000000000..7bae14a490c9 --- /dev/null +++ b/packages/bot-web-ui/src/utils/__tests__/session-storage.spec.ts @@ -0,0 +1,35 @@ +import { getStoredItemsByKey, getStoredItemsByUser, setStoredItemsByKey } from 'Utils/session-storage'; + +describe('Session Storage Util', () => { + const storageKey = 'example_key'; + const defaultValue = 'default_value'; + const storedItems = { example: 'data' }; + + it('should return default value when storage is empty', () => { + const result = getStoredItemsByKey(storageKey, defaultValue); + expect(result).toBe(defaultValue); + }); + + it('should return default value when loginid is falsy', () => { + const loginid = '123'; + const result = getStoredItemsByUser(storageKey, loginid, defaultValue); + expect(result).toBe(defaultValue); + }); + + it('should set stored items', () => { + setStoredItemsByKey(storageKey, storedItems); + const result = getStoredItemsByKey(storageKey, defaultValue); + expect(result).toStrictEqual(storedItems); + }); + + it('should throw error if invalid object is passed to store', () => { + const consoleWarnMock = jest.fn(); + global.console.warn = consoleWarnMock; + const circularObject = { + circularReference: {}, + }; + circularObject.circularReference = circularObject; + setStoredItemsByKey('example_key', circularObject); + expect(consoleWarnMock).toHaveBeenCalled(); + }); +});