import React, { createRef } from 'react'; import { render } from '@testing-library/react'; import { PolymorphicAs } from '@leafygreen-ui/polymorphic'; import BackLink from './BackLink'; import { BackLinkProps } from './BackLink.types'; const renderBackLink = ({ ref, ...props }: BackLinkProps) => { const utils = render( Back link , ); const link = utils.getByTestId('link'); return { ...utils, link }; }; describe('packages/typography', () => { describe('BackLink', () => { test('accepts a ref', () => { const ref = createRef(); const { container } = renderBackLink({ ref, href: 'http://mongodb.design', }); expect(ref.current).toBe(container.firstElementChild); }); test('renders name of link', () => { const { getByText } = renderBackLink({ href: 'http://mongodb.design', }); const text = getByText('Back link'); expect(text).toBeInTheDocument(); }); describe('inferred polymorphic behavior', () => { test('when the "as" prop is supplied its value is respected', () => { const { link } = renderBackLink({ as: 'div', /* @ts-expect-error - when "as" is supplied, "href" is ignored */ href: 'http://localhost:9001', }); expect(link.tagName.toLowerCase()).toBe('div'); }); test('renders as an anchor when an href prop is supplied', () => { const { link } = renderBackLink({ href: 'http://mongodb.design' }); expect(link.tagName.toLowerCase()).toBe('a'); }); test('renders as a span when no "as" or "href" is provided', () => { const { link } = renderBackLink({}); expect(link.tagName.toLowerCase()).toBe('span'); }); test('renders with wrapper component', () => { const Wrapper = (props: JSX.IntrinsicElements['a']) => { return ( wrapper component ); }; const { getByTestId, getByText } = render( Link , ); const link = getByTestId('link'); expect(link.getAttribute('href')).toBe('http://mongodb.design'); expect(link.tagName.toLowerCase()).toBe('a'); const text = getByText('wrapper component'); expect(text).toBeInTheDocument(); }); }); describe('TypeScript types are correct', () => { const WrapperComponent = (props: JSX.IntrinsicElements['button']) => { return