Files
grafana/public/app/features/browse-dashboards/components/CreateNewButton.test.tsx
Ashley Harrison 47f8717149 React: Use new JSX transform (#88802)
* update eslint, tsconfig + esbuild to handle new jsx transform

* remove thing that breaks the new jsx transform

* remove react imports

* adjust grafana-icons build

* is this the correct syntax?

* try this

* well this was much easier than expected...

* change grafana-plugin-configs webpack config

* fixes

* fix lockfile

* fix 2 more violations

* use path.resolve instead of require.resolve

* remove react import

* fix react imports

* more fixes

* remove React import

* remove import React from docs

* remove another react import
2024-06-25 12:43:47 +01:00

77 lines
3.1 KiB
TypeScript

import { render as rtlRender, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { TestProvider } from 'test/helpers/TestProvider';
import { FolderDTO } from 'app/types';
import { mockFolderDTO } from '../fixtures/folder.fixture';
import CreateNewButton from './CreateNewButton';
const mockParentFolder = mockFolderDTO();
function render(...[ui, options]: Parameters<typeof rtlRender>) {
rtlRender(<TestProvider>{ui}</TestProvider>, options);
}
async function renderAndOpen(folder?: FolderDTO) {
render(<CreateNewButton canCreateDashboard canCreateFolder parentFolder={folder} />);
const newButton = screen.getByText('New');
await userEvent.click(newButton);
}
describe('NewActionsButton', () => {
it('should display the correct urls with a given parent folder', async () => {
await renderAndOpen(mockParentFolder);
expect(screen.getByRole('link', { name: 'New dashboard' })).toHaveAttribute(
'href',
`/dashboard/new?folderUid=${mockParentFolder.uid}`
);
expect(screen.getByRole('link', { name: 'Import' })).toHaveAttribute(
'href',
`/dashboard/import?folderUid=${mockParentFolder.uid}`
);
});
it('should display urls without params when there is no parent folder', async () => {
await renderAndOpen();
expect(screen.getByRole('link', { name: 'New dashboard' })).toHaveAttribute('href', '/dashboard/new');
expect(screen.getByRole('link', { name: 'Import' })).toHaveAttribute('href', '/dashboard/import');
});
it('clicking the "New folder" button opens the drawer', async () => {
render(<CreateNewButton canCreateDashboard canCreateFolder parentFolder={mockParentFolder} />);
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: ${mockParentFolder.title}`)).toBeInTheDocument();
});
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);
expect(screen.getByRole('link', { name: 'New dashboard' })).toBeInTheDocument();
expect(screen.getByText('Import')).toBeInTheDocument();
expect(screen.queryByText('New folder')).not.toBeInTheDocument();
});
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);
expect(screen.queryByText('New dashboard')).not.toBeInTheDocument();
expect(screen.queryByText('Import')).not.toBeInTheDocument();
expect(screen.getByText('New folder')).toBeInTheDocument();
});
});