Skip to content

Commit 39d443d

Browse files
committed
test(chat): add input focus & KB arrow up/down tests
1 parent d325646 commit 39d443d

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

src/components/chat/chat.spec.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
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';
28
import { html } from 'lit';
39
import { type SinonFakeTimers, spy, useFakeTimers } from 'sinon';
10+
import { arrowDown, arrowUp } from '../common/controllers/key-bindings.js';
411
import { defineComponents } from '../common/definitions/defineComponents.js';
512
import {
13+
isFocused,
614
simulateBlur,
715
simulateClick,
816
simulateFocus,
@@ -1027,6 +1035,8 @@ describe('Chat', () => {
10271035
expect(chat.messages.length).to.equal(1);
10281036
expect(chat.messages[0].text).to.equal('Hello!');
10291037
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;
10301040
}
10311041
});
10321042

@@ -1056,6 +1066,12 @@ describe('Chat', () => {
10561066
expect(chat.messages.length).to.equal(1);
10571067
expect(chat.messages[0].text).to.equal('Suggestion 1');
10581068
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;
10591075
}
10601076
});
10611077

@@ -1177,8 +1193,77 @@ describe('Chat', () => {
11771193
expect(chat.messages.length).to.equal(1);
11781194
expect(chat.messages[0].text).to.equal('Hello!');
11791195
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;
11801199
}
11811200
});
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+
);
11821267
});
11831268
});
11841269

0 commit comments

Comments
 (0)