-
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.
test: add test for saving to notebook
Signed-off-by: tygao <[email protected]>
- Loading branch information
Showing
4 changed files
with
177 additions
and
47 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
115 changes: 115 additions & 0 deletions
115
public/components/notebook/__tests__/notebook_name_modal.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,115 @@ | ||
/* | ||
* 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 coreContextExports from '../../../contexts/core_context'; | ||
|
||
import { NotebookNameModal, NotebookNameModalProps } from '../notebook_name_modal'; | ||
import { AssistantServices } from '../../../contexts/core_context'; | ||
import { OpenSearchDashboardsReactContextValue } from '../../../../../../src/plugins/opensearch_dashboards_react/public'; | ||
|
||
const setup = ({ onClose, saveChat }: NotebookNameModalProps) => { | ||
const useCoreMock = { | ||
services: coreMock.createStart(), | ||
}; | ||
jest.spyOn(coreContextExports, 'useCore').mockReturnValue( | ||
// In test env, only mock needed core service and assert. | ||
(useCoreMock as unknown) as OpenSearchDashboardsReactContextValue<AssistantServices> | ||
); | ||
|
||
const renderResult = render( | ||
<I18nProvider> | ||
<NotebookNameModal onClose={onClose} saveChat={saveChat} /> | ||
</I18nProvider> | ||
); | ||
|
||
return { | ||
useCoreMock, | ||
renderResult, | ||
}; | ||
}; | ||
|
||
describe('<NotebookNameModal />', () => { | ||
it('should call onClose after cancel button click', async () => { | ||
const onCloseMock = jest.fn(); | ||
const saveChatMock = jest.fn(); | ||
const { renderResult, useCoreMock } = setup({ | ||
onClose: onCloseMock, | ||
saveChat: saveChatMock, | ||
}); | ||
|
||
expect(onCloseMock).not.toHaveBeenCalled(); | ||
|
||
act(() => { | ||
fireEvent.click(renderResult.getByTestId('confirmNotebookCancelButton')); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(onCloseMock).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should show success toast and call onClose after saving chat succeed', async () => { | ||
const onCloseMock = jest.fn(); | ||
const saveChatMock = jest.fn(); | ||
const { renderResult, useCoreMock } = setup({ | ||
onClose: onCloseMock, | ||
saveChat: saveChatMock, | ||
}); | ||
|
||
act(() => { | ||
fireEvent.change(renderResult.getByLabelText('Notebook name input'), { | ||
target: { | ||
value: 'notebook-name', | ||
}, | ||
}); | ||
}); | ||
|
||
expect(onCloseMock).not.toHaveBeenCalled(); | ||
|
||
act(() => { | ||
fireEvent.click(renderResult.getByTestId('confirmNotebookConfirmButton')); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(useCoreMock.services.notifications.toasts.addSuccess).toHaveBeenCalled(); | ||
expect(onCloseMock).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should show error toasts and call onClose after failed save chat', async () => { | ||
const onCloseMock = jest.fn(); | ||
const saveChatMock = jest.fn(() => { | ||
throw new Error(); | ||
}); | ||
const { renderResult, useCoreMock } = setup({ | ||
onClose: onCloseMock, | ||
saveChat: saveChatMock, | ||
}); | ||
|
||
act(() => { | ||
fireEvent.change(renderResult.getByLabelText('Notebook name input'), { | ||
target: { | ||
value: 'notebook-name', | ||
}, | ||
}); | ||
}); | ||
|
||
expect(onCloseMock).not.toHaveBeenCalled(); | ||
|
||
act(() => { | ||
fireEvent.click(renderResult.getByTestId('confirmNotebookConfirmButton')); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(useCoreMock.services.notifications.toasts.addDanger).toHaveBeenCalled(); | ||
expect(onCloseMock).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