Skip to content

Commit

Permalink
fix: new conversation opening using keyboard is not focusing on the l…
Browse files Browse the repository at this point in the history
…ast message (#15681)

* fix: pass current history state while calling history param when showing a conversation

* test: add tests
  • Loading branch information
arjita-mitra committed Aug 29, 2023
1 parent 93f4194 commit a5bf68e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/script/router/Router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import {waitFor} from '@testing-library/react';

import {configureRoutes, navigate} from './Router';
import {configureRoutes, navigate, setHistoryParam} from './Router';

describe('Router', () => {
afterEach(() => {
Expand Down Expand Up @@ -77,4 +77,34 @@ describe('Router', () => {
});
});
});

describe('setHistoryParam', () => {
// Mock window.history.replaceState before each test
beforeEach(() => {
global.history.replaceState = jest.fn();
});

// Restore the original method after each test
afterEach(() => {
(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});

expect(() => setHistoryParam('/path')).toThrow('stateObj must be provided when history.state is not empty.');
});

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});

expect(() => setHistoryParam('/path')).not.toThrow();
});

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

expect(() => setHistoryParam('/path', {})).not.toThrow();
});
});
});
3 changes: 3 additions & 0 deletions src/script/router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@ export const navigate = (path: string, stateObj?: {}) => {
};

export const setHistoryParam = (path: string, stateObj?: {}) => {
if (window.history.state && !stateObj) {
throw new Error('stateObj must be provided when history.state is not empty.');
}
window.history.replaceState(stateObj, '', `#${path}`);
};
1 change: 1 addition & 0 deletions src/script/view_model/ContentViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export class ContentViewModel {
this.previousConversation = this.conversationState.activeConversation();
setHistoryParam(
generateConversationUrl({id: conversationEntity?.id ?? '', domain: conversationEntity?.domain ?? ''}),
history.state,
);

if (openNotificationSettings) {
Expand Down

0 comments on commit a5bf68e

Please sign in to comment.