summaryrefslogtreecommitdiff
path: root/src/components/inline-link.test.tsx
blob: ad9813d515c83ae4256502ec584615df44ebfc2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { render, screen } from '@testing-library/react';
import type { ReactNode } from 'react';
import { describe, expect, test, vi } from 'vitest';
import { InlineLink } from './inline-link';

vi.mock('next/link', () => ({
  default: ({
    href,
    children,
    ...props
  }: {
    href: string;
    children: ReactNode;
  }) => (
    <a href={href} {...props}>
      {children}
    </a>
  ),
}));

describe('InlineLink', () => {
  test('renders as a normal link by default', () => {
    render(<InlineLink href='/posts'>Posts</InlineLink>);

    const link = screen.getByRole('link', { name: 'Posts' });
    expect(link).toHaveAttribute('href', '/posts');
    expect(link).not.toHaveAttribute('target', '_blank');
  });

  test('sets target blank when blank prop is true', () => {
    render(
      <InlineLink href='https://example.com' blank>
        External
      </InlineLink>,
    );

    const link = screen.getByRole('link', { name: 'External' });
    expect(link).toHaveAttribute('href', 'https://example.com');
    expect(link).toHaveAttribute('target', '_blank');
  });
});