Skip to content

Commit 2b111bd

Browse files
chore(tabs): adds screenshot tests
PiperOrigin-RevId: 532182408
1 parent 6116c34 commit 2b111bd

File tree

5 files changed

+49
-25
lines changed

5 files changed

+49
-25
lines changed

tabs/harness.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ import {Tabs} from './lib/tabs.js';
1515
export class TabHarness extends Harness<Tab> {
1616
override async getInteractiveElement() {
1717
await this.element.updateComplete;
18-
return this.element.querySelector<HTMLButtonElement|HTMLLinkElement>(
19-
'.button')!;
18+
return this.element.renderRoot
19+
.querySelector<HTMLButtonElement|HTMLLinkElement>('.button')!;
2020
}
2121

22-
async isIndicatorShowing() {
22+
private async completeIndicatorAnimation() {
2323
await this.element.updateComplete;
24+
const animations = this.element.indicator.getAnimations();
25+
for (const animation of animations) {
26+
animation.finish();
27+
}
28+
}
29+
30+
async isIndicatorShowing() {
31+
await this.completeIndicatorAnimation();
2432
const opacity = getComputedStyle(this.element.indicator)['opacity'];
2533
return opacity === '1';
2634
}
@@ -30,6 +38,17 @@ export class TabHarness extends Harness<Tab> {
3038
* Test harness for Tabs.
3139
*/
3240
export class TabsHarness extends Harness<Tabs> {
41+
// Note, Tabs interactive element is the interactive element of the
42+
// selected tab.
43+
override async getInteractiveElement() {
44+
await this.element.updateComplete;
45+
const selectedItemHarness =
46+
(this.element.selectedItem as ElementWithHarness<Tab>).harness as
47+
TabHarness ??
48+
new TabHarness(this.element.selectedItem);
49+
return await selectedItemHarness.getInteractiveElement();
50+
}
51+
3352
get harnessedItems() {
3453
// Test access to protected property
3554
// tslint:disable-next-line:no-dict-access-on-struct-type

tabs/lib/tab.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ export class Tab extends LitElement {
8181

8282
@state() private showRipple = false;
8383

84-
// whether or not selection state can be animated; used to avoid initial
85-
// animation and becomes true one task after first update.
86-
private canAnimate = false;
87-
8884
constructor() {
8985
super();
9086
if (!isServer) {
@@ -125,13 +121,8 @@ export class Tab extends LitElement {
125121
</button>`;
126122
}
127123

128-
protected override async firstUpdated() {
129-
await new Promise(requestAnimationFrame);
130-
this.canAnimate = true;
131-
}
132-
133124
protected override updated(changed: PropertyValues) {
134-
if (changed.has('selected') && this.shouldAnimate()) {
125+
if (changed.has('selected') && !this.disabled) {
135126
this.animateSelected();
136127
}
137128
}
@@ -144,10 +135,6 @@ export class Tab extends LitElement {
144135
dispatchActivationClick(this.button);
145136
};
146137

147-
private shouldAnimate() {
148-
return this.canAnimate && !this.disabled;
149-
}
150-
151138
private readonly getRipple = () => {
152139
this.showRipple = true;
153140
return this.ripple;

tabs/lib/tabs.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ export class Tabs extends LitElement {
232232
}
233233

234234
protected override async updated(changed: PropertyValues) {
235-
// if there's no items, they may not be ready, so wait before syncronizing
236-
if (this.items.length === 0) {
237-
await new Promise(requestAnimationFrame);
238-
}
239235
const itemsOrVariantChanged =
240236
changed.has('items') || changed.has('variant');
241237
// sync variant with items.
@@ -296,13 +292,20 @@ export class Tabs extends LitElement {
296292
this.requestUpdate();
297293
}
298294

295+
private async itemsUpdateComplete() {
296+
for (const item of this.items) {
297+
await item.updateComplete;
298+
}
299+
return true;
300+
}
301+
299302
// ensures the given item is visible in view; defaults to the selected item
300303
private async scrollItemIntoView(item = this.selectedItem) {
301304
if (!item) {
302305
return;
303306
}
304307
// wait for items to render.
305-
await new Promise(requestAnimationFrame);
308+
await this.itemsUpdateComplete();
306309
const isVertical = this.orientation === 'vertical';
307310
const offset = isVertical ? item.offsetTop : item.offsetLeft;
308311
const extent = isVertical ? item.offsetHeight : item.offsetWidth;
@@ -311,8 +314,11 @@ export class Tabs extends LitElement {
311314
const min = offset - this.scrollMargin;
312315
const max = offset + extent - hostExtent + this.scrollMargin;
313316
const to = Math.min(min, Math.max(max, scroll));
317+
const behavior =
318+
// type annotation because `instant` is valid but not included in type.
319+
this.focusedItem !== undefined ? 'smooth' : 'instant' as ScrollBehavior;
314320
this.scrollTo({
315-
behavior: 'smooth',
321+
behavior,
316322
[isVertical ? 'left' : 'top']: 0,
317323
[isVertical ? 'top' : 'left']: to
318324
});

tabs/test/window_size.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"capabilities": {
3+
"goog:chromeOptions": {
4+
"args": ["--window-position=0,0", "--window-size=3400,2215"]
5+
},
6+
"moz:firefoxOptions": {
7+
"args": ["-width=3400","-height=2215"]
8+
}
9+
},
10+
"extension": {
11+
"xvfbResolution": "3400x2215x24"
12+
}
13+
}

tokens/_md-comp-tab.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ $_default: (
2424
'md-sys-shape': md-sys-shape.values(),
2525
'md-sys-state': md-sys-state.values(),
2626
'md-sys-typescale': md-sys-typescale.values(),
27-
'md-comp-divider': md-comp-divider.values(),
2827
);
2928

3029
$supported-tokens: (
@@ -242,7 +241,7 @@ $unsupported-tokens: (
242241

243242
// add tokens for divider / label-text
244243
@function _add-missing-tokens($tokens, $deps) {
245-
$divider-tokens: map.get($deps, 'md-comp-divider');
244+
$divider-tokens: md-comp-divider.values();
246245
@each $key, $value in $divider-tokens {
247246
$key: 'divider-#{$key}';
248247
$tokens: map.set($tokens, $key, $value);

0 commit comments

Comments
 (0)