|
4 | 4 | screen, |
5 | 5 | userEvent, |
6 | 6 | waitFor, |
| 7 | + waitForElementToBeRemoved, |
| 8 | + within, |
7 | 9 | } from '@mongodb-js/testing-library-compass'; |
8 | 10 | import { |
9 | 11 | AssistantProvider, |
@@ -40,6 +42,19 @@ const TestComponent: React.FunctionComponent<{ |
40 | 42 | }; |
41 | 43 |
|
42 | 44 | describe('AssistantProvider', function () { |
| 45 | + const mockMessages: AssistantMessage[] = [ |
| 46 | + { |
| 47 | + id: '1', |
| 48 | + role: 'user', |
| 49 | + parts: [{ type: 'text', text: 'Test message' }], |
| 50 | + }, |
| 51 | + { |
| 52 | + id: '2', |
| 53 | + role: 'assistant', |
| 54 | + parts: [{ type: 'text', text: 'Test assistant message' }], |
| 55 | + }, |
| 56 | + ]; |
| 57 | + |
43 | 58 | it('always renders children', function () { |
44 | 59 | render(<TestComponent chat={createMockChat({ messages: [] })} />, { |
45 | 60 | preferences: { enableAIAssistant: true }, |
@@ -92,18 +107,6 @@ describe('AssistantProvider', function () { |
92 | 107 | } |
93 | 108 |
|
94 | 109 | it('displays messages in the chat feed', async function () { |
95 | | - const mockMessages: AssistantMessage[] = [ |
96 | | - { |
97 | | - id: '1', |
98 | | - role: 'user', |
99 | | - parts: [{ type: 'text', text: 'Test message' }], |
100 | | - }, |
101 | | - { |
102 | | - id: '2', |
103 | | - role: 'assistant', |
104 | | - parts: [{ type: 'text', text: 'Test assistant message' }], |
105 | | - }, |
106 | | - ]; |
107 | 110 | const mockChat = createMockChat({ messages: mockMessages }); |
108 | 111 |
|
109 | 112 | await renderOpenAssistantDrawer(mockChat); |
@@ -186,6 +189,66 @@ describe('AssistantProvider', function () { |
186 | 189 | expect(screen.getByText('Hello assistant!')).to.exist; |
187 | 190 | }); |
188 | 191 | }); |
| 192 | + |
| 193 | + describe('clear chat button', function () { |
| 194 | + it('clears the chat when the user clicks and confirms', async function () { |
| 195 | + const mockChat = createMockChat({ messages: mockMessages }); |
| 196 | + |
| 197 | + await renderOpenAssistantDrawer(mockChat); |
| 198 | + |
| 199 | + const clearButton = screen.getByTestId('assistant-clear-chat'); |
| 200 | + userEvent.click(clearButton); |
| 201 | + |
| 202 | + await waitFor(() => { |
| 203 | + expect(screen.getByTestId('confirmation-modal')).to.exist; |
| 204 | + }); |
| 205 | + |
| 206 | + // There should be messages in the chat |
| 207 | + expect(screen.getByTestId('assistant-message-1')).to.exist; |
| 208 | + expect(screen.getByTestId('assistant-message-2')).to.exist; |
| 209 | + |
| 210 | + const modal = screen.getByTestId('confirmation-modal'); |
| 211 | + const confirmButton = within(modal).getByText('Clear chat'); |
| 212 | + userEvent.click(confirmButton); |
| 213 | + |
| 214 | + await waitForElementToBeRemoved(() => |
| 215 | + screen.getByTestId('confirmation-modal') |
| 216 | + ); |
| 217 | + |
| 218 | + expect(mockChat.messages).to.be.empty; |
| 219 | + expect(screen.queryByTestId('assistant-message-1')).to.not.exist; |
| 220 | + expect(screen.queryByTestId('assistant-message-2')).to.not.exist; |
| 221 | + }); |
| 222 | + |
| 223 | + it('does not clear the chat when the user clicks the button and cancels', async function () { |
| 224 | + const mockChat = createMockChat({ messages: mockMessages }); |
| 225 | + |
| 226 | + await renderOpenAssistantDrawer(mockChat); |
| 227 | + |
| 228 | + const clearButton = screen.getByTestId('assistant-clear-chat'); |
| 229 | + userEvent.click(clearButton); |
| 230 | + |
| 231 | + await waitFor(() => { |
| 232 | + expect(screen.getByTestId('confirmation-modal')).to.exist; |
| 233 | + }); |
| 234 | + |
| 235 | + // There should be messages in the chat |
| 236 | + expect(screen.getByTestId('assistant-message-1')).to.exist; |
| 237 | + expect(screen.getByTestId('assistant-message-2')).to.exist; |
| 238 | + |
| 239 | + const modal = screen.getByTestId('confirmation-modal'); |
| 240 | + const cancelButton = within(modal).getByText('Cancel'); |
| 241 | + userEvent.click(cancelButton); |
| 242 | + |
| 243 | + await waitForElementToBeRemoved(() => |
| 244 | + screen.getByTestId('confirmation-modal') |
| 245 | + ); |
| 246 | + |
| 247 | + expect(mockChat.messages).to.deep.equal(mockMessages); |
| 248 | + expect(screen.getByTestId('assistant-message-1')).to.exist; |
| 249 | + expect(screen.getByTestId('assistant-message-2')).to.exist; |
| 250 | + }); |
| 251 | + }); |
189 | 252 | }); |
190 | 253 |
|
191 | 254 | describe('CompassAssistantProvider', function () { |
|
0 commit comments