|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import { reloadAfterTenantSwitch } from '../account-nav-button'; |
| 17 | + |
| 18 | +describe('Reload window after tenant switch', () => { |
| 19 | + const originalLocation = window.location; |
| 20 | + const mockSetWindowHref = jest.fn(); |
| 21 | + let pathname: string = ''; |
| 22 | + beforeAll(() => { |
| 23 | + pathname = '/app/myapp'; |
| 24 | + Object.defineProperty(window, 'location', { |
| 25 | + value: { |
| 26 | + get pathname() { |
| 27 | + return pathname; |
| 28 | + }, |
| 29 | + get href() { |
| 30 | + return '/app/dashboards?security_tenant=admin_tenant'; |
| 31 | + }, |
| 32 | + set href(value: string) { |
| 33 | + mockSetWindowHref(value); |
| 34 | + }, |
| 35 | + }, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + afterAll(() => { |
| 40 | + window.location = originalLocation; |
| 41 | + }); |
| 42 | + |
| 43 | + it('should remove the tenant query parameter before reloading', () => { |
| 44 | + pathname = '/app/pathname-only'; |
| 45 | + reloadAfterTenantSwitch(); |
| 46 | + expect(mockSetWindowHref).toHaveBeenCalledWith(pathname); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe('Clear lastUrls after tenant switch', () => { |
| 51 | + afterAll(() => { |
| 52 | + jest.clearAllMocks(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should clear out keys with a lastUrl prefix', () => { |
| 56 | + window.sessionStorage.setItem('lastUrl:dashboard', '/dashboard1'); |
| 57 | + window.sessionStorage.setItem('lastUrl:otherApp', '/otherApp'); |
| 58 | + window.sessionStorage.setItem('somethingElse:here', '/random'); |
| 59 | + const mockRemoveItem = jest.spyOn(Object.getPrototypeOf(window.sessionStorage), 'removeItem'); |
| 60 | + reloadAfterTenantSwitch(); |
| 61 | + expect(mockRemoveItem).toHaveBeenCalledWith('lastUrl:dashboard'); |
| 62 | + expect(mockRemoveItem).toHaveBeenCalledWith('lastUrl:otherApp'); |
| 63 | + expect(mockRemoveItem).toHaveBeenCalledTimes(2); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should not clear out keys without a lastUrl prefix', () => { |
| 67 | + window.sessionStorage.setItem('somethingElse:here', '/random'); |
| 68 | + const mockRemoveItem = jest.spyOn(Object.getPrototypeOf(window.sessionStorage), 'removeItem'); |
| 69 | + |
| 70 | + reloadAfterTenantSwitch(); |
| 71 | + expect(mockRemoveItem).toHaveBeenCalledTimes(0); |
| 72 | + }); |
| 73 | +}); |
0 commit comments