Skip to content

Commit

Permalink
fix: abort not happend in unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <wonglam@amazon.com>
  • Loading branch information
wanglam committed Dec 8, 2023
1 parent 61f8cc9 commit c7ca9b2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
14 changes: 10 additions & 4 deletions public/components/__tests__/edit_conversation_name_modal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
EditConversationNameModal,
EditConversationNameModalProps,
} from '../edit_conversation_name_modal';
import { HttpHandler } from '../../../../../src/core/public';

const setup = ({ onClose, defaultTitle, sessionId }: EditConversationNameModalProps) => {
const useCoreMock = {
Expand Down Expand Up @@ -142,15 +143,20 @@ describe('<EditConversationNameModal />', () => {

it('should call onClose with cancelled after patch session aborted', async () => {
const onCloseMock = jest.fn();
const pendingPromise = new Promise((resolve) => {
setTimeout(resolve, 99999);
});
const { renderResult, useCoreMock } = setup({
sessionId: '1',
defaultTitle: 'foo',
onClose: onCloseMock,
});
useCoreMock.services.http.put.mockImplementation(() => pendingPromise);
useCoreMock.services.http.put.mockImplementation(((_path, options) => {
return new Promise((_resolve, reject) => {
if (options?.signal) {
options.signal.onabort = () => {
reject(new Error('Aborted'));
};
}
});
}) as HttpHandler);

act(() => {
fireEvent.change(renderResult.getByLabelText('Conversation name input'), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
DeleteConversationConfirmModal,
DeleteConversationConfirmModalProps,
} from '../delete_conversation_confirm_modal';
import { HttpHandler } from '../../../../../../src/core/public';

const setup = ({ onClose, sessionId }: DeleteConversationConfirmModalProps) => {
const useCoreMock = {
Expand Down Expand Up @@ -52,7 +53,7 @@ describe('<DeleteConversationConfirmModal />', () => {

it('should call onClose with "canceled" after cancel button click', async () => {
const onCloseMock = jest.fn();
const { renderResult, useCoreMock } = setup({
const { renderResult } = setup({
sessionId: '1',
onClose: onCloseMock,
});
Expand Down Expand Up @@ -111,14 +112,19 @@ describe('<DeleteConversationConfirmModal />', () => {

it('should call onClose with cancelled after delete session aborted', async () => {
const onCloseMock = jest.fn();
const pendingPromise = new Promise((resolve) => {
setTimeout(resolve, 99999);
});
const { renderResult, useCoreMock } = setup({
sessionId: '1',
onClose: onCloseMock,
});
useCoreMock.services.http.delete.mockImplementation(() => pendingPromise);
useCoreMock.services.http.delete.mockImplementation(((_path, options) => {
return new Promise((_resolve, reject) => {
if (options?.signal) {
options.signal.onabort = () => {
reject(new Error('Aborted'));
};
}
});
}) as HttpHandler);

expect(onCloseMock).not.toHaveBeenCalled();
expect(useCoreMock.services.http.delete).not.toHaveBeenCalled();
Expand Down

0 comments on commit c7ca9b2

Please sign in to comment.