|
| 1 | +import { describe, it, expect, afterAll } from 'vitest'; |
| 2 | +import { getRedirectSuffix } from './getRedirectSuffix'; |
| 3 | + |
| 4 | +const originalLocation = globalThis.location; |
| 5 | + |
| 6 | +function mockLocation(search: string, hash: string) { |
| 7 | + Object.defineProperty(globalThis, 'location', { |
| 8 | + value: { ...originalLocation, search, hash }, |
| 9 | + writable: true, |
| 10 | + configurable: true, |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | +// Restore the real object once all tests have finished |
| 15 | +afterAll(() => { |
| 16 | + Object.defineProperty(globalThis, 'location', { |
| 17 | + value: originalLocation, |
| 18 | + writable: true, |
| 19 | + configurable: true, |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +describe('getRedirectSuffix()', () => { |
| 24 | + it('returns "/{search}{hash}" when both parts are present', () => { |
| 25 | + mockLocation('?sap-theme=sap_horizon', '#/mcp/projects'); |
| 26 | + expect(getRedirectSuffix()).toBe('/?sap-theme=sap_horizon#/mcp/projects'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns "/{search}" when only the query string exists', () => { |
| 30 | + mockLocation('?query=foo', ''); |
| 31 | + expect(getRedirectSuffix()).toBe('/?query=foo'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns "{hash}" when only the hash fragment exists', () => { |
| 35 | + mockLocation('', '#/dashboard'); |
| 36 | + expect(getRedirectSuffix()).toBe('#/dashboard'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns an empty string when neither search nor hash exist', () => { |
| 40 | + mockLocation('', ''); |
| 41 | + expect(getRedirectSuffix()).toBe(''); |
| 42 | + }); |
| 43 | +}); |
0 commit comments