Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/instantsearch.js/src/connectors/chat/connectChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ export default (function connectChat<TWidgetParams extends UnknownWidgetParams>(
if (!_chatInstance.messages || _chatInstance.messages.length === 0) {
return;
}
const status = _chatInstance.status;
if (status === 'submitted' || status === 'streaming') {
_chatInstance.stop();
}
setIsClearing(true);
};

Expand Down
60 changes: 60 additions & 0 deletions tests/common/widgets/chat/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,66 @@ export function createOptionsTests(
);
});

test('stops streaming and clears when clear is clicked during streaming', async () => {
const searchClient = createSearchClient();
const chat = new Chat({});

await setup({
instantSearchOptions: {
indexName: 'indexName',
searchClient,
},
widgetParams: {
javascript: createDefaultWidgetParams(chat),
react: createDefaultWidgetParams(chat),
vue: {},
},
});

await openChat(act);

await act(async () => {
chat._state.messages = [
{
id: '1',
role: 'user' as const,
parts: [{ type: 'text' as const, text: 'hello' }],
},
];
await wait(0);
});

const clearButton = document.querySelector(
'.ais-ChatHeader-clear'
) as HTMLButtonElement;
expect(clearButton).not.toBeDisabled();

// Clear button remains enabled during submitted status
await act(async () => {
chat._state.status = 'submitted';
await wait(0);
});

expect(clearButton).not.toBeDisabled();

// Clear button remains enabled during streaming status
await act(async () => {
chat._state.status = 'streaming';
await wait(0);
});

expect(clearButton).not.toBeDisabled();

// Clicking clear during streaming stops the stream and begins clearing
await act(async () => {
clearButton.click();
await wait(0);
});

expect(chat._state.status).toBe('ready');
expect(clearButton).toBeDisabled();
});

describe('cssClasses', () => {
test('adds custom CSS classes', async () => {
const searchClient = createSearchClient();
Expand Down
Loading