forked from material-components/material-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd-navigation-tab_test.ts
252 lines (210 loc) · 7.93 KB
/
md-navigation-tab_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {html, render} from 'lit';
import {customElement} from 'lit/decorators.js';
import {Environment} from '../testing/environment.js';
import {NavigationTabHarness} from './harness.js';
import {MdNavigationTab} from './navigation-tab.js';
@customElement('md-test-navigation-tab')
class TestNavigationTab extends MdNavigationTab {
}
declare global {
interface HTMLElementTagNameMap {
'md-test-navigation-tab': TestNavigationTab;
}
}
describe('mwc-navigation-tab', () => {
const env = new Environment();
async function setupTest() {
// Variant type does not matter for shared tests
const element =
env.render(html`<md-test-navigation-tab></md-test-navigation-tab>`)
.querySelector('md-test-navigation-tab');
if (!element) {
throw new Error('Could not query rendered <md-test-navigation-tab>.');
}
await env.waitForStability();
const button = element.renderRoot.querySelector('button');
if (!button) {
throw new Error('Could not query rendered <button>.');
}
const navTab = element.renderRoot.querySelector('.md3-navigation-tab');
if (!navTab) {
throw new Error('Could not query rendered .md3-navigation-tab.');
}
const focusRing = element.renderRoot.querySelector('md-focus-ring');
if (!focusRing) {
throw new Error('Could not query rendered <md-focus-ring>.');
}
return {
button,
navTab,
focusRing,
harness: new NavigationTabHarness(element),
};
}
describe('basic', () => {
it('initializes as an md-navigation-tab', async () => {
const {harness} = await setupTest();
expect(harness.element).toBeInstanceOf(MdNavigationTab);
expect(harness.element.active).toBeFalse();
expect(harness.element.hideInactiveLabel).toBeFalse();
expect(harness.element.label).toBeUndefined();
expect(harness.element.badgeValue).toEqual('');
expect(harness.element.showBadge).toBeFalse();
});
it('emits interaction event on click', async () => {
const {harness} = await setupTest();
const interactionHandler = jasmine.createSpy();
harness.element.addEventListener(
'navigation-tab-interaction', interactionHandler);
await harness.clickWithMouse();
expect(interactionHandler).toHaveBeenCalled();
});
it('focus() sets focus on button element', async () => {
const {harness, button} = await setupTest();
harness.element.focus();
expect(button.matches(':focus')).toBeTrue();
});
});
it('on render navigation-tab-rendered event fires', async () => {
const element = document.createElement('md-test-navigation-tab');
const renderedHandler = jasmine.createSpy();
element.addEventListener('navigation-tab-rendered', renderedHandler);
env.render(html`${element}`);
await env.waitForStability();
expect(renderedHandler).toHaveBeenCalled();
});
describe('active', () => {
it('affects `aria-selected` of native button', async () => {
const {harness, button} = await setupTest();
harness.element.active = true;
await env.waitForStability();
expect(button.getAttribute('aria-selected')).toEqual('true');
harness.element.active = false;
await env.waitForStability();
expect(button.getAttribute('aria-selected')).toEqual('false');
});
it('affects `tabindex` of native button', async () => {
const {harness, button} = await setupTest();
harness.element.active = true;
await env.waitForStability();
expect(button.getAttribute('tabindex')).toEqual('0');
harness.element.active = false;
await env.waitForStability();
expect(button.getAttribute('tabindex')).toEqual('-1');
});
it('sets the correct classes', async () => {
const {harness, navTab} = await setupTest();
harness.element.active = true;
await env.waitForStability();
expect(navTab.classList.contains('md3-navigation-tab--active'))
.toBeTrue();
});
});
describe('hideInactiveLabel', () => {
it('sets the correct classes', async () => {
const {harness, navTab} = await setupTest();
harness.element.hideInactiveLabel = true;
await env.waitForStability();
expect(
navTab.classList.contains('md3-navigation-tab--hide-inactive-label'))
.toBeTrue();
});
});
describe('label', () => {
it('displays label text', async () => {
const {harness} = await setupTest();
harness.element.label = 'foo';
await env.waitForStability();
const content = harness.element.shadowRoot!.querySelector(
'.md3-navigation-tab__label-text')!;
expect(content.textContent!.trim()).toEqual('foo');
});
});
describe('ariaLabel', () => {
it('affects `aria-label` of native button', async () => {
const {harness, button} = await setupTest();
harness.element.label = 'foo';
harness.element.ariaLabel = 'bar';
await env.waitForStability();
expect(button.getAttribute('aria-label')).toEqual('bar');
});
});
describe('showBadge', () => {
it('displays badge', async () => {
const {harness} = await setupTest();
harness.element.showBadge = true;
await env.waitForStability();
const badge = harness.element.renderRoot.querySelector('md-badge');
expect(badge).toBeDefined();
});
it('does not display badge if showBadge is false', async () => {
const {harness} = await setupTest();
harness.element.showBadge = false;
await env.waitForStability();
const badge = harness.element.renderRoot.querySelector('md-badge');
expect(badge).toEqual(null);
});
});
describe('badgeValue', () => {
it('displays badge value', async () => {
const {harness} = await setupTest();
harness.element.showBadge = true;
harness.element.badgeValue = '9';
await env.waitForStability();
const badge = harness.element.renderRoot.querySelector('md-badge');
expect(badge?.value).toEqual('9');
});
});
describe('icons', () => {
it('nodes with `slot=activeIcon` will serve as the active icon',
async () => {
const {harness} = await setupTest();
const icons = html`
<i slot="activeIcon" class="material-icons">star</i>
<i slot="inactiveIcon" class="material-icons">star_border</i>
`;
render(icons, harness.element);
const icon =
harness.element.querySelector<HTMLElement>('[slot="activeIcon"]')!;
expect(icon.textContent!.trim()).toEqual('star');
});
it('nodes with `slot=inactiveIcon` will serve as the inactive icon',
async () => {
const {harness} = await setupTest();
const icons = html`
<i slot="activeIcon" class="material-icons">star</i>
<i slot="inactiveIcon" class="material-icons">star_border</i>
`;
render(icons, harness.element);
const icon = harness.element.querySelector<HTMLElement>(
'[slot="inactiveIcon"]')!;
expect(icon.textContent!.trim()).toEqual('star_border');
});
});
describe('focus ring', () => {
it('hidden on non-keyboard focus', async () => {
const {harness, focusRing} = await setupTest();
await harness.clickWithMouse();
expect(focusRing.visible).toBeFalse();
});
it('visible on keyboard focus and hides on blur', async () => {
const {harness, focusRing} = await setupTest();
await harness.focusWithKeyboard();
expect(focusRing.visible).toBeTrue();
await harness.blur();
expect(focusRing.visible).toBeFalse();
});
it('hidden after pointer interaction', async () => {
const {harness, focusRing} = await setupTest();
await harness.focusWithKeyboard();
expect(focusRing.visible).toBeTrue();
await harness.clickWithMouse();
expect(focusRing.visible).toBeFalse();
});
});
});