import { expect } from 'chai'; import { createRenderer, screen } from '@mui/internal-test-utils'; import { ThemeProvider, createTheme } from '@mui/material/styles'; import Alert, { alertClasses as classes } from '@mui/material/Alert'; import Paper, { paperClasses } from '@mui/material/Paper'; import { iconButtonClasses } from '@mui/material/IconButton'; import { svgIconClasses } from '@mui/material/SvgIcon'; import describeConformance from '../../test/describeConformance'; import capitalize from '../utils/capitalize'; describe('', () => { const { render } = createRenderer(); describeConformance( {}} />, () => ({ classes, inheritComponent: Paper, render, refInstanceof: window.HTMLDivElement, muiName: 'MuiAlert', testVariantProps: { variant: 'standard', color: 'success' }, testDeepOverrides: { slotName: 'message', slotClassName: classes.message }, testLegacyComponentsProp: ['closeButton', 'closeIcon'], slots: { root: { expectedClassName: classes.root, }, icon: { expectedClassName: classes.icon, }, message: { expectedClassName: classes.message, }, action: { expectedClassName: classes.action, }, closeButton: { expectedClassName: classes.closeButton, }, closeIcon: { expectedClassName: classes.closeIcon, }, }, skip: ['componentsProp'], })); describe('prop: square', () => { it('should have rounded corners by default', () => { render(Hello World); expect(screen.getByTestId('root')).to.have.class(paperClasses.rounded); }); it('should disable rounded corners with square prop', () => { render( Hello World , ); expect(screen.getByTestId('root')).not.to.have.class(paperClasses.rounded); }); }); describe('prop: action', () => { it('using ownerState in styleOverrides should not throw', () => { const theme = createTheme({ components: { MuiAlert: { styleOverrides: { root: (props) => { return { ...(props.ownerState.variant === 'filled' && { border: '1px red solid', }), }; }, }, }, }, }); expect(() => render( Action}>Alert , ), ).not.to.throw(); }); it('should render the action provided into the Alert', () => { render(Action}>Hello World); expect(screen.getByTestId('action')).toBeVisible(); }); }); describe('prop: components', () => { it('should override the default icon used in the close action', () => { function MyCloseIcon() { return
X
; } render( {}} components={{ CloseIcon: MyCloseIcon }}> Hello World , ); expect(screen.getByTestId('closeIcon')).toBeVisible(); }); it('should override the default button used in the close action', () => { function MyCloseButton() { return ; } render( {}} components={{ CloseButton: MyCloseButton }}> Hello World , ); expect(screen.getByTestId('closeButton')).toBeVisible(); }); }); describe('prop: componentsProps', () => { it('should apply the props on the close IconButton component', () => { render( {}} componentsProps={{ closeButton: { 'data-testid': 'closeButton', size: 'large', className: 'my-class', }, }} > Hello World , ); const closeIcon = screen.getByTestId('closeButton'); expect(closeIcon).to.have.class(iconButtonClasses.sizeLarge); expect(closeIcon).to.have.class('my-class'); }); it('should apply the props on the close SvgIcon component', () => { render( {}} componentsProps={{ closeIcon: { 'data-testid': 'closeIcon', fontSize: 'large', className: 'my-class', }, }} > Hello World , ); const closeIcon = screen.getByTestId('closeIcon'); expect(closeIcon).to.have.class(svgIconClasses.fontSizeLarge); expect(closeIcon).to.have.class('my-class'); }); }); describe('prop: icon', () => { it('should render the icon provided into the Alert', () => { render(}>Hello World); expect(screen.getByTestId('icon')).toBeVisible(); }); it('should not render any icon if false is provided', () => { render( }} > Hello World , ); expect(screen.queryByTestId('success-icon')).to.eq(null); }); }); describe('prop: iconMapping', () => { const severities = ['success', 'info', 'warning', 'error']; const iconMapping = severities.reduce((acc, severity) => { acc[severity] =
; return acc; }, {}); severities.forEach((severity) => { it(`should render the icon provided into the Alert for severity ${severity}`, () => { render( Hello World , ); expect(screen.getByTestId(`${severity}-icon`)).toBeVisible(); }); }); }); describe('classes', () => { it('should apply default color class to the root', () => { render(); expect(screen.getByTestId('alert')).to.have.class(classes.colorSuccess); }); ['success', 'info', 'warning', 'error'].forEach((color) => { it('should apply color classes to the root', () => { render(); expect(screen.getByTestId('alert')).to.have.class(classes[`color${capitalize(color)}`]); }); }); }); });