UNPKG

@kadconsulting/dry

Version:
102 lines 4.25 kB
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 ImageCarousel from './ImageCarousel'; const mockImages = [ { attributes: { url: 'image1.jpg', alternativeText: 'Image 1' } }, { attributes: { url: 'image2.jpg', alternativeText: 'Image 2' } }, ]; describe('ImageCarousel', () => { it('renders with a dry-prepended className', () => { // ARRANGE const { container } = render(_jsx(ImageCarousel, { images: mockImages })); // ASSERT expect(container.firstChild).toHaveClass('dry-imagecarousel'); }); 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(ImageCarousel, { ref: ref, images: mockImages }), 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(ImageCarousel, { "data-testid": testId, id: id, images: mockImages })); // 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(ImageCarousel, { "data-testid": testId, className: className, images: mockImages })); // 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(ImageCarousel, { "data-testid": testId, style: style, images: mockImages })); // 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(ImageCarousel, { "data-testid": testId, "data-product": testValue, images: mockImages })); // ASSERT expect(screen.getByTestId(testId)).toHaveAttribute('data-product', testValue); }); it('supports downstream @testing-library `screen.getByTestId`', () => { // ARRANGE const testId = 'test-subject'; // ACT render(_jsx(ImageCarousel, { "data-testid": testId, images: mockImages })); // ASSERT expect(screen.getByTestId(testId)).toBeInTheDocument(); }); it('renders images passed as props', () => { // ARRANGE const testId = 'test-subject'; // ACT render(_jsx(ImageCarousel, { "data-testid": testId, images: mockImages })); // ASSERT mockImages.forEach((image) => { expect(screen.getByAltText(image.attributes.alternativeText)).toBeInTheDocument(); }); }); it('renders Carousel component with correct props', () => { // ARRANGE const testId = 'test-subject'; // ACT render(_jsx(ImageCarousel, { "data-testid": testId, images: mockImages })); // ASSERT const carouselContainer = screen .getByTestId(testId) .querySelector('.carousel'); expect(carouselContainer).toBeInTheDocument(); expect(carouselContainer).toHaveClass('carouselContainer'); }); }); //# sourceMappingURL=ImageCarousel.test.js.map