1
1
import React from 'react' ;
2
- import { render , screen , userEvent } from '@mongodb-js/testing-library-compass' ;
2
+ import {
3
+ render ,
4
+ screen ,
5
+ userEvent ,
6
+ waitFor ,
7
+ } from '@mongodb-js/testing-library-compass' ;
3
8
import { AssistantChat } from './assistant-chat' ;
4
9
import { expect } from 'chai' ;
5
10
import { createMockChat } from '../test/utils' ;
6
11
import type { AssistantMessage } from './compass-assistant-provider' ;
7
12
8
- // TODO: some internal logic in lg-chat breaks all these tests, re-enable the tests
9
- describe . skip ( 'AssistantChat' , function ( ) {
13
+ describe ( 'AssistantChat' , function ( ) {
14
+ let originalScrollTo : typeof Element . prototype . scrollTo ;
15
+ // Mock scrollTo method for DOM elements to prevent test failures
16
+ before ( function ( ) {
17
+ originalScrollTo = Element . prototype . scrollTo . bind ( Element . prototype ) ;
18
+ Element . prototype . scrollTo = ( ) => { } ;
19
+ } ) ;
20
+ after ( function ( ) {
21
+ Element . prototype . scrollTo = originalScrollTo ;
22
+ } ) ;
23
+
10
24
const mockMessages : AssistantMessage [ ] = [
11
25
{
12
26
id : 'user' ,
@@ -36,8 +50,10 @@ describe.skip('AssistantChat', function () {
36
50
it ( 'renders input field and send button' , function ( ) {
37
51
renderWithChat ( [ ] ) ;
38
52
39
- const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
40
- const sendButton = screen . getByTestId ( 'assistant-chat-send-button' ) ;
53
+ const inputField = screen . getByPlaceholderText (
54
+ 'Ask MongoDB Assistant a question'
55
+ ) ;
56
+ const sendButton = screen . getByLabelText ( 'Send message' ) ;
41
57
42
58
expect ( inputField ) . to . exist ;
43
59
expect ( sendButton ) . to . exist ;
@@ -47,9 +63,9 @@ describe.skip('AssistantChat', function () {
47
63
renderWithChat ( [ ] ) ;
48
64
49
65
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
50
- const inputField = screen . getByTestId (
51
- 'assistant-chat-input '
52
- ) as HTMLInputElement ;
66
+ const inputField = screen . getByPlaceholderText (
67
+ 'Ask MongoDB Assistant a question '
68
+ ) as HTMLTextAreaElement ;
53
69
54
70
userEvent . type ( inputField , 'What is MongoDB?' ) ;
55
71
@@ -60,58 +76,67 @@ describe.skip('AssistantChat', function () {
60
76
renderWithChat ( [ ] ) ;
61
77
62
78
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
63
- const sendButton = screen . getByTestId (
64
- 'assistant-chat-send-button '
79
+ const sendButton = screen . getByLabelText (
80
+ 'Send message '
65
81
) as HTMLButtonElement ;
66
82
67
- expect ( sendButton . disabled ) . to . be . true ;
83
+ expect ( sendButton . getAttribute ( 'aria- disabled' ) ) . to . equal ( ' true' ) ;
68
84
} ) ;
69
85
70
86
it ( 'send button is enabled when input has text' , function ( ) {
71
87
renderWithChat ( [ ] ) ;
72
88
73
- const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
89
+ const inputField = screen . getByPlaceholderText (
90
+ 'Ask MongoDB Assistant a question'
91
+ ) ;
74
92
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
75
- const sendButton = screen . getByTestId (
76
- 'assistant-chat-send-button '
93
+ const sendButton = screen . getByLabelText (
94
+ 'Send message '
77
95
) as HTMLButtonElement ;
78
96
79
97
userEvent . type ( inputField , 'What is MongoDB?' ) ;
80
98
81
99
expect ( sendButton . disabled ) . to . be . false ;
82
100
} ) ;
83
101
84
- it ( 'send button is disabled for whitespace-only input' , function ( ) {
102
+ // Not currently supported by the LeafyGreen Input Bar
103
+ it . skip ( 'send button is disabled for whitespace-only input' , async function ( ) {
85
104
renderWithChat ( [ ] ) ;
86
105
87
- const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
106
+ const inputField = screen . getByPlaceholderText (
107
+ 'Ask MongoDB Assistant a question'
108
+ ) ;
88
109
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
89
- const sendButton = screen . getByTestId (
90
- 'assistant-chat-send-button '
110
+ const sendButton = screen . getByLabelText (
111
+ 'Send message '
91
112
) as HTMLButtonElement ;
92
113
93
114
userEvent . type ( inputField , ' ' ) ;
94
115
95
- expect ( sendButton . disabled ) . to . be . true ;
116
+ await waitFor ( ( ) => {
117
+ expect ( sendButton . getAttribute ( 'aria-disabled' ) ) . to . equal ( 'true' ) ;
118
+ } ) ;
96
119
} ) ;
97
120
98
121
it ( 'displays messages in the chat feed' , function ( ) {
99
122
renderWithChat ( mockMessages ) ;
100
123
101
124
expect ( screen . getByTestId ( 'assistant-message-user' ) ) . to . exist ;
102
125
expect ( screen . getByTestId ( 'assistant-message-assistant' ) ) . to . exist ;
103
- expect ( screen . getByTestId ( 'assistant-message-user' ) ) . to . have . text (
126
+ expect ( screen . getByTestId ( 'assistant-message-user' ) ) . to . contain . text (
104
127
'Hello, MongoDB Assistant!'
105
128
) ;
106
- expect ( screen . getByTestId ( 'assistant-message-assistant' ) ) . to . have . text (
129
+ expect ( screen . getByTestId ( 'assistant-message-assistant' ) ) . to . contain . text (
107
130
'Hello! How can I help you with MongoDB today?'
108
131
) ;
109
132
} ) ;
110
133
111
134
it ( 'calls sendMessage when form is submitted' , function ( ) {
112
135
const { chat } = renderWithChat ( [ ] ) ;
113
- const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
114
- const sendButton = screen . getByTestId ( 'assistant-chat-send-button' ) ;
136
+ const inputField = screen . getByPlaceholderText (
137
+ 'Ask MongoDB Assistant a question'
138
+ ) ;
139
+ const sendButton = screen . getByLabelText ( 'Send message' ) ;
115
140
116
141
userEvent . type ( inputField , 'What is aggregation?' ) ;
117
142
userEvent . click ( sendButton ) ;
@@ -124,24 +149,26 @@ describe.skip('AssistantChat', function () {
124
149
renderWithChat ( [ ] ) ;
125
150
126
151
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
127
- const inputField = screen . getByTestId (
128
- 'assistant-chat-input '
129
- ) as HTMLInputElement ;
152
+ const inputField = screen . getByPlaceholderText (
153
+ 'Ask MongoDB Assistant a question '
154
+ ) as HTMLTextAreaElement ;
130
155
131
156
userEvent . type ( inputField , 'Test message' ) ;
132
157
expect ( inputField . value ) . to . equal ( 'Test message' ) ;
133
158
134
- userEvent . click ( screen . getByTestId ( 'assistant-chat-send-button ') ) ;
159
+ userEvent . click ( screen . getByLabelText ( 'Send message ') ) ;
135
160
expect ( inputField . value ) . to . equal ( '' ) ;
136
161
} ) ;
137
162
138
163
it ( 'trims whitespace from input before sending' , function ( ) {
139
164
const { chat } = renderWithChat ( [ ] ) ;
140
165
141
- const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
166
+ const inputField = screen . getByPlaceholderText (
167
+ 'Ask MongoDB Assistant a question'
168
+ ) ;
142
169
143
170
userEvent . type ( inputField , ' What is sharding? ' ) ;
144
- userEvent . click ( screen . getByTestId ( 'assistant-chat-send-button ') ) ;
171
+ userEvent . click ( screen . getByLabelText ( 'Send message ') ) ;
145
172
146
173
expect ( chat . sendMessage . calledWith ( { text : 'What is sharding?' } ) ) . to . be
147
174
. true ;
@@ -150,8 +177,10 @@ describe.skip('AssistantChat', function () {
150
177
it ( 'does not call sendMessage when input is empty or whitespace-only' , function ( ) {
151
178
const { chat } = renderWithChat ( [ ] ) ;
152
179
153
- const inputField = screen . getByTestId ( 'assistant-chat-input' ) ;
154
- const chatForm = screen . getByTestId ( 'assistant-chat-form' ) ;
180
+ const inputField = screen . getByPlaceholderText (
181
+ 'Ask MongoDB Assistant a question'
182
+ ) ;
183
+ const chatForm = screen . getByTestId ( 'assistant-chat-input' ) ;
155
184
156
185
// Test empty input
157
186
userEvent . click ( chatForm ) ;
@@ -169,16 +198,12 @@ describe.skip('AssistantChat', function () {
169
198
const userMessage = screen . getByTestId ( 'assistant-message-user' ) ;
170
199
const assistantMessage = screen . getByTestId ( 'assistant-message-assistant' ) ;
171
200
172
- // User messages should have different background color than assistant messages
201
+ // User messages should have different class names than assistant messages
173
202
expect ( userMessage ) . to . exist ;
174
203
expect ( assistantMessage ) . to . exist ;
175
204
176
- const userStyle = window . getComputedStyle ( userMessage ) ;
177
- const assistantStyle = window . getComputedStyle ( assistantMessage ) ;
178
-
179
- expect ( userStyle . backgroundColor ) . to . not . equal (
180
- assistantStyle . backgroundColor
181
- ) ;
205
+ // Check that they have different class names (indicating different styling)
206
+ expect ( userMessage . className ) . to . not . equal ( assistantMessage . className ) ;
182
207
} ) ;
183
208
184
209
it ( 'handles messages with multiple text parts' , function ( ) {
0 commit comments