|
1 | | -import { aTimeout, elementUpdated, expect, fixture } from '@open-wc/testing'; |
| 1 | +import { |
| 2 | + aTimeout, |
| 3 | + elementUpdated, |
| 4 | + expect, |
| 5 | + fixture, |
| 6 | + nextFrame, |
| 7 | +} from '@open-wc/testing'; |
2 | 8 | import { html } from 'lit'; |
3 | 9 | import { type SinonFakeTimers, spy, useFakeTimers } from 'sinon'; |
| 10 | +import { arrowDown, arrowUp } from '../common/controllers/key-bindings.js'; |
4 | 11 | import { defineComponents } from '../common/definitions/defineComponents.js'; |
5 | 12 | import { |
| 13 | + isFocused, |
6 | 14 | simulateBlur, |
7 | 15 | simulateClick, |
8 | 16 | simulateFocus, |
@@ -1027,6 +1035,8 @@ describe('Chat', () => { |
1027 | 1035 | expect(chat.messages.length).to.equal(1); |
1028 | 1036 | expect(chat.messages[0].text).to.equal('Hello!'); |
1029 | 1037 | expect(chat.messages[0].sender).to.equal('user'); |
| 1038 | + // The focus should be on the input area after send button is clicked |
| 1039 | + expect(isFocused(textArea)).to.be.true; |
1030 | 1040 | } |
1031 | 1041 | }); |
1032 | 1042 |
|
@@ -1056,6 +1066,12 @@ describe('Chat', () => { |
1056 | 1066 | expect(chat.messages.length).to.equal(1); |
1057 | 1067 | expect(chat.messages[0].text).to.equal('Suggestion 1'); |
1058 | 1068 | expect(chat.messages[0].sender).to.equal('user'); |
| 1069 | + // The focus should be on the input area after suggestion click |
| 1070 | + const inputArea = chat.shadowRoot?.querySelector('igc-chat-input'); |
| 1071 | + const textArea = inputArea?.shadowRoot?.querySelector( |
| 1072 | + 'igc-textarea' |
| 1073 | + ) as HTMLElement; |
| 1074 | + expect(isFocused(textArea)).to.be.true; |
1059 | 1075 | } |
1060 | 1076 | }); |
1061 | 1077 |
|
@@ -1177,8 +1193,77 @@ describe('Chat', () => { |
1177 | 1193 | expect(chat.messages.length).to.equal(1); |
1178 | 1194 | expect(chat.messages[0].text).to.equal('Hello!'); |
1179 | 1195 | expect(chat.messages[0].sender).to.equal('user'); |
| 1196 | + |
| 1197 | + // The focus should be on the input area after message is sent |
| 1198 | + expect(isFocused(textArea)).to.be.true; |
1180 | 1199 | } |
1181 | 1200 | }); |
| 1201 | + |
| 1202 | + it('should activates the recent message when the message list is focused', async () => { |
| 1203 | + chat.messages = messages; |
| 1204 | + await elementUpdated(chat); |
| 1205 | + await aTimeout(500); |
| 1206 | + |
| 1207 | + const messageContainer = chat.shadowRoot |
| 1208 | + ?.querySelector('igc-chat-message-list') |
| 1209 | + ?.shadowRoot?.querySelector('.message-container') as HTMLElement; |
| 1210 | + messageContainer.focus(); |
| 1211 | + await elementUpdated(chat); |
| 1212 | + |
| 1213 | + expect(messageContainer.getAttribute('aria-activedescendant')).to.equal( |
| 1214 | + 'message-4' |
| 1215 | + ); |
| 1216 | + |
| 1217 | + const messageElements = |
| 1218 | + messageContainer?.querySelectorAll('igc-chat-message'); |
| 1219 | + messageElements?.forEach((message, index) => { |
| 1220 | + if (index === messages.length - 1) { |
| 1221 | + expect(message.classList.contains('active')).to.be.true; |
| 1222 | + } else { |
| 1223 | + expect(message.classList.contains('active')).to.be.false; |
| 1224 | + } |
| 1225 | + }); |
| 1226 | + }); |
| 1227 | + }); |
| 1228 | + |
| 1229 | + it('should activates the next/previous message on `ArrowDown`/`ArrowUp`', async () => { |
| 1230 | + chat.messages = messages; |
| 1231 | + await elementUpdated(chat); |
| 1232 | + await aTimeout(500); |
| 1233 | + |
| 1234 | + const messageContainer = chat.shadowRoot |
| 1235 | + ?.querySelector('igc-chat-message-list') |
| 1236 | + ?.shadowRoot?.querySelector('.message-container') as HTMLElement; |
| 1237 | + messageContainer.focus(); |
| 1238 | + await elementUpdated(chat); |
| 1239 | + await nextFrame(); |
| 1240 | + await nextFrame(); |
| 1241 | + |
| 1242 | + // Activates the previous message on `ArrowUp` |
| 1243 | + messageContainer.dispatchEvent( |
| 1244 | + new KeyboardEvent('keydown', { |
| 1245 | + key: arrowUp, |
| 1246 | + bubbles: true, |
| 1247 | + cancelable: true, |
| 1248 | + }) |
| 1249 | + ); |
| 1250 | + await nextFrame(); |
| 1251 | + expect(messageContainer.getAttribute('aria-activedescendant')).to.equal( |
| 1252 | + 'message-3' |
| 1253 | + ); |
| 1254 | + |
| 1255 | + // Activates the next message on `ArrowDown` |
| 1256 | + messageContainer.dispatchEvent( |
| 1257 | + new KeyboardEvent('keydown', { |
| 1258 | + key: arrowDown, |
| 1259 | + bubbles: true, |
| 1260 | + cancelable: true, |
| 1261 | + }) |
| 1262 | + ); |
| 1263 | + await nextFrame(); |
| 1264 | + expect(messageContainer.getAttribute('aria-activedescendant')).to.equal( |
| 1265 | + 'message-4' |
| 1266 | + ); |
1182 | 1267 | }); |
1183 | 1268 | }); |
1184 | 1269 |
|
|
0 commit comments