Skip to content

Commit b665917

Browse files
asynclizcopybara-github
authored andcommitted
test(chips): add multi-action navigation tests
PiperOrigin-RevId: 540978409
1 parent 431ba18 commit b665917

File tree

1 file changed

+131
-2
lines changed

1 file changed

+131
-2
lines changed

chips/lib/multi-action-chip_test.ts

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
// import 'jasmine'; (google3-only)
88

9-
import {html} from 'lit';
10-
import {customElement, query} from 'lit/decorators.js';
9+
import {html, nothing} from 'lit';
10+
import {customElement, property, query} from 'lit/decorators.js';
1111

1212
import {Environment} from '../../testing/environment.js';
1313
import {Harness} from '../../testing/harness.js';
@@ -19,6 +19,7 @@ import {renderRemoveButton} from './trailing-icons.js';
1919
class TestMultiActionChip extends MultiActionChip {
2020
@query('#primary') primaryAction!: HTMLElement;
2121
@query('.trailing.action') trailingAction!: HTMLElement;
22+
@property() noTrailingAction = false;
2223

2324
protected primaryId = 'primary';
2425

@@ -27,6 +28,10 @@ class TestMultiActionChip extends MultiActionChip {
2728
}
2829

2930
protected override renderTrailingAction() {
31+
if (this.noTrailingAction) {
32+
return nothing;
33+
}
34+
3035
return renderRemoveButton(
3136
{ariaLabel: this.ariaLabelRemove, disabled: this.disabled});
3237
}
@@ -42,6 +47,130 @@ describe('Multi-action chips', () => {
4247
return chip;
4348
}
4449

50+
describe('navigation', () => {
51+
it('should move internal focus forwards', async () => {
52+
const chip = await setupTest();
53+
const primaryHarness = new Harness(chip.primaryAction);
54+
55+
await primaryHarness.focusWithKeyboard();
56+
expect(chip.primaryAction.matches(':focus'))
57+
.withContext('primary action is focused')
58+
.toBeTrue();
59+
60+
await primaryHarness.keypress('ArrowRight');
61+
expect(chip.trailingAction.matches(':focus'))
62+
.withContext('trailing action is focused')
63+
.toBeTrue();
64+
});
65+
66+
it('should move internal focus forwards in rtl', async () => {
67+
const chip = await setupTest();
68+
chip.style.direction = 'rtl';
69+
const primaryHarness = new Harness(chip.primaryAction);
70+
71+
await primaryHarness.focusWithKeyboard();
72+
expect(chip.primaryAction.matches(':focus'))
73+
.withContext('primary action is focused')
74+
.toBeTrue();
75+
76+
await primaryHarness.keypress('ArrowLeft');
77+
expect(chip.trailingAction.matches(':focus'))
78+
.withContext('trailing action is focused')
79+
.toBeTrue();
80+
});
81+
82+
it('should move internal focus backwards', async () => {
83+
const chip = await setupTest();
84+
const trailingHarness = new Harness(chip.trailingAction);
85+
86+
await trailingHarness.focusWithKeyboard();
87+
expect(chip.trailingAction.matches(':focus'))
88+
.withContext('trailing action is focused')
89+
.toBeTrue();
90+
91+
await trailingHarness.keypress('ArrowLeft');
92+
expect(chip.primaryAction.matches(':focus'))
93+
.withContext('primary action is focused')
94+
.toBeTrue();
95+
});
96+
97+
it('should move internal focus backwards in rtl', async () => {
98+
const chip = await setupTest();
99+
chip.style.direction = 'rtl';
100+
const trailingHarness = new Harness(chip.trailingAction);
101+
102+
await trailingHarness.focusWithKeyboard();
103+
expect(chip.trailingAction.matches(':focus'))
104+
.withContext('trailing action is focused')
105+
.toBeTrue();
106+
107+
await trailingHarness.keypress('ArrowRight');
108+
expect(chip.primaryAction.matches(':focus'))
109+
.withContext('primary action is focused')
110+
.toBeTrue();
111+
});
112+
113+
it('should not bubble when navigating internally', async () => {
114+
const chip = await setupTest();
115+
const primaryHarness = new Harness(chip.primaryAction);
116+
const keydownHandler = jasmine.createSpy();
117+
if (!chip.parentElement) {
118+
throw new Error('Expected chip to have a parentElement for test.');
119+
}
120+
121+
chip.parentElement.addEventListener('keydown', keydownHandler);
122+
123+
await primaryHarness.focusWithKeyboard();
124+
await primaryHarness.keypress('ArrowRight');
125+
expect(keydownHandler).not.toHaveBeenCalled();
126+
});
127+
128+
it('should bubble event when navigating forward past trailing action',
129+
async () => {
130+
const chip = await setupTest();
131+
const trailingHarness = new Harness(chip.trailingAction);
132+
const keydownHandler = jasmine.createSpy();
133+
if (!chip.parentElement) {
134+
throw new Error('Expected chip to have a parentElement for test.');
135+
}
136+
137+
chip.parentElement.addEventListener('keydown', keydownHandler);
138+
139+
await trailingHarness.focusWithKeyboard();
140+
await trailingHarness.keypress('ArrowRight');
141+
expect(keydownHandler).toHaveBeenCalledTimes(1);
142+
});
143+
144+
it('should bubble event when navigating backward before primary action',
145+
async () => {
146+
const chip = await setupTest();
147+
const primaryHarness = new Harness(chip.primaryAction);
148+
const keydownHandler = jasmine.createSpy();
149+
if (!chip.parentElement) {
150+
throw new Error('Expected chip to have a parentElement for test.');
151+
}
152+
153+
chip.parentElement.addEventListener('keydown', keydownHandler);
154+
155+
await primaryHarness.focusWithKeyboard();
156+
await primaryHarness.keypress('ArrowLeft');
157+
expect(keydownHandler).toHaveBeenCalledTimes(1);
158+
});
159+
160+
it('should do nothing if it does not have multiple actions', async () => {
161+
const chip = await setupTest();
162+
chip.noTrailingAction = true;
163+
await env.waitForStability();
164+
165+
const primaryHarness = new Harness(chip.primaryAction);
166+
await primaryHarness.focusWithKeyboard();
167+
await primaryHarness.keypress('ArrowLeft');
168+
expect(chip.primaryAction.matches(':focus'))
169+
.withContext('primary action is still focused')
170+
.toBeTrue();
171+
});
172+
});
173+
45174
describe('remove action', () => {
46175
it('should remove chip from DOM when remove button clicked', async () => {
47176
const chip = await setupTest();

0 commit comments

Comments
 (0)