2023-06-09 10:00:16 -05:00
|
|
|
import { render as rtlRender, screen, within } from '@testing-library/react';
|
2023-04-27 05:43:20 -05:00
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
import React from 'react';
|
2023-06-09 10:00:16 -05:00
|
|
|
import { TestProvider } from 'test/helpers/TestProvider';
|
2023-04-27 05:43:20 -05:00
|
|
|
|
2023-06-09 10:00:16 -05:00
|
|
|
import CreateNewButton from './CreateNewButton';
|
|
|
|
|
|
|
|
function render(...[ui, options]: Parameters<typeof rtlRender>) {
|
|
|
|
rtlRender(<TestProvider>{ui}</TestProvider>, options);
|
|
|
|
}
|
2023-04-27 05:43:20 -05:00
|
|
|
|
|
|
|
async function renderAndOpen(folderUID?: string) {
|
2023-06-09 10:00:16 -05:00
|
|
|
render(<CreateNewButton canCreateDashboard canCreateFolder parentFolderUid={folderUID} />);
|
2023-04-27 05:43:20 -05:00
|
|
|
const newButton = screen.getByText('New');
|
|
|
|
await userEvent.click(newButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('NewActionsButton', () => {
|
|
|
|
it('should display the correct urls with a given folderUID', async () => {
|
|
|
|
await renderAndOpen('123');
|
|
|
|
|
2023-06-09 10:00:16 -05:00
|
|
|
expect(screen.getByText('New dashboard')).toHaveAttribute('href', '/dashboard/new?folderUid=123');
|
2023-04-27 05:43:20 -05:00
|
|
|
expect(screen.getByText('Import')).toHaveAttribute('href', '/dashboard/import?folderUid=123');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should display urls without params when there is no folderUID', async () => {
|
|
|
|
await renderAndOpen();
|
|
|
|
|
2023-06-09 10:00:16 -05:00
|
|
|
expect(screen.getByText('New dashboard')).toHaveAttribute('href', '/dashboard/new');
|
2023-04-27 05:43:20 -05:00
|
|
|
expect(screen.getByText('Import')).toHaveAttribute('href', '/dashboard/import');
|
|
|
|
});
|
2023-04-28 11:00:56 -05:00
|
|
|
|
2023-06-09 10:00:16 -05:00
|
|
|
it('clicking the "New folder" button opens the drawer', async () => {
|
|
|
|
const mockParentFolderTitle = 'mockParentFolderTitle';
|
|
|
|
render(<CreateNewButton canCreateDashboard canCreateFolder parentFolderTitle={mockParentFolderTitle} />);
|
|
|
|
|
|
|
|
const newButton = screen.getByText('New');
|
|
|
|
await userEvent.click(newButton);
|
|
|
|
await userEvent.click(screen.getByText('New folder'));
|
|
|
|
|
|
|
|
const drawer = screen.getByRole('dialog', { name: 'Drawer title New folder' });
|
|
|
|
expect(drawer).toBeInTheDocument();
|
|
|
|
expect(within(drawer).getByRole('heading', { name: 'New folder' })).toBeInTheDocument();
|
|
|
|
expect(within(drawer).getByText(`Location: ${mockParentFolderTitle}`)).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
2023-04-28 11:00:56 -05:00
|
|
|
it('should only render dashboard items when folder creation is disabled', async () => {
|
|
|
|
render(<CreateNewButton canCreateDashboard canCreateFolder={false} />);
|
|
|
|
const newButton = screen.getByText('New');
|
|
|
|
await userEvent.click(newButton);
|
|
|
|
|
2023-06-09 10:00:16 -05:00
|
|
|
expect(screen.getByText('New dashboard')).toBeInTheDocument();
|
2023-04-28 11:00:56 -05:00
|
|
|
expect(screen.getByText('Import')).toBeInTheDocument();
|
2023-06-09 10:00:16 -05:00
|
|
|
expect(screen.queryByText('New folder')).not.toBeInTheDocument();
|
2023-04-28 11:00:56 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should only render folder item when dashboard creation is disabled', async () => {
|
|
|
|
render(<CreateNewButton canCreateDashboard={false} canCreateFolder />);
|
|
|
|
const newButton = screen.getByText('New');
|
|
|
|
await userEvent.click(newButton);
|
|
|
|
|
2023-06-09 10:00:16 -05:00
|
|
|
expect(screen.queryByText('New dashboard')).not.toBeInTheDocument();
|
2023-04-28 11:00:56 -05:00
|
|
|
expect(screen.queryByText('Import')).not.toBeInTheDocument();
|
2023-06-09 10:00:16 -05:00
|
|
|
expect(screen.getByText('New folder')).toBeInTheDocument();
|
2023-04-28 11:00:56 -05:00
|
|
|
});
|
2023-04-27 05:43:20 -05:00
|
|
|
});
|