-
Notifications
You must be signed in to change notification settings - Fork 23
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
add unit tests for HeaderChatButton component #82
Merged
ruanyl
merged 1 commit into
opensearch-project:main
from
ruanyl:unit-test-header-chat-button
Dec 26, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,162 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { act, render, fireEvent, screen } from '@testing-library/react'; | ||
|
||
import { HeaderChatButton } from './chat_header_button'; | ||
import { applicationServiceMock } from '../../../src/core/public/mocks'; | ||
import { AssistantActions } from './types'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
let mockSend: jest.Mock; | ||
let mockLoadChat: jest.Mock; | ||
|
||
jest.mock('./hooks/use_chat_actions', () => { | ||
mockSend = jest.fn(); | ||
mockLoadChat = jest.fn(); | ||
return { | ||
useChatActions: jest.fn().mockReturnValue({ | ||
send: mockSend, | ||
loadChat: mockLoadChat, | ||
openChatUI: jest.fn(), | ||
executeAction: jest.fn(), | ||
abortAction: jest.fn(), | ||
regenerate: jest.fn(), | ||
}), | ||
}; | ||
}); | ||
|
||
jest.mock('./chat_flyout', () => { | ||
return { | ||
ChatFlyout: ({ | ||
toggleFlyoutFullScreen, | ||
flyoutFullScreen, | ||
}: { | ||
toggleFlyoutFullScreen: () => void; | ||
flyoutFullScreen: boolean; | ||
}) => ( | ||
<div aria-label="chat flyout mock"> | ||
<button onClick={toggleFlyoutFullScreen}>toggle chat flyout fullscreen</button> | ||
<p>{flyoutFullScreen ? 'fullscreen mode' : 'dock-right mode'}</p> | ||
</div> | ||
), | ||
}; | ||
}); | ||
|
||
describe('<HeaderChatButton />', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should open chat flyout and send the initial message', () => { | ||
const applicationStart = { | ||
...applicationServiceMock.createStartContract(), | ||
currentAppId$: new BehaviorSubject(''), | ||
}; | ||
render( | ||
<HeaderChatButton | ||
application={applicationStart} | ||
userHasAccess={true} | ||
contentRenderers={{}} | ||
actionExecutors={{}} | ||
assistantActions={{} as AssistantActions} | ||
currentAccount={{ username: 'test_user', tenant: 'test_tenant' }} | ||
/> | ||
); | ||
|
||
act(() => applicationStart.currentAppId$.next('mock_app_id')); | ||
|
||
screen.getByLabelText('chat input').focus(); | ||
fireEvent.change(screen.getByLabelText('chat input'), { | ||
target: { value: 'what indices are in my cluster?' }, | ||
}); | ||
expect(screen.getByLabelText('chat input')).toHaveFocus(); | ||
|
||
fireEvent.keyPress(screen.getByLabelText('chat input'), { | ||
key: 'Enter', | ||
code: 'Enter', | ||
charCode: 13, | ||
}); | ||
|
||
// start a new chat | ||
expect(mockLoadChat).toHaveBeenCalled(); | ||
// send chat message | ||
expect(mockSend).toHaveBeenCalledWith({ | ||
type: 'input', | ||
contentType: 'text', | ||
content: 'what indices are in my cluster?', | ||
context: { appId: 'mock_app_id' }, | ||
}); | ||
// chat flyout displayed | ||
expect(screen.queryByLabelText('chat flyout mock')).toBeInTheDocument(); | ||
// the input value is cleared after pressing enter | ||
expect(screen.getByLabelText('chat input')).toHaveValue(''); | ||
expect(screen.getByLabelText('chat input')).not.toHaveFocus(); | ||
}); | ||
|
||
it('should toggle chat flyout size', () => { | ||
render( | ||
<HeaderChatButton | ||
application={applicationServiceMock.createStartContract()} | ||
userHasAccess={true} | ||
contentRenderers={{}} | ||
actionExecutors={{}} | ||
assistantActions={{} as AssistantActions} | ||
currentAccount={{ username: 'test_user', tenant: 'test_tenant' }} | ||
/> | ||
); | ||
fireEvent.click(screen.getByLabelText('toggle chat flyout icon')); | ||
expect(screen.queryByText('dock-right mode')).toBeInTheDocument(); | ||
|
||
fireEvent.click(screen.getByText('toggle chat flyout fullscreen')); | ||
expect(screen.queryByText('fullscreen mode')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should focus in chat input when click and press Escape should blur', () => { | ||
render( | ||
<HeaderChatButton | ||
application={applicationServiceMock.createStartContract()} | ||
userHasAccess={true} | ||
contentRenderers={{}} | ||
actionExecutors={{}} | ||
assistantActions={{} as AssistantActions} | ||
currentAccount={{ username: 'test_user', tenant: 'test_tenant' }} | ||
/> | ||
); | ||
screen.getByLabelText('chat input').focus(); | ||
expect(screen.getByLabelText('chat input')).toHaveFocus(); | ||
expect(screen.getByTitle('press enter to chat')).toBeInTheDocument(); | ||
|
||
fireEvent.keyUp(screen.getByLabelText('chat input'), { | ||
key: 'Escape', | ||
code: 'Escape', | ||
charCode: 27, | ||
}); | ||
expect(screen.getByLabelText('chat input')).not.toHaveFocus(); | ||
expect(screen.getByTitle('press ⌘ + / to start typing')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should focus on chat input when pressing global shortcut', () => { | ||
render( | ||
<HeaderChatButton | ||
application={applicationServiceMock.createStartContract()} | ||
userHasAccess={true} | ||
contentRenderers={{}} | ||
actionExecutors={{}} | ||
assistantActions={{} as AssistantActions} | ||
currentAccount={{ username: 'test_user', tenant: 'test_tenant' }} | ||
/> | ||
); | ||
expect(screen.getByLabelText('chat input')).not.toHaveFocus(); | ||
fireEvent.keyDown(document.body, { | ||
key: '/', | ||
code: 'NumpadDivide', | ||
charCode: 111, | ||
metaKey: true, | ||
}); | ||
expect(screen.getByLabelText('chat input')).toHaveFocus(); | ||
}); | ||
}); |
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 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 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know if the text will confuse Windows user. As well as that if we need to do any special check logic on Windows browser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we have a task to improve this