123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- import { within } from '@testing-library/dom';
- import { render, screen } from '@testing-library/react';
- import userEvent from '@testing-library/user-event';
- import React from 'react';
- import { selectors } from '@grafana/e2e-selectors';
- import { setAngularLoader, setDataSourceSrv } from '@grafana/runtime';
- import { mockDataSource, MockDataSourceSrv } from 'app/features/alerting/unified/mocks';
- import { AnnotationsSettings } from './AnnotationsSettings';
- describe('AnnotationsSettings', () => {
- let dashboard: any;
- const dataSources = {
- grafana: mockDataSource(
- {
- name: 'Grafana',
- uid: 'uid1',
- type: 'grafana',
- isDefault: true,
- },
- { annotations: true }
- ),
- Testdata: mockDataSource(
- {
- name: 'Testdata',
- uid: 'uid2',
- type: 'testdata',
- isDefault: true,
- },
- { annotations: true }
- ),
- Prometheus: mockDataSource(
- {
- name: 'Prometheus',
- uid: 'uid3',
- type: 'prometheus',
- },
- { annotations: true }
- ),
- };
- setDataSourceSrv(new MockDataSourceSrv(dataSources));
- const getTableBody = () => screen.getAllByRole('rowgroup')[1];
- const getTableBodyRows = () => within(getTableBody()).getAllByRole('row');
- beforeAll(() => {
- setAngularLoader({
- load: () => ({
- destroy: jest.fn(),
- digest: jest.fn(),
- getScope: () => ({ $watch: () => {} }),
- }),
- });
- });
- beforeEach(() => {
- dashboard = {
- id: 74,
- version: 7,
- annotations: {
- list: [
- {
- builtIn: 1,
- datasource: { uid: 'uid1', type: 'grafana' },
- enable: true,
- hide: true,
- iconColor: 'rgba(0, 211, 255, 1)',
- name: 'Annotations & Alerts',
- type: 'dashboard',
- },
- ],
- },
- links: [],
- };
- });
- test('it renders a header and cta if no annotations or only builtIn annotation', async () => {
- render(<AnnotationsSettings dashboard={dashboard} />);
- expect(screen.getByRole('heading', { name: /annotations/i })).toBeInTheDocument();
- expect(screen.queryByRole('table')).toBeInTheDocument();
- expect(screen.getByRole('row', { name: /annotations & alerts \(built\-in\) grafana/i })).toBeInTheDocument();
- expect(
- screen.getByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query'))
- ).toBeInTheDocument();
- expect(screen.queryByRole('link', { name: /annotations documentation/i })).toBeInTheDocument();
- await userEvent.click(screen.getByRole('cell', { name: /annotations & alerts \(built\-in\)/i }));
- const heading = screen.getByRole('heading', {
- name: /annotations edit/i,
- });
- const nameInput = screen.getByRole('textbox', { name: /name/i });
- expect(heading).toBeInTheDocument();
- await userEvent.clear(nameInput);
- await userEvent.type(nameInput, 'My Annotation');
- expect(screen.queryByText(/grafana/i)).toBeInTheDocument();
- expect(screen.getByRole('checkbox', { name: /hidden/i })).toBeChecked();
- await userEvent.click(within(heading).getByText(/annotations/i));
- expect(screen.getByRole('table')).toBeInTheDocument();
- expect(screen.getByRole('row', { name: /my annotation \(built\-in\) grafana/i })).toBeInTheDocument();
- expect(
- screen.getByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query'))
- ).toBeInTheDocument();
- expect(screen.queryByRole('button', { name: /new query/i })).not.toBeInTheDocument();
- await userEvent.click(screen.getAllByLabelText(/Delete query with title/)[0]);
- await userEvent.click(screen.getByRole('button', { name: 'Delete' }));
- expect(screen.queryAllByRole('row').length).toBe(0);
- expect(
- screen.getByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query'))
- ).toBeInTheDocument();
- });
- test('it renders the anotation names or uid if annotation doesnt exist', async () => {
- const annotationsList = [
- ...dashboard.annotations.list,
- {
- builtIn: 0,
- datasource: { uid: 'uid3', type: 'prometheus' },
- enable: true,
- hide: true,
- iconColor: 'rgba(0, 211, 255, 1)',
- name: 'Annotation 2',
- type: 'dashboard',
- },
- {
- builtIn: 0,
- datasource: { uid: 'deletedAnnotationId', type: 'prometheus' },
- enable: true,
- hide: true,
- iconColor: 'rgba(0, 211, 255, 1)',
- name: 'Annotation 2',
- type: 'dashboard',
- },
- ];
- const dashboardWithAnnotations = {
- ...dashboard,
- annotations: {
- list: [...annotationsList],
- },
- };
- render(<AnnotationsSettings dashboard={dashboardWithAnnotations} />);
- // Check that we have the correct annotations
- expect(screen.queryByText(/prometheus/i)).toBeInTheDocument();
- expect(screen.queryByText(/deletedAnnotationId/i)).toBeInTheDocument();
- });
- test('it renders a sortable table of annotations', async () => {
- const annotationsList = [
- ...dashboard.annotations.list,
- {
- builtIn: 0,
- datasource: { uid: 'uid3', type: 'prometheus' },
- enable: true,
- hide: true,
- iconColor: 'rgba(0, 211, 255, 1)',
- name: 'Annotation 2',
- type: 'dashboard',
- },
- {
- builtIn: 0,
- datasource: { uid: 'uid3', type: 'prometheus' },
- enable: true,
- hide: true,
- iconColor: 'rgba(0, 211, 255, 1)',
- name: 'Annotation 3',
- type: 'dashboard',
- },
- ];
- const dashboardWithAnnotations = {
- ...dashboard,
- annotations: {
- list: [...annotationsList],
- },
- };
- render(<AnnotationsSettings dashboard={dashboardWithAnnotations} />);
- // Check that we have sorting buttons
- expect(within(getTableBodyRows()[0]).queryByRole('button', { name: 'arrow-up' })).not.toBeInTheDocument();
- expect(within(getTableBodyRows()[0]).queryByRole('button', { name: 'arrow-down' })).toBeInTheDocument();
- expect(within(getTableBodyRows()[1]).queryByRole('button', { name: 'arrow-up' })).toBeInTheDocument();
- expect(within(getTableBodyRows()[1]).queryByRole('button', { name: 'arrow-down' })).toBeInTheDocument();
- expect(within(getTableBodyRows()[2]).queryByRole('button', { name: 'arrow-up' })).toBeInTheDocument();
- expect(within(getTableBodyRows()[2]).queryByRole('button', { name: 'arrow-down' })).not.toBeInTheDocument();
- // Check the original order
- expect(within(getTableBodyRows()[0]).queryByText(/annotations & alerts/i)).toBeInTheDocument();
- expect(within(getTableBodyRows()[1]).queryByText(/annotation 2/i)).toBeInTheDocument();
- expect(within(getTableBodyRows()[2]).queryByText(/annotation 3/i)).toBeInTheDocument();
- await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'arrow-down' })[0]);
- await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'arrow-down' })[1]);
- await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'arrow-up' })[0]);
- // Checking if it has changed the sorting accordingly
- expect(within(getTableBodyRows()[0]).queryByText(/annotation 3/i)).toBeInTheDocument();
- expect(within(getTableBodyRows()[1]).queryByText(/annotation 2/i)).toBeInTheDocument();
- expect(within(getTableBodyRows()[2]).queryByText(/annotations & alerts/i)).toBeInTheDocument();
- });
- test('it renders a form for adding/editing annotations', async () => {
- render(<AnnotationsSettings dashboard={dashboard} />);
- await userEvent.click(screen.getByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query')));
- const heading = screen.getByRole('heading', {
- name: /annotations edit/i,
- });
- const nameInput = screen.getByRole('textbox', { name: /name/i });
- expect(heading).toBeInTheDocument();
- await userEvent.clear(nameInput);
- await userEvent.type(nameInput, 'My Prometheus Annotation');
- await userEvent.click(screen.getByText(/testdata/i));
- expect(await screen.findByText(/Prometheus/i)).toBeVisible();
- expect(screen.queryAllByText(/testdata/i)).toHaveLength(2);
- await userEvent.click(screen.getByText(/prometheus/i));
- expect(screen.getByRole('checkbox', { name: /hidden/i })).not.toBeChecked();
- await userEvent.click(within(heading).getByText(/annotations/i));
- expect(within(screen.getAllByRole('rowgroup')[1]).getAllByRole('row').length).toBe(2);
- expect(screen.queryByRole('row', { name: /my prometheus annotation prometheus/i })).toBeInTheDocument();
- expect(screen.queryByRole('button', { name: /new query/i })).toBeInTheDocument();
- expect(
- screen.queryByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query'))
- ).not.toBeInTheDocument();
- await userEvent.click(screen.getByRole('button', { name: /new query/i }));
- await userEvent.click(within(screen.getByRole('heading', { name: /annotations edit/i })).getByText(/annotations/i));
- expect(within(screen.getAllByRole('rowgroup')[1]).getAllByRole('row').length).toBe(3);
- await userEvent.click(screen.getAllByLabelText(/Delete query with title/)[0]);
- await userEvent.click(screen.getByRole('button', { name: 'Delete' }));
- expect(within(screen.getAllByRole('rowgroup')[1]).getAllByRole('row').length).toBe(2);
- });
- });
|