diff --git a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx index ddb479e9cf9..4dbf411b2ec 100644 --- a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx +++ b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx @@ -1,8 +1,7 @@ import React from 'react'; -import { cleanup, fireEvent, render, screen, act } from '@testing-library/react'; +import { fireEvent, render, screen, act, waitFor } from '@testing-library/react'; import selectEvent from 'react-select-event'; import { setupMockedDataSource } from '../../__mocks__/CloudWatchDataSource'; -import '@testing-library/jest-dom'; import { CloudWatchMetricsQuery } from '../../types'; import userEvent from '@testing-library/user-event'; import { MetricStatEditor } from '..'; @@ -36,68 +35,68 @@ const props = { onRunQuery: jest.fn(), }; -afterEach(cleanup); - describe('MetricStatEditor', () => { describe('statistics field', () => { - test.each([['Average', 'p23.23', 'p34', '$statistic']])('should accept valid values', (statistic) => { + test.each([['Average', 'p23.23', 'p34', '$statistic']])('should accept valid values', async (statistic) => { const onChange = jest.fn(); const onRunQuery = jest.fn(); props.datasource.getVariables = jest.fn().mockReturnValue(['$statistic']); render(); - const statisticElement = screen.getByLabelText('Statistic'); + const statisticElement = await screen.findByLabelText('Statistic'); expect(statisticElement).toBeInTheDocument(); - userEvent.type(statisticElement!, statistic); - fireEvent.keyDown(statisticElement!, { keyCode: 13 }); + userEvent.type(statisticElement, statistic); + fireEvent.keyDown(statisticElement, { keyCode: 13 }); expect(onChange).toHaveBeenCalledWith({ ...props.query, statistic }); expect(onRunQuery).toHaveBeenCalled(); }); - test.each([['CustomStat', 'p23,23', '$statistic']])('should not accept invalid values', (statistic) => { + test.each([['CustomStat', 'p23,23', '$statistic']])('should not accept invalid values', async (statistic) => { const onChange = jest.fn(); const onRunQuery = jest.fn(); render(); - const statisticElement = screen.getByLabelText('Statistic'); + const statisticElement = await screen.findByLabelText('Statistic'); expect(statisticElement).toBeInTheDocument(); - userEvent.type(statisticElement!, statistic); - fireEvent.keyDown(statisticElement!, { keyCode: 13 }); + userEvent.type(statisticElement, statistic); + fireEvent.keyDown(statisticElement, { keyCode: 13 }); expect(onChange).not.toHaveBeenCalled(); expect(onRunQuery).not.toHaveBeenCalled(); }); }); describe('expressions', () => { - it('should display match exact switch is not set', () => { + it('should display match exact switch is not set', async () => { render(); - expect(screen.getByText('Match exact')).toBeInTheDocument(); + expect(await screen.findByText('Match exact')).toBeInTheDocument(); }); - it('should display match exact switch if prop is set to false', () => { + it('should display match exact switch if prop is set to false', async () => { render(); - expect(screen.getByText('Match exact')).toBeInTheDocument(); + expect(await screen.findByText('Match exact')).toBeInTheDocument(); }); it('should not display match exact switch if prop is set to true', async () => { render(); - expect(screen.queryByText('Match exact')).toBeNull(); + await waitFor(() => { + expect(screen.queryByText('Match exact')).toBeNull(); + }); }); }); describe('match exact', () => { - it('should be checked when value is true', () => { + it('should be checked when value is true', async () => { render(); - expect(screen.getByLabelText('Match exact - optional')).toBeChecked(); + expect(await screen.findByLabelText('Match exact - optional')).toBeChecked(); }); - it('should be unchecked when value is false', () => { + it('should be unchecked when value is false', async () => { render(); - expect(screen.getByLabelText('Match exact - optional')).not.toBeChecked(); + expect(await screen.findByLabelText('Match exact - optional')).not.toBeChecked(); }); });