@kadconsulting/dry
Version:
KAD Reusable Component Library
102 lines • 4.18 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
// CLI Version 1.0.1
// Component Version 1.0.0
import { useEffect, useRef, useState } from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import InfoText from './InfoText';
describe('InfoText', () => {
it('renders with a dry-prepended className', () => {
// ARRANGE
const { container } = render(_jsx(InfoText, { text: 'Test Info' }));
// ASSERT
expect(container.firstChild).toHaveClass('dry-infotext');
});
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(InfoText, { ref: ref, text: 'Test Info' }), refWasPassed && _jsx("div", { children: "Ref was passed!" })] }));
};
// ACT
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(InfoText, { "data-testid": testId, id: id, text: 'Test Info' }));
// 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(InfoText, { "data-testid": testId, className: className, text: 'Test Info' }));
// 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(InfoText, { "data-testid": testId, style: style, text: 'Test Info' }));
// 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(InfoText, { "data-testid": testId, "data-product": testValue, text: 'Test Info' }));
// ASSERT
expect(screen.getByTestId(testId)).toHaveAttribute('data-product', testValue);
});
it('supports downstream @testing-library `screen.getByTestId`', () => {
// ARRANGE
const testId = 'test-subject';
// ACT
render(_jsx(InfoText, { "data-testid": testId, text: 'Test Info' }));
// ASSERT
expect(screen.getByTestId(testId)).toBeInTheDocument();
});
it('renders the text prop correctly', () => {
// ARRANGE
const testText = 'This is a test info text';
// ACT
render(_jsx(InfoText, { text: testText }));
// ASSERT
expect(screen.getByText(testText)).toBeInTheDocument();
});
it('renders the Icon component', () => {
// ARRANGE
const testId = 'test-subject';
// ACT
render(_jsx(InfoText, { "data-testid": testId, text: 'Test Info' }));
// ASSERT
const infoTextElement = screen.getByTestId(testId);
expect(infoTextElement.querySelector('svg')).toBeInTheDocument();
});
it('applies passProps correctly', () => {
// ARRANGE
const testId = 'test-subject';
const passProps = { 'aria-label': 'Additional info' };
// ACT
render(_jsx(InfoText, { "data-testid": testId, text: 'Test Info', passProps: passProps }));
// ASSERT
expect(screen.getByTestId(testId)).toHaveAttribute('aria-label', 'Additional info');
});
});
//# sourceMappingURL=InfoText.test.js.map