2018-09-04 17:24:08 +02:00
|
|
|
import React from 'react';
|
2021-08-25 14:27:04 +01:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
|
import { BrowserRouter } from 'react-router-dom';
|
2021-09-22 14:56:15 +01:00
|
|
|
import DropdownChild from './DropdownChild';
|
2018-09-04 17:24:08 +02:00
|
|
|
|
2021-09-22 14:56:15 +01:00
|
|
|
describe('DropdownChild', () => {
|
2021-08-25 14:27:04 +01:00
|
|
|
const mockText = 'MyChildItem';
|
|
|
|
|
const mockUrl = '/route';
|
|
|
|
|
const mockIcon = 'home-alt';
|
2018-09-04 17:24:08 +02:00
|
|
|
|
2021-08-25 14:27:04 +01:00
|
|
|
it('displays the text', () => {
|
2021-09-22 14:56:15 +01:00
|
|
|
render(<DropdownChild text={mockText} />);
|
2021-08-25 14:27:04 +01:00
|
|
|
const text = screen.getByText(mockText);
|
|
|
|
|
expect(text).toBeInTheDocument();
|
|
|
|
|
});
|
2018-09-04 17:24:08 +02:00
|
|
|
|
2021-08-31 10:37:51 +01:00
|
|
|
it('attaches the url to the text if provided', () => {
|
2021-08-25 14:27:04 +01:00
|
|
|
render(
|
|
|
|
|
<BrowserRouter>
|
2021-09-22 14:56:15 +01:00
|
|
|
<DropdownChild text={mockText} url={mockUrl} />
|
2021-08-25 14:27:04 +01:00
|
|
|
</BrowserRouter>
|
|
|
|
|
);
|
|
|
|
|
const link = screen.getByRole('link', { name: mockText });
|
|
|
|
|
expect(link).toBeInTheDocument();
|
2021-08-31 10:37:51 +01:00
|
|
|
expect(link).toHaveAttribute('href', mockUrl);
|
2021-08-25 14:27:04 +01:00
|
|
|
});
|
2018-09-04 17:24:08 +02:00
|
|
|
|
2021-08-25 14:27:04 +01:00
|
|
|
it('displays an icon if a valid icon is provided', () => {
|
2021-09-22 14:56:15 +01:00
|
|
|
render(<DropdownChild text={mockText} icon={mockIcon} />);
|
2021-08-25 14:27:04 +01:00
|
|
|
const icon = screen.getByTestId('dropdown-child-icon');
|
|
|
|
|
expect(icon).toBeInTheDocument();
|
2018-09-04 17:24:08 +02:00
|
|
|
});
|
|
|
|
|
|
2021-10-01 15:53:56 +01:00
|
|
|
it('displays an external link icon if the target is _blank', () => {
|
|
|
|
|
render(<DropdownChild text={mockText} icon={mockIcon} url={mockUrl} target="_blank" />);
|
|
|
|
|
const icon = screen.getByTestId('external-link-icon');
|
|
|
|
|
expect(icon).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-25 14:27:04 +01:00
|
|
|
it('displays a divider instead when isDivider is true', () => {
|
2021-09-22 14:56:15 +01:00
|
|
|
render(<DropdownChild text={mockText} icon={mockIcon} url={mockUrl} isDivider />);
|
2021-08-25 14:27:04 +01:00
|
|
|
|
|
|
|
|
// Check the divider is shown
|
|
|
|
|
const divider = screen.getByTestId('dropdown-child-divider');
|
|
|
|
|
expect(divider).toBeInTheDocument();
|
2018-09-04 17:24:08 +02:00
|
|
|
|
2021-08-25 14:27:04 +01:00
|
|
|
// Check nothing else is rendered
|
|
|
|
|
const text = screen.queryByText(mockText);
|
|
|
|
|
const icon = screen.queryByTestId('dropdown-child-icon');
|
|
|
|
|
const link = screen.queryByRole('link', { name: mockText });
|
|
|
|
|
expect(text).not.toBeInTheDocument();
|
|
|
|
|
expect(icon).not.toBeInTheDocument();
|
|
|
|
|
expect(link).not.toBeInTheDocument();
|
2018-09-04 17:24:08 +02:00
|
|
|
});
|
|
|
|
|
});
|