From 85e186cf10b0d672be5ee17f906f19efb765889b Mon Sep 17 00:00:00 2001 From: Josh Hunt Date: Tue, 16 Feb 2021 16:19:55 +0000 Subject: [PATCH] Grafana-UI: Add id to Select to make it easier to test (#31230) * Prettier formatting * Grafana-UI: Add support for id and inputId props to Select * Grafana-UI: Add aria-label to Select * Grafana-UI: InlineField and Field get ID from inputId prop For Select * Fix tests using TagFilter * Update Select prop documentation * Update Field tests to use screen instead * Fix the last few tests --- package.json | 2 +- .../src/components/Forms/Field.test.tsx | 37 +++++++++++++ .../grafana-ui/src/components/Forms/Field.tsx | 10 +--- .../src/components/Forms/InlineField.test.tsx | 37 +++++++++++++ .../src/components/Forms/InlineField.tsx | 7 +-- .../src/components/Select/Select.mdx | 54 ++++++++++++++----- .../src/components/Select/SelectBase.test.tsx | 42 ++++++++++----- .../src/components/Select/SelectBase.tsx | 6 +++ .../grafana-ui/src/components/Select/types.ts | 8 ++- packages/grafana-ui/src/utils/children.ts | 19 +++++++ .../core/components/TagFilter/TagFilter.tsx | 4 +- .../components/DashboardSearch.test.tsx | 9 ++-- .../components/SearchResultsFilter.test.tsx | 4 +- yarn.lock | 21 ++++++++ 14 files changed, 210 insertions(+), 50 deletions(-) create mode 100644 packages/grafana-ui/src/components/Forms/Field.test.tsx create mode 100644 packages/grafana-ui/src/components/Forms/InlineField.test.tsx create mode 100644 packages/grafana-ui/src/utils/children.ts diff --git a/package.json b/package.json index 8a3f1ed944f..371f500ab32 100644 --- a/package.json +++ b/package.json @@ -175,6 +175,7 @@ "postcss-reporter": "6.0.1", "prettier": "2.2.1", "react-hot-loader": "4.8.0", + "react-select-event": "^5.1.0", "react-test-renderer": "16.12.0", "redux-mock-store": "1.5.4", "regexp-replace-loader": "1.0.1", @@ -216,7 +217,6 @@ "@types/react-virtualized-auto-sizer": "1.0.0", "@types/sockjs-client": "^1.1.1", "@types/uuid": "8.3.0", - "@types/hoist-non-react-statics": "3.3.1", "@welldone-software/why-did-you-render": "4.0.6", "abortcontroller-polyfill": "1.4.0", "angular": "1.8.2", diff --git a/packages/grafana-ui/src/components/Forms/Field.test.tsx b/packages/grafana-ui/src/components/Forms/Field.test.tsx new file mode 100644 index 00000000000..970985fa95c --- /dev/null +++ b/packages/grafana-ui/src/components/Forms/Field.test.tsx @@ -0,0 +1,37 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import { Input } from '../Input/Input'; +import { Field } from './Field'; +import { Select } from '../Select/Select'; + +describe('Field', () => { + it('renders the label', () => { + render( + + + + ); + + expect(screen.getByText('My label')).toBeInTheDocument(); + }); + + it('renders with the id of its children', () => { + render( + + + + ); + + expect(screen.getByLabelText('My label')).toBeInTheDocument(); + }); + + it('renders with the inputId of its children', () => { + render( + + + + ); + + expect(screen.getByText('My label')).toBeInTheDocument(); + }); + + it('renders with the id of its children', () => { + render( + + + + ); + + expect(screen.getByLabelText('My label')).toBeInTheDocument(); + }); + + it('renders with the inputId of its children', () => { + render( + + ` using its matching label, such as the label assigned with the `inputId` prop. Use the `react-select-event` package to select values from the options. + +```tsx +import { render, screen } from '@testing-library/react'; +import selectEvent from 'react-select-event'; +import { Select } from '@grafana/ui'; + +it('should call onChange', () => { + const onChange = jest.fn(); + + render( + <> + +