grafana/public/app/features/browse-dashboards/BrowseFolderLibraryPanelsPage.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

135 lines
4.5 KiB
TypeScript

import { render as rtlRender, screen } from '@testing-library/react';
import { http, HttpResponse } from 'msw';
import { SetupServer, setupServer } from 'msw/node';
import { TestProvider } from 'test/helpers/TestProvider';
import { contextSrv } from 'app/core/core';
import { getRouteComponentProps } from 'app/core/navigation/__mocks__/routeProps';
import { backendSrv } from 'app/core/services/backend_srv';
import BrowseFolderLibraryPanelsPage, { OwnProps } from './BrowseFolderLibraryPanelsPage';
import { getLibraryElementsResponse } from './fixtures/libraryElements.fixture';
import * as permissions from './permissions';
function render(...[ui, options]: Parameters<typeof rtlRender>) {
rtlRender(<TestProvider>{ui}</TestProvider>, options);
}
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
getBackendSrv: () => backendSrv,
config: {
...jest.requireActual('@grafana/runtime').config,
unifiedAlertingEnabled: true,
},
}));
const mockFolderName = 'myFolder';
const mockFolderUid = '12345';
const mockLibraryElementsResponse = getLibraryElementsResponse(1, {
folderUid: mockFolderUid,
});
describe('browse-dashboards BrowseFolderLibraryPanelsPage', () => {
let props: OwnProps;
let server: SetupServer;
const mockPermissions = {
canCreateDashboards: true,
canEditDashboards: true,
canCreateFolders: true,
canDeleteFolders: true,
canEditFolders: true,
canViewPermissions: true,
canSetPermissions: true,
};
beforeAll(() => {
server = setupServer(
http.get('/api/folders/:uid', () => {
return HttpResponse.json({
title: mockFolderName,
uid: mockFolderUid,
});
}),
http.get('/api/library-elements', () => {
return HttpResponse.json({
result: mockLibraryElementsResponse,
});
}),
http.get('/api/search/sorting', () => {
return HttpResponse.json({});
})
);
server.listen();
});
afterAll(() => {
server.close();
});
beforeEach(() => {
jest.spyOn(permissions, 'getFolderPermissions').mockImplementation(() => mockPermissions);
jest.spyOn(contextSrv, 'hasPermission').mockReturnValue(true);
props = {
...getRouteComponentProps({
match: {
params: {
uid: mockFolderUid,
},
isExact: false,
path: '',
url: '',
},
}),
};
});
afterEach(() => {
jest.restoreAllMocks();
server.resetHandlers();
});
it('displays the folder title', async () => {
render(<BrowseFolderLibraryPanelsPage {...props} />);
expect(await screen.findByRole('heading', { name: mockFolderName })).toBeInTheDocument();
});
it('displays the "Folder actions" button', async () => {
render(<BrowseFolderLibraryPanelsPage {...props} />);
expect(await screen.findByRole('button', { name: 'Folder actions' })).toBeInTheDocument();
});
it('does not display the "Folder actions" button if the user does not have permissions', async () => {
jest.spyOn(permissions, 'getFolderPermissions').mockImplementation(() => {
return {
...mockPermissions,
canDeleteFolders: false,
canEditFolders: false,
canViewPermissions: false,
canSetPermissions: false,
};
});
render(<BrowseFolderLibraryPanelsPage {...props} />);
expect(await screen.findByRole('heading', { name: mockFolderName })).toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Folder actions' })).not.toBeInTheDocument();
});
it('displays all the folder tabs and shows the "Library panels" tab as selected', async () => {
render(<BrowseFolderLibraryPanelsPage {...props} />);
expect(await screen.findByRole('tab', { name: 'Dashboards' })).toBeInTheDocument();
expect(await screen.findByRole('tab', { name: 'Dashboards' })).toHaveAttribute('aria-selected', 'false');
expect(await screen.findByRole('tab', { name: 'Panels' })).toBeInTheDocument();
expect(await screen.findByRole('tab', { name: 'Panels' })).toHaveAttribute('aria-selected', 'true');
expect(await screen.findByRole('tab', { name: 'Alert rules' })).toBeInTheDocument();
expect(await screen.findByRole('tab', { name: 'Alert rules' })).toHaveAttribute('aria-selected', 'false');
});
it('displays the library panels returned by the API', async () => {
render(<BrowseFolderLibraryPanelsPage {...props} />);
expect(await screen.findByText(mockLibraryElementsResponse.elements[0].name)).toBeInTheDocument();
});
});