|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { SECURITY_DASHBOARDS_LOGOUT_URL } from '../../../common/constants/data_sources'; |
| 7 | +import { |
| 8 | + ASYNC_QUERY_ACCELERATIONS_CACHE, |
| 9 | + ASYNC_QUERY_DATASOURCE_CACHE, |
| 10 | +} from '../../../common/constants/shared'; |
| 11 | +import { catalogRequestIntercept } from './cache_intercept'; |
| 12 | + |
| 13 | +interface LooseObject { |
| 14 | + [key: string]: any; |
| 15 | +} |
| 16 | + |
| 17 | +// Mock sessionStorage |
| 18 | +const sessionStorageMock = (() => { |
| 19 | + let store = {} as LooseObject; |
| 20 | + return { |
| 21 | + getItem(key: string) { |
| 22 | + return store[key] || null; |
| 23 | + }, |
| 24 | + setItem(key: string, value: string) { |
| 25 | + store[key] = value.toString(); |
| 26 | + }, |
| 27 | + removeItem(key: string) { |
| 28 | + delete store[key]; |
| 29 | + }, |
| 30 | + clear() { |
| 31 | + store = {}; |
| 32 | + }, |
| 33 | + }; |
| 34 | +})(); |
| 35 | + |
| 36 | +Object.defineProperty(window, 'sessionStorage', { value: sessionStorageMock }); |
| 37 | + |
| 38 | +describe('Intercept logout handler', () => { |
| 39 | + beforeEach(() => { |
| 40 | + jest.spyOn(window.sessionStorage, 'removeItem'); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + jest.restoreAllMocks(); |
| 45 | + }); |
| 46 | + |
| 47 | + const logoutPath = { |
| 48 | + path: SECURITY_DASHBOARDS_LOGOUT_URL, |
| 49 | + }; |
| 50 | + |
| 51 | + it('Intercept logout handler should clear the cache session', () => { |
| 52 | + const logoutInterceptFn = catalogRequestIntercept(); |
| 53 | + logoutInterceptFn(logoutPath, null); |
| 54 | + expect(sessionStorage.removeItem).toBeCalledWith(ASYNC_QUERY_DATASOURCE_CACHE); |
| 55 | + expect(sessionStorage.removeItem).toBeCalledWith(ASYNC_QUERY_ACCELERATIONS_CACHE); |
| 56 | + }); |
| 57 | +}); |
0 commit comments