Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Rows, Cards, FeedbackScreen, FormFields, Buttons): add test ids for components and their internal elements #1270

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/__private_stories__/image-sizes-story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const Default: StoryComponent<Args> = ({forceNonCssAspectRatio}) => {
);

return (
<Stack space={16} dataAttributes={{testId: 'story'}}>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated, but camelCase is not correct here

<Stack space={16} dataAttributes={{testid: 'story'}}>
<div style={{width: 400, border: '2px solid pink'}}>
<Stack space={16}>{cases.map((props, index) => renderCase(props, index))}</Stack>
</div>
Expand Down
175 changes: 175 additions & 0 deletions src/__tests__/testid-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import * as React from 'react';
import {RowList, Row} from '../list';
import {screen, render, within} from '@testing-library/react';
import {
ButtonPrimary,
ButtonSecondary,
DataCard,
DateField,
IconShopRegular,
Placeholder,
SearchField,
Stack,
SuccessFeedbackScreen,
TextField,
ThemeContextProvider,
} from '..';
import {makeTheme} from './test-utils';

const checkTestIds = (
content: React.ReactElement,
elements: ReadonlyArray<{componentName: string; internalTestIds: ReadonlyArray<string>}>
) => {
render(<ThemeContextProvider theme={makeTheme()}>{content}</ThemeContextProvider>);

elements.forEach(({componentName, internalTestIds}) => {
const element = screen.getByTestId(componentName);
expect(element).toBeInTheDocument();

internalTestIds.forEach((id) => expect(within(element).getByTestId(id)).toBeInTheDocument());
});
};

test('Row test ids', () => {
checkTestIds(
<RowList>
<Row
title="Title"
headline="Headline"
subtitle="Subtitle"
description="Description"
detail="Detail"
asset={<IconShopRegular />}
extra={<Placeholder />}
right="right"
href="#"
/>
</RowList>,
[
{
componentName: 'Row',
internalTestIds: [
'headline',
'title',
'subtitle',
'description',
'detail',
'asset',
'slot',
'chevron',
'endSlot',
],
},
]
);
});

test('Cards test ids', () => {
checkTestIds(
<DataCard
headline="Headline"
pretitle="Pretitle"
title="Title"
subtitle="Subtitle"
description="Description"
extra={<Placeholder />}
asset={<IconShopRegular />}
actions={[
{
Icon: IconShopRegular,
onPress: () => {},
label: 'Lightning',
},
]}
/>,
[
{
componentName: 'DataCard',
internalTestIds: [
'headline',
'pretitle',
'title',
'subtitle',
'description',
'asset',
'topActions',
'slot',
],
},
]
);
});

test('FeedbackScreen test ids', () => {
checkTestIds(
<SuccessFeedbackScreen
title="Title"
description="Description"
extra={<Placeholder />}
primaryButton={<ButtonPrimary onPress={() => {}}>Action</ButtonPrimary>}
imageUrl="https://picsum.photos/1200/1200"
/>,
[
{
componentName: 'SuccessFeedbackScreen',
internalTestIds: ['icon', 'title', 'description', 'image', 'slot'],
},
]
);
});

test('InputFields test ids', () => {
checkTestIds(
<Stack space={16}>
<TextField
name="textField"
label="Label 1"
helperText="Helper"
multiline
maxLength={200}
endIcon={<IconShopRegular />}
/>
<SearchField name="searchField" label="Label 2" helperText="helperText" error />
<DateField name="dateField" label="Label 3" />
</Stack>,

[
{
componentName: 'TextField',
internalTestIds: ['label', 'helperText', 'endHelperText', 'endIcon'],
},
{
componentName: 'SearchField',
internalTestIds: ['startIcon', 'errorText'],
},
// In DateField/MonthField/DatetimeField, we use endIconOverlay for setting the endIcon
{
componentName: 'DateField',
internalTestIds: ['endIcon'],
},
]
);
});

test('Buttons test ids', () => {
checkTestIds(
<Stack space={16}>
<ButtonPrimary StartIcon={IconShopRegular} EndIcon={IconShopRegular} onPress={() => {}}>
Action
</ButtonPrimary>
<ButtonSecondary loadingText="Loading text" showSpinner onPress={() => {}}>
Action 2
</ButtonSecondary>
</Stack>,
[
{
componentName: 'ButtonPrimary',
internalTestIds: ['startIcon', 'endIcon'],
},
{
componentName: 'ButtonSecondary',
internalTestIds: ['loadingText'],
},
]
);
});
3 changes: 2 additions & 1 deletion src/boxed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const InternalBoxed = React.forwardRef<HTMLDivElement, Props & InternalPr
(
{
children,
isInverse: isInverseInside = false,
isInverse = false,
className,
role,
dataAttributes,
Expand All @@ -90,6 +90,7 @@ export const InternalBoxed = React.forwardRef<HTMLDivElement, Props & InternalPr
ref
) => {
const isInverseOutside = useIsInverseOrMediaVariant();
const isInverseInside = isInverse || variant === 'inverse' || variant === 'media';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but Boxed was not working properly when using media variant


return (
<div
Expand Down
30 changes: 24 additions & 6 deletions src/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const renderButtonContent = ({
alignItems: 'center',
marginRight: styles.iconMargin,
}}
data-testid="startIcon"
>
<StartIcon size={defaultIconSize} color="currentColor" />
</div>
Expand All @@ -201,6 +202,7 @@ const renderButtonContent = ({
alignItems: 'center',
marginLeft: styles.iconMargin,
}}
data-testid="endIcon"
>
<EndIcon size={defaultIconSize} color="currentColor" />
</div>
Expand Down Expand Up @@ -249,7 +251,11 @@ const renderButtonContent = ({
}}
/>
)}
{loadingText ? <Box paddingLeft={8}>{loadingButtonElement}</Box> : null}
{loadingText ? (
<Box paddingLeft={8} dataAttributes={{testid: 'loadingText'}}>
{loadingButtonElement}
</Box>
) : null}
</div>
</>
);
Expand Down Expand Up @@ -513,7 +519,7 @@ export const ButtonLink = React.forwardRef<
>(({dataAttributes, className, ...props}, ref) => {
return (
<BaseButton
dataAttributes={{'component-name': 'ButtonLink', ...dataAttributes}}
dataAttributes={{'component-name': 'ButtonLink', testid: 'ButtonLink', ...dataAttributes}}
className={classnames(className, {[styles.smallLink]: props.small})}
{...props}
ref={ref}
Expand All @@ -526,7 +532,11 @@ export const ButtonLinkDanger = React.forwardRef<TouchableElement, ButtonLinkPro
({dataAttributes, className, ...props}, ref) => {
return (
<BaseButton
dataAttributes={{'component-name': 'ButtonLinkDanger', ...dataAttributes}}
dataAttributes={{
'component-name': 'ButtonLinkDanger',
testid: 'ButtonLinkDanger',
...dataAttributes,
}}
className={classnames(className, {[styles.smallLink]: props.small})}
{...props}
withChevron={false}
Expand All @@ -541,7 +551,11 @@ export const ButtonPrimary = React.forwardRef<TouchableElement, ButtonProps>(
({dataAttributes, ...props}, ref) => {
return (
<BaseButton
dataAttributes={{'component-name': 'ButtonPrimary', ...dataAttributes}}
dataAttributes={{
'component-name': 'ButtonPrimary',
testid: 'ButtonPrimary',
...dataAttributes,
}}
{...props}
ref={ref}
buttonType="primary"
Expand All @@ -554,7 +568,11 @@ export const ButtonSecondary = React.forwardRef<TouchableElement, ButtonProps>(
({dataAttributes, ...props}, ref) => {
return (
<BaseButton
dataAttributes={{'component-name': 'ButtonSecondary', ...dataAttributes}}
dataAttributes={{
'component-name': 'ButtonSecondary',
testid: 'ButtonSecondary',
...dataAttributes,
}}
{...props}
ref={ref}
buttonType="secondary"
Expand All @@ -567,7 +585,7 @@ export const ButtonDanger = React.forwardRef<TouchableElement, ButtonProps>(
({dataAttributes, ...props}, ref) => {
return (
<BaseButton
dataAttributes={{'component-name': 'ButtonDanger', ...dataAttributes}}
dataAttributes={{'component-name': 'ButtonDanger', testid: 'ButtonDanger', ...dataAttributes}}
{...props}
ref={ref}
buttonType="danger"
Expand Down
Loading
Loading