Skip to content

Commit

Permalink
Add role prop to Text and Message
Browse files Browse the repository at this point in the history
  • Loading branch information
moroshko committed Jan 22, 2021
1 parent 94d7d20 commit 16fb797
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/components/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ function Message(props) {
title: (title) => typeof title === "string" && title.length > 0,
}
);
const { severity, bg, title, callToAction, children, testId } = mergedProps;
const {
severity,
bg,
title,
callToAction,
role,
children,
testId,
} = mergedProps;
const theme = useTheme();
const textAndIconColor = [
"highlight.pink.t100",
Expand Down Expand Up @@ -134,7 +142,7 @@ function Message(props) {
}),
}}
>
<Text color={textAndIconColor}>
<Text color={textAndIconColor} role={role}>
{title && (
<strong
css={{ display: "block", marginBottom: theme.space[1] }}
Expand Down Expand Up @@ -173,6 +181,7 @@ Message.propTypes = {
title: PropTypes.string,
...responsivePropType("hasBreakpointWidth", PropTypes.bool),
...responsivePaddingType,
role: PropTypes.string,
children: PropTypes.node.isRequired,
testId: PropTypes.string,
};
Expand Down
13 changes: 13 additions & 0 deletions src/components/Message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ describe("Message", () => {
});
});

it("with role", () => {
render(
<Message severity="blocking" bg="secondary.pink.t30" role="alert">
Please fix the errors above.
</Message>
);

expect(screen.getByText("Please fix the errors above.")).toHaveAttribute(
"role",
"alert"
);
});

it("with testId", () => {
const { container } = render(
<Message severity="info-or-minor" testId="my-message">
Expand Down
5 changes: 3 additions & 2 deletions src/components/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function Text(props) {
align: (align) => ALIGNS.includes(align),
wrap: (wrap) => typeof wrap === "boolean",
});
const { id, as, align, wrap, children, testId } = mergedProps;
const { id, as, align, wrap, role, children, testId } = mergedProps;
const css = useResponsivePropsCSS(mergedProps, DEFAULT_PROPS, {
color: (_, theme, bp) => {
const color =
Expand All @@ -115,7 +115,7 @@ function Text(props) {
const Component = as;

return (
<Component id={id} css={css} data-testid={testId}>
<Component id={id} css={css} role={role} data-testid={testId}>
{children}
</Component>
);
Expand All @@ -142,6 +142,7 @@ Text.propTypes = {
},
align: PropTypes.oneOf(ALIGNS),
wrap: PropTypes.bool,
role: PropTypes.string,
children: PropTypes.node,
testId: PropTypes.string,
};
Expand Down
8 changes: 8 additions & 0 deletions src/components/Text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ describe("Text", () => {
});
});

it("with role", () => {
render(<Text role="alert">Hello World</Text>);

const node = screen.getByText("Hello World");

expect(node).toHaveAttribute("role", "alert");
});

it("with testId", () => {
const { container } = render(<Text testId="my-text">Hello World</Text>);

Expand Down

1 comment on commit 16fb797

@vercel
Copy link

@vercel vercel bot commented on 16fb797 Jan 22, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.