@kadconsulting/dry
Version:
KAD Reusable Component Library
77 lines • 3.97 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useRef, useState } from 'react';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import ConfirmationDialog from './ConfirmationDialog';
describe('ConfirmationDialog', () => {
// Default props to satisfy required properties
const defaultProps = {
message: 'Are you sure?',
onConfirm: () => { },
onCancel: () => { },
};
it('renders the given message', () => {
render(_jsx(ConfirmationDialog, { ...defaultProps, message: 'Confirm action?' }));
expect(screen.getByText('Confirm action?')).toBeInTheDocument();
});
it('calls onConfirm when Confirm button is clicked', () => {
const onConfirmMock = jest.fn();
render(_jsx(ConfirmationDialog, { ...defaultProps, onConfirm: onConfirmMock }));
fireEvent.click(screen.getByText('Confirm'));
expect(onConfirmMock).toHaveBeenCalledTimes(1);
});
it('calls onCancel when Cancel button is clicked', () => {
const onCancelMock = jest.fn();
render(_jsx(ConfirmationDialog, { ...defaultProps, onCancel: onCancelMock }));
fireEvent.click(screen.getByText('Cancel'));
expect(onCancelMock).toHaveBeenCalledTimes(1);
});
// Existing tests for className, id, data-testid, etc., can remain unchanged.
it('renders with a dry-prepended className', () => {
const { container } = render(_jsx(ConfirmationDialog, { ...defaultProps }));
expect(container.firstChild).toHaveClass('dry-confirmation-dialog');
});
it('passes a ref to its outermost element', async () => {
const Wrapper = () => {
const ref = useRef(null);
const [refWasPassed, setRefWasPassed] = useState(false);
useEffect(() => {
setRefWasPassed(!!ref.current);
}, []);
return (_jsxs(_Fragment, { children: [_jsx(ConfirmationDialog, { ref: ref, ...defaultProps }), refWasPassed && _jsx("div", { children: "Ref was passed!" })] }));
};
render(_jsx(Wrapper, {}));
await waitFor(() => screen.getByText('Ref was passed!'));
});
it('passes a downstream id', () => {
const id = 'test-id';
const testId = 'test-subject';
render(_jsx(ConfirmationDialog, { ...defaultProps, "data-testid": testId, id: id }));
expect(screen.getByTestId(testId)).toHaveAttribute('id', id);
});
it('passes any downstream className(s)', () => {
const className = 'first second third';
const testId = 'test-subject';
render(_jsx(ConfirmationDialog, { ...defaultProps, "data-testid": testId, className: className }));
expect(screen.getByTestId(testId)).toHaveClass('first');
expect(screen.getByTestId(testId)).toHaveClass('second');
expect(screen.getByTestId(testId)).toHaveClass('third');
});
it('passes any downstream inline style(s)', () => {
const style = { color: 'red' };
const testId = 'test-subject';
render(_jsx(ConfirmationDialog, { ...defaultProps, "data-testid": testId, style: style }));
expect(screen.getByTestId(testId)).toHaveStyle('color: red');
});
it('passes any downstream data-* attribute(s)', () => {
const testId = 'test-subject';
const testValue = 'product-1234-abcd-5678-efgh';
render(_jsx(ConfirmationDialog, { ...defaultProps, "data-testid": testId, "data-product": testValue }));
expect(screen.getByTestId(testId)).toHaveAttribute('data-product', testValue);
});
it('supports downstream @testing-library `screen.getByTestId`', () => {
const testId = 'test-subject';
render(_jsx(ConfirmationDialog, { ...defaultProps, "data-testid": testId }));
expect(screen.getByTestId(testId)).toBeInTheDocument();
});
});
//# sourceMappingURL=ConfirmationDialog.test.js.map