-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update conversation data after edit (#48)
* fix: update conversation data after edit Signed-off-by: Lin Wang <wonglam@amazon.com> * test: add unit tests for update conversation name Signed-off-by: Lin Wang <wonglam@amazon.com> --------- Signed-off-by: Lin Wang <wonglam@amazon.com>
- Loading branch information
Showing
5 changed files
with
191 additions
and
4 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
public/components/__tests__/chat_window_header_title.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { act, fireEvent, render, waitFor } from '@testing-library/react'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { I18nProvider } from '@osd/i18n/react'; | ||
|
||
import { coreMock } from '../../../../../src/core/public/mocks'; | ||
import * as useChatStateExports from '../../hooks/use_chat_state'; | ||
import * as useChatActionsExports from '../../hooks/use_chat_actions'; | ||
import * as useSaveChatExports from '../../hooks/use_save_chat'; | ||
import * as chatContextExports from '../../contexts/chat_context'; | ||
import * as coreContextExports from '../../contexts/core_context'; | ||
|
||
import { ChatWindowHeaderTitle } from '../chat_window_header_title'; | ||
|
||
const setup = () => { | ||
const useCoreMock = { | ||
services: { | ||
...coreMock.createStart(), | ||
sessions: { | ||
sessions$: new BehaviorSubject({ | ||
objects: [ | ||
{ | ||
id: '1', | ||
title: 'foo', | ||
}, | ||
], | ||
total: 1, | ||
}), | ||
reload: jest.fn(), | ||
}, | ||
}, | ||
}; | ||
useCoreMock.services.http.put.mockImplementation(() => Promise.resolve()); | ||
|
||
const useChatStateMock = { | ||
chatState: { messages: [] }, | ||
}; | ||
const useChatContextMock = { | ||
sessionId: '1', | ||
title: 'foo', | ||
setSessionId: jest.fn(), | ||
setTitle: jest.fn(), | ||
}; | ||
const useChatActionsMock = { | ||
loadChat: jest.fn(), | ||
}; | ||
const useSaveChatMock = { | ||
saveChat: jest.fn(), | ||
}; | ||
jest.spyOn(coreContextExports, 'useCore').mockReturnValue(useCoreMock); | ||
jest.spyOn(useChatStateExports, 'useChatState').mockReturnValue(useChatStateMock); | ||
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue(useChatContextMock); | ||
jest.spyOn(useChatActionsExports, 'useChatActions').mockReturnValue(useChatActionsMock); | ||
jest.spyOn(useSaveChatExports, 'useSaveChat').mockReturnValue(useSaveChatMock); | ||
|
||
const renderResult = render( | ||
<I18nProvider> | ||
<ChatWindowHeaderTitle /> | ||
</I18nProvider> | ||
); | ||
|
||
return { | ||
useCoreMock, | ||
useChatStateMock, | ||
useChatContextMock, | ||
renderResult, | ||
}; | ||
}; | ||
|
||
describe('<ChatWindowHeaderTitle />', () => { | ||
it('should reload history list after edit conversation name', async () => { | ||
const { renderResult, useCoreMock } = setup(); | ||
|
||
act(() => { | ||
fireEvent.click(renderResult.getByText('foo')); | ||
}); | ||
|
||
act(() => { | ||
fireEvent.click(renderResult.getByText('Rename conversation')); | ||
}); | ||
|
||
act(() => { | ||
fireEvent.change(renderResult.getByLabelText('Conversation name input'), { | ||
target: { value: 'bar' }, | ||
}); | ||
}); | ||
|
||
expect(useCoreMock.services.sessions.reload).not.toHaveBeenCalled(); | ||
|
||
act(() => { | ||
fireEvent.click(renderResult.getByTestId('confirmModalConfirmButton')); | ||
}); | ||
|
||
waitFor(() => { | ||
expect(useCoreMock.services.sessions.reload).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
public/tabs/history/__tests__/chat_history_search_list.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { act, fireEvent, render, waitFor } from '@testing-library/react'; | ||
import { I18nProvider } from '@osd/i18n/react'; | ||
|
||
import { coreMock } from '../../../../../../src/core/public/mocks'; | ||
import * as chatContextExports from '../../../contexts/chat_context'; | ||
import * as coreContextExports from '../../../contexts/core_context'; | ||
|
||
import { ChatHistorySearchList } from '../chat_history_search_list'; | ||
|
||
const setup = () => { | ||
const useChatContextMock = { | ||
sessionId: '1', | ||
setTitle: jest.fn(), | ||
}; | ||
const useCoreMock = { | ||
services: coreMock.createStart(), | ||
}; | ||
useCoreMock.services.http.put.mockImplementation(() => Promise.resolve()); | ||
jest.spyOn(coreContextExports, 'useCore').mockReturnValue(useCoreMock); | ||
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue(useChatContextMock); | ||
|
||
const renderResult = render( | ||
<I18nProvider> | ||
<ChatHistorySearchList | ||
loading={false} | ||
histories={[{ id: '1', title: 'foo', updatedTimeMs: 0 }]} | ||
onSearchChange={jest.fn()} | ||
onLoadChat={jest.fn()} | ||
onRefresh={jest.fn()} | ||
onHistoryDeleted={jest.fn()} | ||
/> | ||
</I18nProvider> | ||
); | ||
|
||
return { | ||
useChatContextMock, | ||
renderResult, | ||
}; | ||
}; | ||
|
||
describe('<ChatHistorySearchList />', () => { | ||
it('should set new window title after edit conversation name', async () => { | ||
const { renderResult, useChatContextMock } = setup(); | ||
|
||
act(() => { | ||
fireEvent.click(renderResult.getByLabelText('Edit conversation name')); | ||
}); | ||
|
||
act(() => { | ||
fireEvent.change(renderResult.getByLabelText('Conversation name input'), { | ||
target: { value: 'bar' }, | ||
}); | ||
}); | ||
|
||
expect(useChatContextMock.setTitle).not.toHaveBeenCalled(); | ||
|
||
act(() => { | ||
fireEvent.click(renderResult.getByTestId('confirmModalConfirmButton')); | ||
}); | ||
|
||
waitFor(() => { | ||
expect(useChatContextMock.setTitle).toHaveBeenLastCalledWith('bar'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters