Skip to content

Commit

Permalink
fix: uses history.state if it is not empty and stateObj is not provid…
Browse files Browse the repository at this point in the history
…ed (#15684)
  • Loading branch information
arjita-mitra committed Aug 29, 2023
1 parent 5a17fad commit c125b6d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
26 changes: 17 additions & 9 deletions src/script/router/Router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,30 @@ describe('Router', () => {
(global.history.replaceState as jest.Mock).mockRestore();
});

it('throws an error if history.state is not empty and stateObj is not provided', () => {
Object.defineProperty(window.history, 'state', {value: {}, writable: true});
it('uses history.state if it is not empty and stateObj is not provided', () => {
const mockState = {eventKey: 'Enter'};
Object.defineProperty(window.history, 'state', {value: mockState, writable: true});

expect(() => setHistoryParam('/path')).toThrow('stateObj must be provided when history.state is not empty.');
setHistoryParam('/path');
expect(global.history.replaceState).toHaveBeenCalledWith(mockState, '', '#/path');
});

it('does not throw an error if history.state is empty and stateObj is not provided', () => {
Object.defineProperty(window.history, 'state', {value: null, writable: true});
it('uses stateObj even if history.state is not empty', () => {
const mockState = {eventKey: 'Tab'};
Object.defineProperty(window.history, 'state', {value: mockState, writable: true});

expect(() => setHistoryParam('/path')).not.toThrow();
const newStateObj = {newState: 'state'};
setHistoryParam('/path', newStateObj);
expect(global.history.replaceState).toHaveBeenCalledWith(newStateObj, '', '#/path');
});

it('does not throw an error if history.state is not empty and stateObj is provided', () => {
Object.defineProperty(window.history, 'state', {value: {}, writable: true}); // Temporarily override history.state for this test
it('explicitely resetting the state is allowed', () => {
const mockState = {eventKey: 'Tab'};
Object.defineProperty(window.history, 'state', {value: mockState, writable: true});

expect(() => setHistoryParam('/path', {})).not.toThrow();
const newStateObj = {};
setHistoryParam('/path', newStateObj);
expect(global.history.replaceState).toHaveBeenCalledWith(newStateObj, '', '#/path');
});
});
});
5 changes: 1 addition & 4 deletions src/script/router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ export const navigate = (path: string, stateObj?: {}) => {
parseRoute();
};

export const setHistoryParam = (path: string, stateObj?: {}) => {
if (window.history.state && !stateObj) {
throw new Error('stateObj must be provided when history.state is not empty.');
}
export const setHistoryParam = (path: string, stateObj: {} = window.history.state) => {
window.history.replaceState(stateObj, '', `#${path}`);
};

0 comments on commit c125b6d

Please sign in to comment.