Skip to content

Commit ee7ac59

Browse files
fix: Ensure toggle button test utils only find toggle buttons (#3868)
1 parent df8cc49 commit ee7ac59

File tree

15 files changed

+109
-25
lines changed

15 files changed

+109
-25
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import React from 'react';
4+
5+
import Button from '~components/button';
6+
7+
import ScreenshotArea from '../utils/screenshot-area';
8+
9+
export default function ButtonsScenario() {
10+
return (
11+
<article>
12+
<h1>Legacy buttons</h1>
13+
<ScreenshotArea>
14+
<button className="awsui_button_vjswe_t8nlg_157">Legacy button</button>
15+
<button className="awsui_button_vjswe_1qz6m_157">Older legacy button</button>
16+
<Button>Button</Button>
17+
</ScreenshotArea>
18+
</article>
19+
);
20+
}

src/__tests__/snapshot-tests/__snapshots__/test-utils-selectors.test.tsx.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ exports[`test-utils selectors 1`] = `
8484
"awsui_item_d19fg",
8585
],
8686
"button": [
87-
"awsui_button_vjswe",
8887
"awsui_content_vjswe",
8988
"awsui_disabled-reason-tooltip_1ueyk",
9089
"awsui_icon-left_vjswe",
90+
"awsui_root_1ueyk",
9191
],
9292
"button-dropdown": [
9393
"awsui_button-dropdown_sne0l",
@@ -652,6 +652,9 @@ exports[`test-utils selectors 1`] = `
652652
"toggle": [
653653
"awsui_root_4yi2u",
654654
],
655+
"toggle-button": [
656+
"awsui_root_1qd09",
657+
],
655658
"token": [
656659
"awsui_dismiss-button_1epxo",
657660
"awsui_root_1epxo",

src/button/__integ__/button.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,15 @@ describe('Button Style API', () => {
6969
})
7070
);
7171
});
72+
73+
describe('legacy selectors', () => {
74+
test(
75+
'finds legacy and new buttons',
76+
useBrowser(async browser => {
77+
await browser.url('#/light/button/legacy-button');
78+
const page = new ButtonPageObject(browser);
79+
await expect(page.getText(wrapper.findButton().toSelector())).resolves.toBe('Legacy button');
80+
await expect(browser.$$(wrapper.findAllButtons().toSelector())).resolves.toHaveLength(3);
81+
})
82+
);
83+
});

src/button/__tests__/button.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,4 +926,15 @@ describe('Style API', () => {
926926
'0 0 8px green'
927927
);
928928
});
929+
930+
test('finds legacy buttons', () => {
931+
const renderResult = render(
932+
<div>
933+
<Button>Button</Button>
934+
<button className="awsui_button_vjswe_t8nlg_157">Legacy button</button>
935+
</div>
936+
);
937+
const wrapper = createWrapper(renderResult.container);
938+
expect(wrapper.findAllButtons()).toHaveLength(2);
939+
});
929940
});

src/button/__tests__/native-attributes.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('Button native attributes', () => {
7575
const wrapper = createWrapper(container).findButton()!;
7676

7777
expect(wrapper.getElement()).toHaveClass('my-additional-class');
78-
expect(wrapper.getElement()).toHaveClass(styles.button);
78+
expect(wrapper.getElement()).toHaveClass(styles.root);
7979
});
8080

8181
test('events get chained', () => {

src/button/internal.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export type InternalButtonProps = Omit<ButtonProps, 'variant'> & {
5656
__title?: string;
5757
__emitPerformanceMarks?: boolean;
5858
__skipNativeAttributesWarnings?: boolean;
59+
__hideFromTestUtils?: boolean;
5960
} & InternalBaseComponentProps;
6061

6162
export const InternalButton = React.forwardRef(
@@ -98,6 +99,7 @@ export const InternalButton = React.forwardRef(
9899
__title,
99100
__emitPerformanceMarks = true,
100101
__skipNativeAttributesWarnings,
102+
__hideFromTestUtils = false,
101103
analyticsAction = 'click',
102104
...props
103105
}: InternalButtonProps,
@@ -178,7 +180,8 @@ export const InternalButton = React.forwardRef(
178180
buttonContext.onClick({ variant });
179181
};
180182

181-
const buttonClass = clsx(props.className, styles.button, styles[`variant-${variant}`], {
183+
const buttonClass = clsx(props.className, styles.root, styles[`variant-${variant}`], {
184+
[testUtilStyles.root]: !__hideFromTestUtils,
182185
[styles.disabled]: isNotInteractive,
183186
[styles['disabled-with-reason']]: isDisabledWithReason,
184187
[styles['button-no-wrap']]: !wrapText,

src/button/styles.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
/* used in test-utils */
5454
}
5555

56-
.button {
56+
.root {
5757
@include styles.styles-reset;
5858
@include styles.text-wrapping;
5959
@include styles.font-button;

src/button/test-classes/styles.scss

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
.disabled-reason-tooltip {
7-
/* used in test-utils or tests */
8-
}
9-
10-
.external-icon {
6+
.disabled-reason-tooltip,
7+
.external-icon,
8+
.root {
119
/* used in test-utils or tests */
1210
}

src/table/internal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ const InternalTable = React.forwardRef(
201201
?.innerText ?? toolsHeaderPerformanceMarkRef.current?.innerText;
202202
const getPatternIdentifier = () => {
203203
const hasActions = !!toolsHeaderPerformanceMarkRef.current?.querySelector<HTMLElement>(
204-
`.${headerStyles.actions} .${buttonStyles.button}`
204+
`.${headerStyles.actions} .${buttonStyles.root}`
205205
);
206206

207207
if (hasActions) {

src/test-utils/dom/button-dropdown/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { act } from '@cloudscape-design/test-utils-core/utils-dom';
55

66
import ButtonWrapper from '../button/index.js';
77

8-
import buttonStyles from '../../../button/styles.selectors.js';
98
import categoryStyles from '../../../button-dropdown/category-elements/styles.selectors.js';
109
import itemStyles from '../../../button-dropdown/item-element/styles.selectors.js';
1110
import styles from '../../../button-dropdown/styles.selectors.js';
@@ -21,15 +20,11 @@ export default class ButtonDropdownWrapper extends ComponentWrapper {
2120
}
2221

2322
findTriggerButton(): ButtonWrapper | null {
24-
return (
25-
this.findByClassName(styles['dropdown-trigger'])?.findComponent(`.${buttonStyles.button}`, ButtonWrapper) ?? null
26-
);
23+
return this.findByClassName(styles['dropdown-trigger'])?.findButton() ?? null;
2724
}
2825

2926
findMainAction(): null | ButtonWrapper {
30-
return (
31-
this.findByClassName(styles['split-trigger'])?.findComponent(`.${buttonStyles.button}`, ButtonWrapper) ?? null
32-
);
27+
return this.findByClassName(styles['split-trigger'])?.findButton() ?? null;
3328
}
3429

3530
findOpenDropdown(): ElementWrapper | null {

0 commit comments

Comments
 (0)