| 1 |
import { getUrlParameter } from '@shared/utils/get-url-parameter'; |
| 2 |
|
| 3 |
describe('getUrlParameter', () => { |
| 4 |
beforeEach(() => { |
| 5 |
jest.restoreAllMocks(); |
| 6 |
window.history.pushState({}, '', '/'); |
| 7 |
}); |
| 8 |
|
| 9 |
it('should return null if parameter does not exist', () => { |
| 10 |
window.history.pushState({}, '', '/?param1=value1'); |
| 11 |
expect(getUrlParameter('param2')).toBeNull(); |
| 12 |
}); |
| 13 |
|
| 14 |
it('should return the correct parameter value from URL', () => { |
| 15 |
window.history.pushState({}, '', '/?param1=value1¶m2=value2'); |
| 16 |
expect(getUrlParameter('param1')).toBe('value1'); |
| 17 |
expect(getUrlParameter('param2')).toBe('value2'); |
| 18 |
}); |
| 19 |
|
| 20 |
it('should cleanup the parameter in url after use', () => { |
| 21 |
window.history.pushState({}, '', '/?param1=Hello%20World'); |
| 22 |
const replaceStateSpy = jest.spyOn(window.history, 'replaceState'); |
| 23 |
|
| 24 |
getUrlParameter('param1'); |
| 25 |
|
| 26 |
expect(replaceStateSpy).toHaveBeenCalledWith({}, document.title, '/'); |
| 27 |
}); |
| 28 |
|
| 29 |
it('should NOT cleanup the parameter in url if cleanUrl is false', () => { |
| 30 |
window.history.pushState({}, '', '/?param1=Hello%20World'); |
| 31 |
const replaceStateSpy = jest.spyOn(window.history, 'replaceState'); |
| 32 |
|
| 33 |
getUrlParameter('param1', false); |
| 34 |
|
| 35 |
expect(replaceStateSpy).not.toHaveBeenCalled(); |
| 36 |
}); |
| 37 |
|
| 38 |
it('should NOT affect other parameters in url when cleanup', () => { |
| 39 |
window.history.pushState({}, '', '/?param1=Hello%20World&other=keep-it'); |
| 40 |
const replaceStateSpy = jest.spyOn(window.history, 'replaceState'); |
| 41 |
|
| 42 |
getUrlParameter('param1'); |
| 43 |
|
| 44 |
expect(replaceStateSpy).toHaveBeenCalledWith( |
| 45 |
{}, |
| 46 |
document.title, |
| 47 |
'/?other=keep-it', |
| 48 |
); |
| 49 |
}); |
| 50 |
}); |
| 51 |
|