-
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 chat window header (#63)
* test: add unit tests for chat window header Signed-off-by: Lin Wang <wonglam@amazon.com> * test: add ut for chat window header title Signed-off-by: Lin Wang <wonglam@amazon.com> * test: add unit tests for chat experimental badge Signed-off-by: Lin Wang <wonglam@amazon.com> * Address PR comments Signed-off-by: Lin Wang <wonglam@amazon.com> --------- Signed-off-by: Lin Wang <wonglam@amazon.com>
- Loading branch information
Showing
5 changed files
with
282 additions
and
5 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
public/components/__tests__/chat_experimental_badge.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,53 @@ | ||
/* | ||
* 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 { ChatExperimentalBadge } from '../chat_experimental_badge'; | ||
|
||
describe('<ChatWindowHeaderTitle />', () => { | ||
it('should show experimental dropdown after icon clicked', () => { | ||
const { getByRole, getByText, queryByText } = render( | ||
<I18nProvider> | ||
<ChatExperimentalBadge /> | ||
</I18nProvider> | ||
); | ||
|
||
expect(queryByText('Experimental')).not.toBeInTheDocument(); | ||
fireEvent.click(getByRole('button')); | ||
expect(getByText('Experimental')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should hide experimental dropdown after click other places', async () => { | ||
const { getByRole, getByText, queryByText } = render( | ||
<I18nProvider> | ||
<ChatExperimentalBadge /> | ||
</I18nProvider> | ||
); | ||
|
||
act(() => { | ||
fireEvent.click(getByRole('button')); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(getByText('Experimental')).toBeInTheDocument(); | ||
|
||
// Ensure focus trap enabled, then we can click outside. | ||
expect( | ||
getByText('Experimental').closest('div[data-focus-lock-disabled="false"]') | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
act(() => { | ||
fireEvent.mouseDown(document.body); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(queryByText('Experimental')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen, fireEvent, act } from '@testing-library/react'; | ||
|
||
import { ChatWindowHeader, ChatWindowHeaderProps } from '../chat_window_header'; | ||
import * as chatContextExports from '../../contexts/chat_context'; | ||
import { TabId } from '../../types'; | ||
|
||
jest.mock('../../components/chat_window_header_title', () => { | ||
return { ChatWindowHeaderTitle: () => <div>OpenSearch Assistant</div> }; | ||
}); | ||
|
||
const setup = ({ | ||
selectedTabId, | ||
...props | ||
}: Partial<ChatWindowHeaderProps> & { selectedTabId?: TabId } = {}) => { | ||
const useChatContextMock = { | ||
sessionId: '1', | ||
title: 'foo', | ||
selectedTabId: selectedTabId || 'chat', | ||
setSessionId: jest.fn(), | ||
setTitle: jest.fn(), | ||
setFlyoutVisible: jest.fn(), | ||
setSelectedTabId: jest.fn(), | ||
setFlyoutComponent: jest.fn(), | ||
}; | ||
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue(useChatContextMock); | ||
const renderResult = render( | ||
<ChatWindowHeader flyoutFullScreen={false} toggleFlyoutFullScreen={jest.fn()} {...props} /> | ||
); | ||
|
||
return { | ||
renderResult, | ||
useChatContextMock, | ||
}; | ||
}; | ||
|
||
describe('<ChatWindowHeader />', () => { | ||
it('should render title, history, fullscreen and close button', () => { | ||
const { renderResult } = setup(); | ||
|
||
expect(renderResult.getByText('OpenSearch Assistant')).toBeInTheDocument(); | ||
expect(renderResult.getByLabelText('history')).toBeInTheDocument(); | ||
expect(renderResult.getByLabelText('fullScreen')).toBeInTheDocument(); | ||
expect(renderResult.getByLabelText('close')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call setFlyoutVisible with false after close button clicked', () => { | ||
const { renderResult, useChatContextMock } = setup(); | ||
|
||
expect(useChatContextMock.setFlyoutVisible).not.toHaveBeenCalled(); | ||
act(() => { | ||
fireEvent.click(renderResult.getByLabelText('close')); | ||
}); | ||
expect(useChatContextMock.setFlyoutVisible).toHaveBeenLastCalledWith(false); | ||
}); | ||
|
||
it('should call setFlyoutComponent with undefined after history button click', () => { | ||
const { renderResult, useChatContextMock } = setup(); | ||
|
||
expect(useChatContextMock.setFlyoutComponent).not.toHaveBeenCalled(); | ||
act(() => { | ||
fireEvent.click(renderResult.getByLabelText('history')); | ||
}); | ||
expect(useChatContextMock.setFlyoutComponent).toHaveBeenLastCalledWith(undefined); | ||
}); | ||
|
||
it('should call setSelectedTabId with "chat" when selectedTabId is "history"', () => { | ||
const { renderResult, useChatContextMock } = setup({ | ||
selectedTabId: 'history', | ||
}); | ||
|
||
expect(useChatContextMock.setSelectedTabId).not.toHaveBeenCalled(); | ||
act(() => { | ||
fireEvent.click(renderResult.getByLabelText('history')); | ||
}); | ||
expect(useChatContextMock.setSelectedTabId).toHaveBeenLastCalledWith('chat'); | ||
}); | ||
|
||
it('should call setSelectedTabId with "history" when selectedTabId is "chat"', () => { | ||
const { renderResult, useChatContextMock } = setup({ | ||
selectedTabId: 'chat', | ||
}); | ||
|
||
expect(useChatContextMock.setSelectedTabId).not.toHaveBeenCalled(); | ||
act(() => { | ||
fireEvent.click(renderResult.getByLabelText('history')); | ||
}); | ||
expect(useChatContextMock.setSelectedTabId).toHaveBeenLastCalledWith('history'); | ||
}); | ||
|
||
it('should call toggleFullScreen after fullScreen clicked', () => { | ||
const toggleFlyoutFullScreenMock = jest.fn(); | ||
const { renderResult } = setup({ | ||
flyoutFullScreen: true, | ||
toggleFlyoutFullScreen: toggleFlyoutFullScreenMock, | ||
}); | ||
|
||
expect(toggleFlyoutFullScreenMock).not.toHaveBeenCalled(); | ||
act(() => { | ||
fireEvent.click(renderResult.getByLabelText('fullScreen')); | ||
}); | ||
expect(toggleFlyoutFullScreenMock).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