From 47ed51b3ad56cfc56c0cbc0827fbe23f431168ef Mon Sep 17 00:00:00 2001 From: danny-may Date: Tue, 13 Aug 2024 10:54:46 +0100 Subject: [PATCH] Add tests for VirtualizedListBox Signed-off-by: danny-may --- .changeset/big-countries-wait.md | 5 + .../fields/VirtualizedListbox.test.tsx | 277 ++++++++++++++++++ .../components/fields/VirtualizedListbox.tsx | 18 +- 3 files changed, 293 insertions(+), 7 deletions(-) create mode 100644 .changeset/big-countries-wait.md create mode 100644 plugins/scaffolder/src/components/fields/VirtualizedListbox.test.tsx diff --git a/.changeset/big-countries-wait.md b/.changeset/big-countries-wait.md new file mode 100644 index 0000000000..15c48b6d44 --- /dev/null +++ b/.changeset/big-countries-wait.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder': patch +--- + +Add an extra bit of height to the EntityPicker dropdown to make it clear there are more options to select from, and to remove the scroll bar when there is less than 10 options diff --git a/plugins/scaffolder/src/components/fields/VirtualizedListbox.test.tsx b/plugins/scaffolder/src/components/fields/VirtualizedListbox.test.tsx new file mode 100644 index 0000000000..7e77826e32 --- /dev/null +++ b/plugins/scaffolder/src/components/fields/VirtualizedListbox.test.tsx @@ -0,0 +1,277 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import React from 'react'; +import { VirtualizedListbox } from './VirtualizedListbox'; +import { renderInTestApp } from '@backstage/test-utils'; + +describe('', () => { + it('Should forward additional props to the outer div', async () => { + const { baseElement } = await renderInTestApp( + , + ); + + expect(baseElement.children[0]).toMatchInlineSnapshot(` +
+
+
+
+
+
+
+ `); + }); + it('Should render with no items', async () => { + const { baseElement } = await renderInTestApp(); + + expect(baseElement.children[0]).toMatchInlineSnapshot(` +
+
+
+
+
+
+
+ `); + }); + it('Should render with one item', async () => { + const { baseElement } = await renderInTestApp( + + Item 1 + , + ); + + expect(baseElement.children[0]).toMatchInlineSnapshot(` +
+
+
+
+ + Item 1 + +
+
+
+
+ `); + }); + it('Should render with 10 items', async () => { + const { baseElement } = await renderInTestApp( + + {[...new Array(10)].map((_, i) => ( + Item {i} + ))} + , + ); + + expect(baseElement.children[0]).toMatchInlineSnapshot(` +
+
+
+
+ + Item + 0 + + + Item + 1 + + + Item + 2 + + + Item + 3 + + + Item + 4 + + + Item + 5 + + + Item + 6 + + + Item + 7 + + + Item + 8 + + + Item + 9 + +
+
+
+
+ `); + }); + it('Should render up to 10.5 items (+ 2 buffer) even when there are many more', async () => { + const { baseElement } = await renderInTestApp( + + {[...new Array(100)].map((_, i) => ( + Item {i} + ))} + , + ); + + expect(baseElement.children[0]).toMatchInlineSnapshot(` +
+
+
+
+ + Item + 0 + + + Item + 1 + + + Item + 2 + + + Item + 3 + + + Item + 4 + + + Item + 5 + + + Item + 6 + + + Item + 7 + + + Item + 8 + + + Item + 9 + + + Item + 10 + + + Item + 11 + + + Item + 12 + +
+
+
+
+ `); + }); +}); diff --git a/plugins/scaffolder/src/components/fields/VirtualizedListbox.tsx b/plugins/scaffolder/src/components/fields/VirtualizedListbox.tsx index 61e96cd7a9..6ac6568c2f 100644 --- a/plugins/scaffolder/src/components/fields/VirtualizedListbox.tsx +++ b/plugins/scaffolder/src/components/fields/VirtualizedListbox.tsx @@ -17,22 +17,26 @@ import React from 'react'; import { FixedSizeList, ListChildComponentProps } from 'react-window'; +type HTMLDivProps = React.HTMLAttributes; + const renderRow = (props: ListChildComponentProps) => { const { data, index, style } = props; return React.cloneElement(data[index], { style }); }; // Context needed to keep Autocomplete working correctly : https://v4.mui.com/components/autocomplete/#Virtualize.tsx -const OuterElementContext = React.createContext({}); +const OuterElementContext = React.createContext({}); -const OuterElementType = React.forwardRef((props, ref) => { - const outerProps = React.useContext(OuterElementContext); - return
; -}); +const OuterElementType = React.forwardRef( + (props, ref) => { + const outerProps = React.useContext(OuterElementContext); + return
; + }, +); export const VirtualizedListbox = React.forwardRef< HTMLDivElement, - { children?: React.ReactNode } + HTMLDivProps >((props, ref) => { const { children, ...other } = props; const itemData = React.Children.toArray(children); @@ -40,7 +44,7 @@ export const VirtualizedListbox = React.forwardRef< const itemSize = 36; - const itemsToShow = Math.min(10, itemCount); + const itemsToShow = Math.min(10, itemCount) + 0.5; const height = itemsToShow * itemSize; return (