@kadconsulting/dry
Version:
KAD Reusable Component Library
54 lines • 2.71 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import Link from './Link';
describe('Link', () => {
it('renders with correct href and text', () => {
render(_jsx(Link, { href: '/test', value: 'Test Link' }));
const linkElement = screen.getByText('Test Link');
expect(linkElement).toBeInTheDocument();
expect(linkElement).toHaveAttribute('href', '/test');
});
it('renders children when provided', () => {
render(_jsx(Link, { href: '/test', children: _jsx("span", { children: "Child Content" }) }));
expect(screen.getByText('Child Content')).toBeInTheDocument();
});
it('applies custom className', () => {
render(_jsx(Link, { href: '/test', value: 'Test Link', className: 'custom-class' }));
expect(screen.getByText('Test Link')).toHaveClass('custom-class');
});
it('passes additional props to the anchor element', () => {
render(_jsx(Link, { href: '/test', value: 'Test Link', target: '_blank', rel: 'noopener' }));
const linkElement = screen.getByText('Test Link');
expect(linkElement).toHaveAttribute('target', '_blank');
expect(linkElement).toHaveAttribute('rel', 'noopener');
});
it('passes a ref to its outermost element', async () => {
const Wrapper = () => {
const ref = React.useRef(null);
const [refWasPassed, setRefWasPassed] = React.useState(false);
React.useEffect(() => {
setRefWasPassed(!!ref.current);
}, []);
return (_jsxs(_Fragment, { children: [_jsx(Link, { ref: ref, href: '#', value: 'Test Link' }), refWasPassed && _jsx("div", { children: "Ref was passed!" })] }));
};
render(_jsx(Wrapper, {}));
await waitFor(() => screen.getByText('Ref was passed!'));
});
it('renders NextJS Link when in NextJS environment', () => {
const originalEnv = process.env.NEXT_PUBLIC_APP_ENV;
process.env.NEXT_PUBLIC_APP_ENV = 'nextjs';
const NextLinkMock = jest
.fn()
.mockImplementation(({ children }) => children);
jest.mock('next/link', () => ({ default: NextLinkMock }));
render(_jsx(Link, { href: '/test', value: 'NextJS Link' }));
expect(NextLinkMock).toHaveBeenCalledWith(expect.objectContaining({
href: '/test',
className: expect.stringContaining('dry-link'),
}), expect.anything());
process.env.NEXT_PUBLIC_APP_ENV = originalEnv;
});
});
//# sourceMappingURL=Link.test.js.map