-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathButtonOrLink.test.jsx
36 lines (29 loc) · 1.19 KB
/
ButtonOrLink.test.jsx
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
import React from 'react';
import { render, screen, fireEvent, waitFor, history } from '../test-utils';
import ButtonOrLink from './ButtonOrLink';
describe('ButtonOrLink', () => {
const clickHandler = jest.fn();
afterEach(() => {
clickHandler.mockClear();
});
it('can render a clickable button', () => {
render(<ButtonOrLink onClick={clickHandler}>Text</ButtonOrLink>);
const button = screen.getByRole('button');
expect(button).toBeInstanceOf(HTMLButtonElement);
expect(button).toContainHTML('<button aria-disabled="false">Text</button>');
fireEvent.click(button);
expect(clickHandler).toHaveBeenCalled();
});
it('can render an external link', () => {
render(<ButtonOrLink href="https://p5js.org">p5</ButtonOrLink>);
const link = screen.getByRole('link');
expect(link).toBeInstanceOf(HTMLAnchorElement);
expect(link).toHaveAttribute('href', 'https://p5js.org');
});
it('can render an internal link with react-router', async () => {
render(<ButtonOrLink href="/about">About</ButtonOrLink>);
const link = screen.getByText('About');
fireEvent.click(link);
await waitFor(() => expect(history.location.pathname).toEqual('/about'));
});
});