|
|
@@ -15,12 +15,75 @@
|
|
|
// limitations under the License.
|
|
|
|
|
|
import changeURL from './changeURL';
|
|
|
+import { hueWindow } from 'types/types';
|
|
|
|
|
|
-describe('changeURL.ts', () => {
|
|
|
- it('should change completely the URL', () => {
|
|
|
- changeURL('/banana');
|
|
|
+describe('changeURL', () => {
|
|
|
+ const baseUrl = 'https://www.gethue.com/hue';
|
|
|
+ const mockPushState = jest.fn();
|
|
|
+ const mockReplaceState = jest.fn();
|
|
|
|
|
|
- expect(window.location.pathname).toEqual('/banana');
|
|
|
- changeURL('/jasmine');
|
|
|
+ beforeAll(() => {
|
|
|
+ global.window.history.pushState = mockPushState;
|
|
|
+ global.window.history.replaceState = mockReplaceState;
|
|
|
+ (global.window as hueWindow).HUE_BASE_URL = baseUrl;
|
|
|
+ });
|
|
|
+
|
|
|
+ beforeEach(() => {
|
|
|
+ jest.clearAllMocks();
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should append query params to the URL with pushState', () => {
|
|
|
+ const newURL = '/new/path';
|
|
|
+ const params = { key1: 'value1', key2: 2 };
|
|
|
+
|
|
|
+ changeURL(newURL, params);
|
|
|
+
|
|
|
+ expect(mockReplaceState).not.toHaveBeenCalled();
|
|
|
+ expect(mockPushState).toHaveBeenCalledWith(null, '', `${baseUrl}/new/path?key1=value1&key2=2`);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should append hash to the URL with pushState', () => {
|
|
|
+ const newURL = '/new/path#section';
|
|
|
+ const params = { key1: 'value1' };
|
|
|
+
|
|
|
+ changeURL(newURL, params);
|
|
|
+
|
|
|
+ expect(mockReplaceState).not.toHaveBeenCalled();
|
|
|
+ expect(mockPushState).toHaveBeenCalledWith(null, '', `${baseUrl}/new/path?key1=value1#section`);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle replaceState when isReplace is true', () => {
|
|
|
+ const newURL = '/new/path';
|
|
|
+ const params = { key1: 'value1' };
|
|
|
+
|
|
|
+ changeURL(newURL, params, true);
|
|
|
+
|
|
|
+ expect(mockPushState).not.toHaveBeenCalled();
|
|
|
+ expect(mockReplaceState).toHaveBeenCalledWith(null, '', `${baseUrl}/new/path?key1=value1`);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should not add the base URL if the newURL already starts with the HUE_BASE_URL', () => {
|
|
|
+ const newURL = `${baseUrl}/new/path`;
|
|
|
+
|
|
|
+ changeURL(newURL, { key1: 'value1' });
|
|
|
+
|
|
|
+ expect(mockReplaceState).not.toHaveBeenCalled();
|
|
|
+ expect(mockPushState).toHaveBeenCalledWith(null, '', `${baseUrl}/new/path?key1=value1`);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle the absence of params correctly', () => {
|
|
|
+ const newURL = '/new/path#section';
|
|
|
+
|
|
|
+ changeURL(newURL);
|
|
|
+
|
|
|
+ expect(mockReplaceState).not.toHaveBeenCalled();
|
|
|
+ expect(mockPushState).toHaveBeenCalledWith(null, '', `${baseUrl}/new/path#section`);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should not change the URL if the new URL is the same as the current URL with hash', () => {
|
|
|
+ changeURL(baseUrl, {});
|
|
|
+
|
|
|
+ expect(mockReplaceState).not.toHaveBeenCalled();
|
|
|
+ expect(mockPushState).not.toHaveBeenCalled();
|
|
|
});
|
|
|
});
|