Skip to content

Commit

Permalink
test: Add tests for MultiTypeaheadSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
mvollmer committed Dec 10, 2024
1 parent 9bd085e commit b1ce8d6
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/lib/cockpit-components-multi-typeahead-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSel
}

// Open the menu when the input value changes and the new value is not empty
openMenu();
if (!isOpen) {
openMenu();
}
}

setSelectOptions(newSelectOptions);
Expand Down
162 changes: 162 additions & 0 deletions pkg/playground/react-demo-multi-typeahead.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* This file is part of Cockpit.
*
* Copyright (C) 2024 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <https://www.gnu.org/licenses/>.
*/

import cockpit from "cockpit";

import React, { useState } from "react";
import { createRoot, Container } from 'react-dom/client';

import { Checkbox } from '@patternfly/react-core';
import { MultiTypeaheadSelect, MultiTypeaheadSelectOption } from "cockpit-components-multi-typeahead-select";

const MultiTypeaheadDemo = ({ options } : { options: MultiTypeaheadSelectOption[] }) => {
const [notFoundIsString, setNotFoundIsString] = useState(false);
const [selected, setSelected] = useState<(string | number)[]>([]);
const [toggles, setToggles] = useState(0);
const [changes, setChanges] = useState(0);

function add(val: string | number) {
setSelected(selected.concat([val]));
}

function rem(val: string | number) {
setSelected(selected.filter(v => v != val));
}

return (
<div>
<MultiTypeaheadSelect
id='multi-typeahead-widget'
placeholder="Select flavors"
isScrollable
noOptionsFoundMessage={notFoundIsString ? "Not found" : val => cockpit.format("'$0' not found", val) }
options={options}
selected={selected}
onAdd={add}
onRemove={rem}
onToggle={() => setToggles(val => val + 1)}
onInputChange={() => setChanges(val => val + 1)}
/>
<div>Selected: <span id="multi-value">{JSON.stringify(selected)}</span></div>
<div>Toggles: <span id="multi-toggles">{toggles}</span></div>
<div>Changes: <span id="multi-changes">{changes}</span></div>
<Checkbox
id="notFoundIsStringMulti"
label="notFoundIsString"
isChecked={notFoundIsString}
onChange={(_event, checked) => setNotFoundIsString(checked)}
/>
</div>
);
};

export function showMultiTypeaheadDemo(rootElement: Container) {
const flavors: string[] = [
"Alumni Swirl",
"Apple Cobbler Crunch",
"Arboretum Breeze",
"August Pie",
"Autumn Delight",
"Bavarian Raspberry Crunch",
"Berkey Brickle",
"Birthday Bash",
"Bittersweet Mint",
"Black Cow",
"Black Raspberry",
"Blueberry Cheesecake",
"Butter Pecan",
"Candy Bar/Snickers",
"Caramel Critters",
"Centennial Vanilla Bean",
"Cherry Cheesecake",
"Cherry Chip",
"Cherry Quist",
"Cherry Sherbet",
"Chocolate",
"Chocolate Cherry Cordia",
"Chocolate Chip",
"Chocolate Chip Cheesecake",
"Chocolate Chip Cookie Dough",
"Chocolate Chocolate Nut",
"Chocolate Marble",
"Chocolate Marshmallow",
"Chocolate Pretzel Crunch",
"Chunky Chocolate",
"Chunky Chocolate- Vanilla",
"Coconut Chip",
"Coffee Mocha Fudge",
"Coffee w/Cream and Sugar",
"Crazy Charlie Sundae Swirl",
"Death By Chocolate",
"Egg Nog",
"Espresso Fudge Pie",
"German Chocolate Cake",
"Golden Chocolate Pecan",
"Goo Goo Cluster",
"Grape Sherbet",
"Happy Happy Joy Joy",
"Heath Bar Candy",
"Just Fudge",
"Kenney Beany Chocolate",
"Lion Tracks",
"LionS'more",
"Mallo Cup",
"Maple Nut",
"Mint Nittany",
"Monster Mash",
"Orange Vanilla Sundae",
"Palmer Mousseum With Almonds",
"Peachy Paterno",
"Peanut Butter Cup",
"Peanut Butter Fudge Cluster",
"Peanut Butter Marshmallow",
"Peanut Butter Swirl",
"Pecan Apple Danish",
"Peppermint Stick",
"Pistachio",
"Pralines N Cream",
"Pumpkin Pie",
"Raspberry Fudge Torte",
"Raspberry Parfait",
"Rum Raisin",
"Russ 'Digs' Roseberry",
"Santa Fe Banana",
"Scholar's Chip",
"Sea Salt Chocolate Caramel",
"Somerset Shortcake",
"Southern Chocolate Pie",
"Southern Pecan Cheesecake",
"Strawberry",
"Strawberry Cheesecake",
"Teaberry",
"Tin Roof Sundae",
"Toasted Almond",
"Toasted Almond Fudge",
"Turtle Creek",
"Vanilla",
"White House",
"Wicked Caramel Sundae",
"WPSU Coffee Break",
];

const options: MultiTypeaheadSelectOption[] = flavors.map((f, i) => ({ value: i + 1, content: f }));

const root = createRoot(rootElement);
root.render(<MultiTypeaheadDemo options={options} />);
}
5 changes: 5 additions & 0 deletions pkg/playground/react-patterns.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ <h3>Typeahead</h3>
<div id="demo-typeahead"></div>
</section>

