@@ -3,16 +3,17 @@ import { render, screen, userEvent } from '@mongodb-js/testing-library-compass';
3
3
import { AssistantChat } from './assistant-chat' ;
4
4
import { expect } from 'chai' ;
5
5
import type { UIMessage } from './@ai-sdk/react/use-chat' ;
6
+ import { createMockChat } from '../test/utils' ;
6
7
7
8
describe ( 'AssistantChat' , function ( ) {
8
9
const mockMessages : UIMessage [ ] = [
9
10
{
10
- id : '1 ' ,
11
+ id : 'user ' ,
11
12
role : 'user' ,
12
13
parts : [ { type : 'text' , text : 'Hello, MongoDB Assistant!' } ] ,
13
14
} ,
14
15
{
15
- id : '2 ' ,
16
+ id : 'assistant ' ,
16
17
role : 'assistant' ,
17
18
parts : [
18
19
{
@@ -23,8 +24,16 @@ describe('AssistantChat', function () {
23
24
} ,
24
25
] ;
25
26
27
+ function renderWithChat ( messages : UIMessage [ ] ) {
28
+ const chat = createMockChat ( { messages } ) ;
29
+ return {
30
+ result : render ( < AssistantChat chat = { chat } /> ) ,
31
+ chat,
32
+ } ;
33
+ }
34
+
26
35
it ( 'renders input field and send button' , function ( ) {
27
- render ( < AssistantChat messages = { [ ] } /> ) ;
36
+ renderWithChat ( [ ] ) ;
28
37
29
38
const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
30
39
const sendButton = screen . getByTestId ( 'assistant-chat-send-button' ) ;
@@ -34,7 +43,7 @@ describe('AssistantChat', function () {
34
43
} ) ;
35
44
36
45
it ( 'input field accepts text input' , function ( ) {
37
- render ( < AssistantChat messages = { [ ] } /> ) ;
46
+ renderWithChat ( [ ] ) ;
38
47
39
48
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
40
49
const inputField = screen . getByTestId (
@@ -47,7 +56,7 @@ describe('AssistantChat', function () {
47
56
} ) ;
48
57
49
58
it ( 'send button is disabled when input is empty' , function ( ) {
50
- render ( < AssistantChat messages = { [ ] } /> ) ;
59
+ renderWithChat ( [ ] ) ;
51
60
52
61
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
53
62
const sendButton = screen . getByTestId (
@@ -58,7 +67,7 @@ describe('AssistantChat', function () {
58
67
} ) ;
59
68
60
69
it ( 'send button is enabled when input has text' , function ( ) {
61
- render ( < AssistantChat messages = { [ ] } /> ) ;
70
+ renderWithChat ( [ ] ) ;
62
71
63
72
const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
64
73
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
@@ -72,7 +81,7 @@ describe('AssistantChat', function () {
72
81
} ) ;
73
82
74
83
it ( 'send button is disabled for whitespace-only input' , function ( ) {
75
- render ( < AssistantChat messages = { [ ] } /> ) ;
84
+ renderWithChat ( [ ] ) ;
76
85
77
86
const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
78
87
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
@@ -86,36 +95,32 @@ describe('AssistantChat', function () {
86
95
} ) ;
87
96
88
97
it ( 'displays messages in the chat feed' , function ( ) {
89
- render ( < AssistantChat messages = { mockMessages } /> ) ;
98
+ renderWithChat ( mockMessages ) ;
90
99
91
100
expect ( screen . getByTestId ( 'assistant-message-user' ) ) . to . exist ;
92
101
expect ( screen . getByTestId ( 'assistant-message-assistant' ) ) . to . exist ;
93
- expect ( screen . getByText ( 'Hello, MongoDB Assistant!' ) ) . to . exist ;
94
- expect ( screen . getByText ( 'Hello! How can I help you with MongoDB today?' ) ) . to
95
- . exist ;
102
+ expect ( screen . getByTestId ( 'assistant-message-user' ) ) . to . have . text (
103
+ 'Hello, MongoDB Assistant!'
104
+ ) ;
105
+ expect ( screen . getByTestId ( 'assistant-message-assistant' ) ) . to . have . text (
106
+ 'Hello! How can I help you with MongoDB today?'
107
+ ) ;
96
108
} ) ;
97
109
98
- it ( 'calls onSendMessage when form is submitted' , function ( ) {
99
- let sentMessage = '' ;
100
- const handleSendMessage = ( message : string ) => {
101
- sentMessage = message ;
102
- } ;
103
-
104
- render ( < AssistantChat messages = { [ ] } onSendMessage = { handleSendMessage } /> ) ;
105
-
110
+ it ( 'calls sendMessage when form is submitted' , function ( ) {
111
+ const { chat } = renderWithChat ( [ ] ) ;
106
112
const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
107
113
const sendButton = screen . getByTestId ( 'assistant-chat-send-button' ) ;
108
114
109
115
userEvent . type ( inputField , 'What is aggregation?' ) ;
110
116
userEvent . click ( sendButton ) ;
111
117
112
- expect ( sentMessage ) . to . equal ( 'What is aggregation?' ) ;
118
+ expect ( chat . sendMessage . calledWith ( { text : 'What is aggregation?' } ) ) . to . be
119
+ . true ;
113
120
} ) ;
114
121
115
122
it ( 'clears input field after successful submission' , function ( ) {
116
- const handleSendMessage = ( ) => { } ;
117
-
118
- render ( < AssistantChat messages = { [ ] } onSendMessage = { handleSendMessage } /> ) ;
123
+ renderWithChat ( [ ] ) ;
119
124
120
125
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
121
126
const inputField = screen . getByTestId (
@@ -130,44 +135,35 @@ describe('AssistantChat', function () {
130
135
} ) ;
131
136
132
137
it ( 'trims whitespace from input before sending' , function ( ) {
133
- let sentMessage = '' ;
134
- const handleSendMessage = ( message : string ) => {
135
- sentMessage = message ;
136
- } ;
137
-
138
- render ( < AssistantChat messages = { [ ] } onSendMessage = { handleSendMessage } /> ) ;
138
+ const { chat } = renderWithChat ( [ ] ) ;
139
139
140
140
const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
141
141
142
142
userEvent . type ( inputField , ' What is sharding? ' ) ;
143
143
userEvent . click ( screen . getByTestId ( 'assistant-chat-send-button' ) ) ;
144
144
145
- expect ( sentMessage ) . to . equal ( 'What is sharding?' ) ;
145
+ expect ( chat . sendMessage . calledWith ( { text : 'What is sharding?' } ) ) . to . be
146
+ . true ;
146
147
} ) ;
147
148
148
- it ( 'does not call onSendMessage when input is empty or whitespace-only' , function ( ) {
149
- let messageSent = false ;
150
- const handleSendMessage = ( ) => {
151
- messageSent = true ;
152
- } ;
153
-
154
- render ( < AssistantChat messages = { [ ] } onSendMessage = { handleSendMessage } /> ) ;
149
+ it ( 'does not call sendMessage when input is empty or whitespace-only' , function ( ) {
150
+ const { chat } = renderWithChat ( [ ] ) ;
155
151
156
152
const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
157
153
const chatForm = screen . getByTestId ( 'assistant-chat-form' ) ;
158
154
159
155
// Test empty input
160
156
userEvent . click ( chatForm ) ;
161
- expect ( messageSent ) . to . be . false ;
157
+ expect ( chat . sendMessage . notCalled ) . to . be . true ;
162
158
163
159
// Test whitespace-only input
164
160
userEvent . type ( inputField , ' ' ) ;
165
161
userEvent . click ( chatForm ) ;
166
- expect ( messageSent ) . to . be . false ;
162
+ expect ( chat . sendMessage . notCalled ) . to . be . true ;
167
163
} ) ;
168
164
169
165
it ( 'displays user and assistant messages with different styling' , function ( ) {
170
- render ( < AssistantChat messages = { mockMessages } /> ) ;
166
+ renderWithChat ( mockMessages ) ;
171
167
172
168
const userMessage = screen . getByTestId ( 'assistant-message-user' ) ;
173
169
const assistantMessage = screen . getByTestId ( 'assistant-message-assistant' ) ;
@@ -196,7 +192,7 @@ describe('AssistantChat', function () {
196
192
} ,
197
193
] ;
198
194
199
- render ( < AssistantChat messages = { messagesWithMultipleParts } /> ) ;
195
+ renderWithChat ( messagesWithMultipleParts ) ;
200
196
201
197
expect ( screen . getByText ( 'Here is part 1. And here is part 2.' ) ) . to . exist ;
202
198
} ) ;
@@ -215,7 +211,7 @@ describe('AssistantChat', function () {
215
211
} ,
216
212
] ;
217
213
218
- render ( < AssistantChat messages = { messagesWithMixedParts } /> ) ;
214
+ renderWithChat ( messagesWithMixedParts ) ;
219
215
220
216
expect ( screen . getByText ( 'This is text content. More text content.' ) ) . to
221
217
. exist ;
0 commit comments