I have been writing a test for my alert.js. I need to access prop value to test its severity. I tried lots of things. But failed every time.
import React from 'react';
import PropTypes from 'prop-types';
import { Snackbar } from '@material-ui/core';
import { Alert as MuiAlert } from '@material-ui/lab';
const Alert = (props) => {
return (
<Snackbar
open={props.open}
autoHideDuration={3000}
onClose={props.onClose}
data-testid='component-alert'
>
<MuiAlert
onClose={props.onClose}
severity={props.severity}
data-testid='alert-message'
>
{props.message}
</MuiAlert>
</Snackbar>
);
};
Alert.propTypes = {
message: PropTypes.string.isRequired,
severity: PropTypes.oneOf(['error', 'warning', 'info', 'success'])
.isRequired,
open: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
};
export default Alert;
import { render, cleanup } from '@testing-library/react'; import Alert from '../Alert';
const mockFunc = jest.fn(); const defaultProps = { message: 'abc', severity: 'error', open: true, onClose: mockFunc, };
test('render correct severity', () => {
render(<Alert {...defaultProps}/>)
//How to access severity prop value here
});
});
"error"
. – Sherris