<section class="pf-v5-c-page__main-section pf-m-light">
<h3>Multi Typeahead</h3>
<div id="demo-multi-typeahead"></div>
</section>

<section class="pf-v5-c-page__main-section pf-m-light">
<h3>Dialogs</h3>
<button id="demo-show-dialog" class="pf-v5-c-button pf-m-secondary">Show Dialog</button>
Expand Down
4 changes: 4 additions & 0 deletions pkg/playground/react-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { showCardsDemo } from "./react-demo-cards.jsx";
import { showUploadDemo } from "./react-demo-file-upload.jsx";
import { showFileAcDemo, showFileAcDemoPreselected } from "./react-demo-file-autocomplete.jsx";
import { showTypeaheadDemo } from "./react-demo-typeahead.jsx";
import { showMultiTypeaheadDemo } from "./react-demo-multi-typeahead.jsx";

/* -----------------------------------------------------------------------------
Modal Dialog
Expand Down Expand Up @@ -130,6 +131,9 @@ document.addEventListener("DOMContentLoaded", function() {
// Plain typeahead select with headers and dividers
showTypeaheadDemo(document.getElementById('demo-typeahead'));

// Multi typeahead
showMultiTypeaheadDemo(document.getElementById('demo-multi-typeahead'));

// Cards
showCardsDemo(document.getElementById('demo-cards'));

Expand Down
104 changes: 104 additions & 0 deletions test/verify/check-lib
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,110 @@ class TestLib(testlib.MachineCase):
b.wait_text("#toggles", "14")
b.wait_text("#changes", "26")

def testMultiTypeaheadSelect(self):
b = self.browser

# Login

self.login_and_go("/playground/react-patterns")

# No clear button

b.wait_not_visible("#demo-multi-typeahead .pf-v5-c-text-input-group__utilities button")

# Open menu, pixel test

b.click("#demo-multi-typeahead .pf-v5-c-menu-toggle__button")
b.assert_pixels("#multi-typeahead-widget", "menu")
b.wait_text("#multi-toggles", "1")
b.wait_text("#multi-changes", "0")

# Select from menu (with mouse)

b.click("#multi-typeahead-widget .pf-v5-c-menu__item:contains(Strawberry Cheesecake)")
b.wait_not_present("#multi-typeahead-widget")
b.assert_pixels("#demo-multi-typeahead .pf-v5-c-menu-toggle", "input")
b.wait_text("#multi-value", "[76]")
b.wait_text("#multi-toggles", "2")
b.wait_text("#multi-changes", "0")

# Select another one

b.click("#demo-multi-typeahead .pf-v5-c-menu-toggle__button")
b.wait_visible("#multi-typeahead-widget")
b.click("#multi-typeahead-widget .pf-v5-c-menu__item:contains(Coconut Chip)")
b.wait_not_present("#multi-typeahead-widget")
b.wait_text("#multi-value", "[76,32]")
b.wait_text("#multi-toggles", "4")
b.wait_text("#multi-changes", "0")

# Remove one

b.click("#demo-multi-typeahead .pf-v5-c-label:contains(Strawberry Cheesecake) button")
b.wait_text("#multi-value", "[32]")
b.wait_text("#multi-toggles", "4")
b.wait_text("#multi-changes", "0")

# Open by clicking into input, close with ESC

b.click("#demo-multi-typeahead .pf-v5-c-text-input-group__text-input")
b.wait_visible("#multi-typeahead-widget")
b.key("Escape")
b.wait_not_present("#multi-typeahead-widget")
b.wait_text("#multi-toggles", "6")
b.wait_text("#multi-changes", "0")

# Select with keys

b.click("#demo-multi-typeahead .pf-v5-c-menu-toggle__button")
b.wait_visible("#multi-typeahead-widget")
b.key("ArrowDown") # Alumni Swirl
b.key("ArrowUp") # Wraps, WPSU Coffee break
b.key("ArrowDown") # Wraps
b.key("ArrowDown")
b.key("ArrowDown")
b.key("ArrowDown")
b.key("ArrowDown") # Autumn Delight
b.key("Enter")
b.key("Escape")
b.wait_not_present("#multi-typeahead-widget")
b.wait_text("#multi-value", "[32,5]")
b.wait_text("#multi-toggles", "8")
b.wait_text("#multi-changes", "0")

b.wait_visible("#demo-multi-typeahead .pf-v5-c-label:contains(Coconut Chip)")
b.wait_visible("#demo-multi-typeahead .pf-v5-c-label:contains(Autumn Delight)")

# Search for non-existent

b.set_input_text("#demo-multi-typeahead .pf-v5-c-text-input-group__text-input", "Salmiak")
b.wait_text("#multi-typeahead-widget .pf-v5-c-menu__item", "'Salmiak' not found")
b.click("#demo-multi-typeahead .pf-v5-c-menu-toggle__button")
b.wait_not_present("#multi-typeahead-widget")
b.wait_text("#multi-toggles", "10")
b.wait_text("#multi-changes", "7")

# Again with formatted "not found" message

b.set_checked("#notFoundIsStringMulti", val=True)
b.set_input_text("#demo-multi-typeahead .pf-v5-c-text-input-group__text-input", "Salmiaki")
b.wait_text("#multi-typeahead-widget .pf-v5-c-menu__item", "Not found")
b.click("#demo-multi-typeahead .pf-v5-c-menu-toggle__button")
b.wait_not_present("#multi-typeahead-widget")
b.wait_text("#multi-toggles", "12")
b.wait_text("#multi-changes", "15")

# Search for existing, pixel test, select

b.set_input_text("#demo-multi-typeahead .pf-v5-c-text-input-group__text-input", "Rum")
b.wait_visible("#multi-typeahead-widget")
b.assert_pixels("#multi-typeahead-widget", "search")
b.click("#multi-typeahead-widget .pf-v5-c-menu__item")
b.wait_not_present("#multi-typeahead-widget")
b.wait_text("#multi-value", "[32,5,67]")
b.wait_text("#multi-toggles", "14")
b.wait_text("#multi-changes", "18")


if __name__ == '__main__':
testlib.test_main()

0 comments on commit b1ce8d6

Please sign in to comment.