Skip to content

Commit ad4f0c7

Browse files
committed
Refactor Button component to prevent duplicate data-testid attributes on host and inner button
1 parent 9443fbd commit ad4f0c7

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

src/components/visual/button/button.tsx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { EventEmitter } from '@stencil/core';
2-
import { Component, Element, Event, h, Prop } from '@stencil/core';
2+
import { Component, 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,29 +10,16 @@ import type { ButtonSizeEnum, ButtonVariantEnum } from '../../../common/Button/b
1010
shadow: true,
1111
})
1212
export class Button {
13-
@Element() hostElement!: HTMLElement;
14-
1513
@Event() buttonClick: EventEmitter<MouseEvent>;
1614

1715
@Prop() class?: string = '';
18-
@Prop({ attribute: 'data-testid' }) dataTestId?: string = '';
16+
// Accept `data-testid` without reflecting it on the host element,
17+
// so only the inner `<button>` ends up with this attribute.
18+
@Prop({ attribute: 'data-testid', reflect: false }) dataTestId?: string = '';
1919
@Prop() disabled?: boolean = false;
2020
@Prop() size?: `${ButtonSizeEnum}` = 'large';
2121
@Prop() variant?: `${ButtonVariantEnum}` = 'primary';
2222

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-
3623
private handleClick = (event: MouseEvent) => {
3724
this.buttonClick.emit(event);
3825
};

0 commit comments

Comments
 (0)