diff --git a/packages/core-components/src/components/WarningPanel/WarningPanel.test.tsx b/packages/core-components/src/components/WarningPanel/WarningPanel.test.tsx index 36b5372900..5a6f49b689 100644 --- a/packages/core-components/src/components/WarningPanel/WarningPanel.test.tsx +++ b/packages/core-components/src/components/WarningPanel/WarningPanel.test.tsx @@ -17,22 +17,39 @@ import React from 'react'; import { fireEvent, screen } from '@testing-library/react'; import { renderInTestApp } from '@backstage/test-utils'; -import { Typography } from '@material-ui/core'; +import { ThemeProvider, Typography } from '@material-ui/core'; -import { WarningPanel } from './WarningPanel'; +import { WarningPanel, WarningProps } from './WarningPanel'; +import { lightTheme } from '@backstage/theme'; -const propsTitle = { title: 'Mock title' }; -const propsTitleMessage = { title: 'Mock title', message: 'Some more info' }; -const propsMessage = { message: 'Some more info' }; +const propsTitle: WarningProps = { title: 'Mock title' }; +const propsTitleMessage: WarningProps = { + title: 'Mock title', + message: 'Some more info', +}; +const propsMessage: WarningProps = { message: 'Some more info' }; +const propsErrorMessage: WarningProps = { + severity: 'error', + title: 'Mock title', + message: 'Some more info', +}; describe('', () => { it('renders without exploding', async () => { - await renderInTestApp(); + await renderInTestApp( + + + , + ); expect(screen.getByText('Warning: Mock title')).toBeInTheDocument(); }); it('renders title', async () => { - await renderInTestApp(); + await renderInTestApp( + + + , + ); const expandIcon = await screen.getByText('Warning: Mock title'); fireEvent.click(expandIcon); expect(screen.getByText('Warning: Mock title')).toBeInTheDocument(); @@ -41,27 +58,43 @@ describe('', () => { it('renders title and children', async () => { await renderInTestApp( - - Java stacktrace - , + + + Java stacktrace + + , ); expect(screen.getByText('Java stacktrace')).toBeInTheDocument(); }); it('renders message', async () => { - await renderInTestApp(); + await renderInTestApp( + + + , + ); expect(screen.getByText('Warning')).toBeInTheDocument(); expect(screen.getByText('Some more info')).toBeInTheDocument(); }); it('renders title, message, and children', async () => { await renderInTestApp( - - Java stacktrace - , + + + Java stacktrace + + , ); expect(screen.getByText('Warning: Mock title')).toBeInTheDocument(); expect(screen.getByText('Some more info')).toBeInTheDocument(); expect(screen.getByText('Java stacktrace')).toBeInTheDocument(); }); + it('renders message using severity', async () => { + await renderInTestApp( + + + , + ); + expect(screen.getByText('Error: Mock title')).toBeInTheDocument(); + }); }); diff --git a/packages/core-components/src/components/WarningPanel/WarningPanel.tsx b/packages/core-components/src/components/WarningPanel/WarningPanel.tsx index 23bd825787..e6188172b7 100644 --- a/packages/core-components/src/components/WarningPanel/WarningPanel.tsx +++ b/packages/core-components/src/components/WarningPanel/WarningPanel.tsx @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - import { BackstageTheme } from '@backstage/theme'; import { Accordion, @@ -22,60 +21,61 @@ import { Grid, makeStyles, Typography, + darken, + lighten, } from '@material-ui/core'; import ErrorOutline from '@material-ui/icons/ErrorOutline'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import React from 'react'; const getWarningTextColor = ( - severity: Props['severity'], + severity: NonNullable, theme: BackstageTheme, ) => { - switch (severity) { - case 'error': - return theme.palette.errorText; - case 'info': - return theme.palette.infoText; - default: - return theme.palette.warningText; - } + const getColor = theme.palette.type === 'light' ? darken : lighten; + return getColor(theme.palette[severity].light, 0.6); }; const getWarningBackgroundColor = ( - severity: Props['severity'], + severity: NonNullable, theme: BackstageTheme, ) => { - switch (severity) { - case 'error': - return theme.palette.errorBackground; - case 'info': - return theme.palette.infoBackground; - default: - return theme.palette.warningBackground; - } + const getBackgroundColor = theme.palette.type === 'light' ? lighten : darken; + return getBackgroundColor(theme.palette[severity].light, 0.9); }; const useErrorOutlineStyles = makeStyles(theme => ({ root: { marginRight: theme.spacing(1), - fill: ({ severity }: Props) => getWarningTextColor(severity, theme), + fill: ({ severity }: WarningProps) => + getWarningTextColor( + severity as NonNullable, + theme, + ), }, })); -const ErrorOutlineStyled = ({ severity }: Pick) => { +const ErrorOutlineStyled = ({ severity }: Pick) => { const classes = useErrorOutlineStyles({ severity }); return ; }; -const ExpandMoreIconStyled = ({ severity }: Pick) => { +const ExpandMoreIconStyled = ({ severity }: Pick) => { const classes = useErrorOutlineStyles({ severity }); return ; }; const useStyles = makeStyles(theme => ({ panel: { - backgroundColor: ({ severity }: Props) => - getWarningBackgroundColor(severity, theme), - color: ({ severity }: Props) => getWarningTextColor(severity, theme), + backgroundColor: ({ severity }: WarningProps) => + getWarningBackgroundColor( + severity as NonNullable, + theme, + ), + color: ({ severity }: WarningProps) => + getWarningTextColor( + severity as NonNullable, + theme, + ), verticalAlign: 'middle', }, summary: { @@ -83,15 +83,26 @@ const useStyles = makeStyles(theme => ({ flexDirection: 'row', }, summaryText: { - color: ({ severity }: Props) => getWarningTextColor(severity, theme), + color: ({ severity }: WarningProps) => + getWarningTextColor( + severity as NonNullable, + theme, + ), fontWeight: 'bold', }, message: { width: '100%', display: 'block', - color: ({ severity }: Props) => getWarningTextColor(severity, theme), - backgroundColor: ({ severity }: Props) => - getWarningBackgroundColor(severity, theme), + color: ({ severity }: WarningProps) => + getWarningTextColor( + severity as NonNullable, + theme, + ), + backgroundColor: ({ severity }: WarningProps) => + getWarningBackgroundColor( + severity as NonNullable, + theme, + ), }, details: { width: '100%', @@ -104,7 +115,7 @@ const useStyles = makeStyles(theme => ({ }, })); -type Props = { +export type WarningProps = { title?: string; severity?: 'warning' | 'error' | 'info'; message?: React.ReactNode; @@ -126,15 +137,14 @@ const capitalize = (s: string) => { * @param {Object} [children] Objects to provide context, such as a stack trace or detailed error reporting. * Will be available inside an unfolded accordion. */ -export const WarningPanel = (props: Props) => { - const classes = useStyles(props); - const { - severity = 'warning', - title, - message, - children, - defaultExpanded, - } = props; +export const WarningPanel = ({ + severity = 'warning', + title, + message, + children, + defaultExpanded, +}: WarningProps) => { + const classes = useStyles({ severity }); // If no severity or title provided, the heading will read simply "Warning" const subTitle = capitalize(severity) + (title ? `: ${title}` : ''); diff --git a/plugins/explore/src/components/GroupsExplorerContent/GroupsExplorerContent.test.tsx b/plugins/explore/src/components/GroupsExplorerContent/GroupsExplorerContent.test.tsx index 6953cf57a2..7b3a73673e 100644 --- a/plugins/explore/src/components/GroupsExplorerContent/GroupsExplorerContent.test.tsx +++ b/plugins/explore/src/components/GroupsExplorerContent/GroupsExplorerContent.test.tsx @@ -100,7 +100,7 @@ describe('', () => { const catalogError = new Error('Network timeout'); catalogApi.getEntities.mockRejectedValueOnce(catalogError); - const { getByText } = await renderInTestApp( + const { getAllByText } = await renderInTestApp( , @@ -108,7 +108,7 @@ describe('', () => { ); await waitFor(() => - expect(getByText(/Warning: Network timeout/)).toBeInTheDocument(), + expect(getAllByText(/Error: Network timeout/).length).not.toBe(0), ); }); }); diff --git a/plugins/fossa/src/components/FossaCard/FossaCard.test.tsx b/plugins/fossa/src/components/FossaCard/FossaCard.test.tsx index 3c771bd7d6..79b401e4a8 100644 --- a/plugins/fossa/src/components/FossaCard/FossaCard.test.tsx +++ b/plugins/fossa/src/components/FossaCard/FossaCard.test.tsx @@ -74,7 +74,7 @@ describe('', () => { fossaApi.getFindingSummary.mockRejectedValue(new Error('My Error')); - const { getByText } = await renderInTestApp( + const { getAllByText } = await renderInTestApp( @@ -82,7 +82,7 @@ describe('', () => { , ); - expect(getByText(/Warning: My Error/i)).toBeInTheDocument(); + expect(getAllByText(/Error: My Error/i).length).not.toBe(0); }); it('shows empty', async () => {