@kadconsulting/dry
Version:
KAD Reusable Component Library
99 lines • 4.27 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
// Version 1.0.0
import { useEffect, useRef, useState } from 'react';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import ProgressNavigation from './ProgressNavigation';
const defaultSteps = [
{ id: 1, title: 'Step 1', text: 'Description of step 1' },
{ id: 2, title: 'Step 2', text: 'Description of step 2' },
{ id: 3, title: 'Step 3', text: 'Description of step 3' },
];
describe('ProgressNavigation', () => {
it('renders with a dry-prepended className', () => {
// ARRANGE
const { container } = render(_jsx(ProgressNavigation, { steps: defaultSteps }));
// ASSERT
expect(container.firstChild).toHaveClass('dry-progressnavigation');
});
it('passes a ref to its outermost element', async () => {
// ARRANGE
const Wrapper = () => {
const ref = useRef(null);
const [refWasPassed, setRefWasPassed] = useState(false);
useEffect(() => {
setRefWasPassed(!!ref.current);
}, []);
return (_jsxs(_Fragment, { children: [_jsx(ProgressNavigation, { ref: ref, steps: defaultSteps }), refWasPassed && _jsx("div", { children: "Ref was passed!" })] }));
};
render(_jsx(Wrapper, {}));
// ASSERT
await waitFor(() => screen.getByText('Ref was passed!'));
});
it('passes a downstream id', () => {
// ARRANGE
const id = 'test-id';
const testId = 'test-subject';
// ACT
render(_jsx(ProgressNavigation, { "data-testid": testId, id: id, steps: defaultSteps }));
// ASSERT
expect(screen.getByTestId(testId)).toHaveAttribute('id', id);
});
it('passes any downstream className(s)', () => {
// ARRANGE
const className = 'first second third';
const testId = 'test-subject';
// ACT
render(_jsx(ProgressNavigation, { "data-testid": testId, className: className, steps: defaultSteps }));
// ASSERT
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)', () => {
// ARRANGE
const style = { color: 'red' };
const testId = 'test-subject';
// ACT
render(_jsx(ProgressNavigation, { "data-testid": testId, style: style, steps: defaultSteps }));
// ASSERT
expect(screen.getByTestId(testId)).toHaveStyle('color: red');
});
it('passes any downstream data-* attribute(s)', () => {
// ARRANGE
const testId = 'test-subject';
const testValue = 'product-1234-abcd-5678-efgh';
// ACT
render(_jsx(ProgressNavigation, { "data-testid": testId, "data-product": testValue, steps: defaultSteps }));
// ASSERT
expect(screen.getByTestId(testId)).toHaveAttribute('data-product', testValue);
});
it('supports downstream @testing-library `screen.getByTestId`', () => {
// ARRANGE
const testId = 'test-subject';
// ACT
render(_jsx(ProgressNavigation, { "data-testid": testId, steps: defaultSteps }));
// ASSERT
expect(screen.getByTestId(testId)).toBeInTheDocument();
});
it('renders the correct number of steps', () => {
// ARRANGE
const testId = 'test-subject';
// ACT
render(_jsx(ProgressNavigation, { "data-testid": testId, steps: defaultSteps }));
// ASSERT
const steps = screen.getAllByRole('button');
expect(steps.length).toBe(defaultSteps.length);
});
it('calls onStepClick when a step is clicked', () => {
// ARRANGE
const testId = 'test-subject';
const onStepClick = jest.fn();
// ACT
render(_jsx(ProgressNavigation, { "data-testid": testId, steps: defaultSteps, onStepClick: onStepClick }));
const step = screen.getAllByRole('button')[1];
fireEvent.click(step);
// ASSERT
expect(onStepClick).toHaveBeenCalledWith(1);
});
});
//# sourceMappingURL=ProgressNavigation.test.js.map