Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: new conversation opening using keyboard is not focusing on the last message #15681

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading