-
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.
add unit tests for ChatPage and ChatInputControls (#45)
removed ChatPageGreetings which is not need anymore --------- Signed-off-by: Yulong Ruan <[email protected]>
- Loading branch information
Showing
6 changed files
with
206 additions
and
78 deletions.
There are no files selected for viewing
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,87 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
|
||
import { coreMock } from '../../../../../src/core/public/mocks'; | ||
import { SessionLoadService } from '../../services/session_load_service'; | ||
import { ChatPage } from './chat_page'; | ||
import * as chatContextExports from '../../contexts/chat_context'; | ||
import * as coreContextExports from '../../contexts/core_context'; | ||
import * as hookExports from '../../hooks/use_chat_state'; | ||
|
||
jest.mock('./controls/chat_input_controls', () => { | ||
return { ChatInputControls: () => <div /> }; | ||
}); | ||
|
||
jest.mock('./chat_page_content', () => { | ||
return { | ||
ChatPageContent: ({ onRefresh }: { onRefresh: () => void }) => ( | ||
<button onClick={onRefresh}>refresh</button> | ||
), | ||
}; | ||
}); | ||
|
||
describe('<ChatPage />', () => { | ||
const dispatchMock = jest.fn(); | ||
const loadMock = jest.fn().mockResolvedValue({ | ||
title: 'session title', | ||
version: 1, | ||
createdTimeMs: new Date().getTime(), | ||
updatedTimeMs: new Date().getTime(), | ||
messages: [], | ||
interactions: [], | ||
}); | ||
const sessionLoadService = new SessionLoadService(coreMock.createStart().http); | ||
|
||
beforeEach(() => { | ||
jest.spyOn(sessionLoadService, 'load').mockImplementation(loadMock); | ||
|
||
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue({ | ||
sessionId: 'mocked_session_id', | ||
chatEnabled: true, | ||
}); | ||
|
||
jest.spyOn(hookExports, 'useChatState').mockReturnValue({ | ||
chatStateDispatch: dispatchMock, | ||
chatState: { messages: [], llmResponding: false }, | ||
}); | ||
|
||
jest.spyOn(coreContextExports, 'useCore').mockReturnValue({ | ||
services: { | ||
sessionLoad: sessionLoadService, | ||
}, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should reload the current conversation when user click refresh', async () => { | ||
render(<ChatPage />); | ||
fireEvent.click(screen.getByText('refresh')); | ||
|
||
expect(loadMock).toHaveBeenCalledWith('mocked_session_id'); | ||
await waitFor(() => { | ||
expect(dispatchMock).toHaveBeenCalledWith({ | ||
type: 'receive', | ||
payload: { messages: [], interactions: [] }, | ||
}); | ||
}); | ||
}); | ||
|
||
it('should NOT call reload if current conversation is not set', async () => { | ||
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue({ | ||
sessionId: undefined, | ||
chatEnabled: true, | ||
}); | ||
render(<ChatPage />); | ||
fireEvent.click(screen.getByText('refresh')); | ||
|
||
expect(loadMock).not.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
This file was deleted.
Oops, something went wrong.
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,117 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen, fireEvent, waitFor, getByRole } from '@testing-library/react'; | ||
|
||
import { ChatInputControls } from './chat_input_controls'; | ||
import * as contextExports from '../../../contexts/chat_context'; | ||
import * as hookExports from '../../../hooks/use_chat_actions'; | ||
|
||
describe('<ChatInputControls />', () => { | ||
const sendMock = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.spyOn(contextExports, 'useChatContext').mockReturnValue({ | ||
appId: 'mocked_app_id', | ||
}); | ||
jest.spyOn(hookExports, 'useChatActions').mockReturnValue({ | ||
send: sendMock, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should display submit button and text box in different state accordingly', () => { | ||
const { rerender } = render(<ChatInputControls loading={true} disabled={true} />); | ||
expect(screen.getByRole('button')).toBeDisabled(); | ||
expect(screen.getByRole('textbox')).toBeDisabled(); | ||
expect(screen.getByRole('button')).toHaveTextContent('Generating...'); | ||
|
||
rerender(<ChatInputControls loading={false} disabled={true} />); | ||
expect(screen.getByRole('button')).toBeDisabled(); | ||
expect(screen.getByRole('textbox')).toBeDisabled(); | ||
expect(screen.getByRole('button')).toHaveTextContent('Go'); | ||
|
||
rerender(<ChatInputControls loading={true} disabled={false} />); | ||
expect(screen.getByRole('button')).toBeEnabled(); | ||
expect(screen.getByRole('textbox')).toBeEnabled(); | ||
expect(screen.getByRole('button')).toHaveTextContent('Generating...'); | ||
|
||
rerender(<ChatInputControls loading={false} disabled={false} />); | ||
expect(screen.getByRole('button')).toBeEnabled(); | ||
expect(screen.getByRole('textbox')).toBeEnabled(); | ||
expect(screen.getByRole('button')).toHaveTextContent('Go'); | ||
}); | ||
|
||
it('should send message when clicking submit button', () => { | ||
render(<ChatInputControls loading={false} disabled={false} />); | ||
fireEvent.change(screen.getByRole('textbox'), { | ||
target: { value: 'what indices are in my cluster?' }, | ||
}); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(sendMock).toHaveBeenCalledWith({ | ||
type: 'input', | ||
content: 'what indices are in my cluster?', | ||
contentType: 'text', | ||
context: { | ||
appId: 'mocked_app_id', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should send message when pressing `Enter`', () => { | ||
render(<ChatInputControls loading={false} disabled={false} />); | ||
fireEvent.change(screen.getByRole('textbox'), { | ||
target: { value: 'what indices are in my cluster?' }, | ||
}); | ||
fireEvent.keyPress(screen.getByRole('textbox'), { | ||
key: 'Enter', | ||
keyCode: 13, | ||
shiftKey: false, | ||
}); | ||
expect(sendMock).toHaveBeenCalledWith({ | ||
type: 'input', | ||
content: 'what indices are in my cluster?', | ||
contentType: 'text', | ||
context: { | ||
appId: 'mocked_app_id', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should NOT send message when pressing `shift+Enter`', () => { | ||
render(<ChatInputControls loading={false} disabled={false} />); | ||
fireEvent.change(screen.getByRole('textbox'), { | ||
target: { value: 'what indices are in my cluster?' }, | ||
}); | ||
fireEvent.keyPress(screen.getByRole('textbox'), { | ||
key: 'Enter', | ||
keyCode: 13, | ||
shiftKey: true, | ||
}); | ||
expect(sendMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should NOT send message if disabled', () => { | ||
render(<ChatInputControls loading={false} disabled={true} />); | ||
fireEvent.change(screen.getByRole('textbox'), { | ||
target: { value: 'what indices are in my cluster?' }, | ||
}); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(sendMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should NOT send message if input is trimmed empty', () => { | ||
render(<ChatInputControls loading={false} disabled={false} />); | ||
fireEvent.change(screen.getByRole('textbox'), { | ||
target: { value: ' ' }, | ||
}); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(sendMock).not.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