From b327f02eef596d57f946efbf790cd7017d0d4dde Mon Sep 17 00:00:00 2001 From: Wouter Admiraal Date: Wed, 19 Jan 2022 14:26:49 +0100 Subject: [PATCH] SONAR-15901 Make SelectList component more accessible --- .../controls/SelectListListElement.tsx | 17 +++++--- .../__tests__/SelectListListElement-test.tsx | 40 ++++++++++++------- .../SelectListListElement-test.tsx.snap | 16 ++++---- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/server/sonar-web/src/main/js/components/controls/SelectListListElement.tsx b/server/sonar-web/src/main/js/components/controls/SelectListListElement.tsx index a3d3aac73f2..d839989e101 100644 --- a/server/sonar-web/src/main/js/components/controls/SelectListListElement.tsx +++ b/server/sonar-web/src/main/js/components/controls/SelectListListElement.tsx @@ -27,7 +27,7 @@ interface Props { element: string; onSelect: (element: string) => Promise; onUnselect: (element: string) => Promise; - renderElement: (element: string) => React.ReactNode; + renderElement: (element: string) => React.ReactNode | [React.ReactNode, React.ReactNode]; selected: boolean; } @@ -60,23 +60,28 @@ export default class SelectListListElement extends React.PureComponent - - {this.props.renderElement(this.props.element)} - + {item} + {extra && {extra}} ); } diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/SelectListListElement-test.tsx b/server/sonar-web/src/main/js/components/controls/__tests__/SelectListListElement-test.tsx index ca127222e04..e2e060f40ad 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/SelectListListElement-test.tsx +++ b/server/sonar-web/src/main/js/components/controls/__tests__/SelectListListElement-test.tsx @@ -22,26 +22,36 @@ import * as React from 'react'; import { waitAndUpdate } from '../../../helpers/testUtils'; import SelectListListElement from '../SelectListListElement'; -const listElement = ( - Promise.resolve())} - onUnselect={jest.fn(() => Promise.resolve())} - renderElement={(foo: string) => foo} - selected={false} - /> -); - it('should display a loader when checking', async () => { - const wrapper = shallow(listElement); - expect(wrapper).toMatchSnapshot(); + const wrapper = shallowRender(); + expect(wrapper).toMatchSnapshot('default'); expect(wrapper.state().loading).toBe(false); - (wrapper.instance() as SelectListListElement).handleCheck(true); + wrapper.instance().handleCheck(true); expect(wrapper.state().loading).toBe(true); - expect(wrapper).toMatchSnapshot(); + expect(wrapper).toMatchSnapshot('loading'); await waitAndUpdate(wrapper); expect(wrapper.state().loading).toBe(false); }); + +it('should correctly handle a render callback that returns 2 elements', () => { + const wrapper = shallowRender({ + renderElement: (foo: string) => [foo, 'extra info'] + }); + expect(wrapper.find('.select-list-list-extra').exists()).toBe(true); +}); + +function shallowRender(props: Partial = {}) { + return shallow( + Promise.resolve())} + onUnselect={jest.fn(() => Promise.resolve())} + renderElement={(foo: string) => foo} + selected={false} + {...props} + /> + ); +} diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SelectListListElement-test.tsx.snap b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SelectListListElement-test.tsx.snap index 0b05a479c5c..9acb0969df1 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SelectListListElement-test.tsx.snap +++ b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SelectListListElement-test.tsx.snap @@ -1,18 +1,18 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`should display a loader when checking 1`] = ` +exports[`should display a loader when checking: default 1`] = `
  • foo @@ -20,19 +20,19 @@ exports[`should display a loader when checking 1`] = `
  • `; -exports[`should display a loader when checking 2`] = ` +exports[`should display a loader when checking: loading 1`] = `
  • foo -- 2.39.5