import { FunctionComponent } from 'react'; import { render, screen, within } from '@testing-library/react'; import { ThemeProvider } from '../../../theme'; import AppButton, { AppButtonProps } from './AppButton'; import DefaultIcon from '@mui/icons-material/MoreHoriz'; import { randomText, capitalize } from '@/utils'; /** * AppButton wrapped with Theme Provider */ const ComponentToTest: FunctionComponent = (props) => ( ); /** * Test specific color for AppButton * @param {string} colorName - name of the color, one of ColorName type * @param {string} [expectedClassName] - optional value to be found in className (color "true" may use "success" class name) * @param {boolean} [ignoreClassName] - optional flag to ignore className (color "inherit" doesn't use any class name) */ function testButtonColor(colorName: string, ignoreClassName = false, expectedClassName = colorName) { it(`supports "${colorName}" color`, () => { const testId = randomText(8); let text = `${colorName} button`; render( {text} ); let button = screen.getByTestId(testId); expect(button).toBeDefined(); // console.log('button.className:', button?.className); if (!ignoreClassName) { expect(button?.className?.includes('MuiButton-root')).toBeTruthy(); expect(button?.className?.includes('MuiButton-contained')).toBeTruthy(); expect(button?.className?.includes(`MuiButton-contained${capitalize(expectedClassName)}`)).toBeTruthy(); // Check for "MuiButton-contained[Primary| Secondary |...]" class } }); } describe(' component', () => { // beforeEach(() => {}); it('renders itself', () => { let text = 'sample button'; const testId = randomText(8); render({text}); const button = screen.getByTestId(testId); expect(button).toBeDefined(); expect(button).toHaveAttribute('role', 'button'); expect(button).toHaveAttribute('type', 'button'); // not "submit" or "input" by default }); it('has .margin style by default', () => { let text = 'button with default margin'; const testId = randomText(8); render({text}); const button = screen.getByTestId(testId); expect(button).toBeDefined(); expect(button).toHaveStyle('margin: 8px'); // Actually it is theme.spacing(1) value }); it('supports .className property', () => { let text = 'button with specific class'; let className = 'someClassName'; const testId = randomText(8); render( {text} ); const button = screen.getByTestId(testId); expect(button).toBeDefined(); expect(button).toHaveClass(className); }); it('supports .label property', () => { let text = 'button with label'; render(); let span = screen.getByText(text); expect(span).toBeDefined(); let button = span.closest('button'); // parent