Skip to content

Commit ccc0e7e

Browse files
committed
Add host element reference to Button component for data-testid management
1 parent 1e45906 commit ccc0e7e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/components/visual/button/button.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { EventEmitter } from '@stencil/core';
2-
import { Component, Event, h, Prop } from '@stencil/core';
2+
import { Component, Element, Event, h, Prop } from '@stencil/core';
33
import { Button as ButtonComponent } from 'common/Button/Button';
44

55
import type { ButtonSizeEnum, ButtonVariantEnum } from '../../../common/Button/button.types';
@@ -10,6 +10,8 @@ import type { ButtonSizeEnum, ButtonVariantEnum } from '../../../common/Button/b
1010
shadow: true,
1111
})
1212
export class Button {
13+
@Element() hostElement!: HTMLElement;
14+
1315
@Event() buttonClick: EventEmitter<MouseEvent>;
1416

1517
@Prop() class?: string = '';
@@ -18,6 +20,19 @@ export class Button {
1820
@Prop() size?: `${ButtonSizeEnum}` = 'large';
1921
@Prop() variant?: `${ButtonVariantEnum}` = 'primary';
2022

23+
/**
24+
* Stencil reflects `data-testid` to the host element based on the Prop decorator above.
25+
* That causes two DOM nodes with the same `data-testid`: the `<mvx-button>` host and
26+
* the inner `<button>`, which breaks Playwright strict mode. We keep using the
27+
* attribute to populate `dataTestId`, but remove it from the host after render so that
28+
* only the inner `<button>` keeps the `data-testid`.
29+
*/
30+
componentDidRender() {
31+
if (this.hostElement.hasAttribute('data-testid')) {
32+
this.hostElement.removeAttribute('data-testid');
33+
}
34+
}
35+
2136
private handleClick = (event: MouseEvent) => {
2237
this.buttonClick.emit(event);
2338
};

0 commit comments

Comments
 (0)