Skip to content
Snippets Groups Projects
Unverified Commit 6c83c8a2 authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Fix sorting and add tests (#19045)

parent dec0d84a
No related merge requests found
...@@ -20,6 +20,8 @@ import { ...@@ -20,6 +20,8 @@ import {
EngineSearchRoot, EngineSearchRoot,
} from "./EngineWidget.styled"; } from "./EngineWidget.styled";
const DEFAULT_OPTIONS_COUNT = 6;
const EngineWidget = ({ field, options, isHosted }) => { const EngineWidget = ({ field, options, isHosted }) => {
if (field.value) { if (field.value) {
return <EngineInfo field={field} options={options} />; return <EngineInfo field={field} options={options} />;
...@@ -63,6 +65,7 @@ const EngineSearch = ({ field, options, isHosted }) => { ...@@ -63,6 +65,7 @@ const EngineSearch = ({ field, options, isHosted }) => {
const [searchText, setSearchText] = useState(""); const [searchText, setSearchText] = useState("");
const [isExpanded, setIsExpanded] = useState(false); const [isExpanded, setIsExpanded] = useState(false);
const isSearching = searchText.length > 0; const isSearching = searchText.length > 0;
const hasMoreOptions = options.length > DEFAULT_OPTIONS_COUNT;
const sortedOptions = useMemo(() => { const sortedOptions = useMemo(() => {
return getSortedOptions(options); return getSortedOptions(options);
...@@ -85,7 +88,7 @@ const EngineSearch = ({ field, options, isHosted }) => { ...@@ -85,7 +88,7 @@ const EngineSearch = ({ field, options, isHosted }) => {
) : ( ) : (
<EngineEmptyState isHosted={isHosted} /> <EngineEmptyState isHosted={isHosted} />
)} )}
{!isSearching && ( {!isSearching && hasMoreOptions && (
<EngineToggle <EngineToggle
isExpanded={isExpanded} isExpanded={isExpanded}
onExpandedChange={setIsExpanded} onExpandedChange={setIsExpanded}
...@@ -188,6 +191,8 @@ const getSortedOptions = options => { ...@@ -188,6 +191,8 @@ const getSortedOptions = options => {
return a.index - b.index; return a.index - b.index;
} else if (a.index >= 0) { } else if (a.index >= 0) {
return -1; return -1;
} else if (b.index >= 0) {
return 1;
} else { } else {
return a.name.localeCompare(b.name); return a.name.localeCompare(b.name);
} }
...@@ -200,7 +205,7 @@ const getVisibleOptions = (options, isExpanded, isSearching, searchText) => { ...@@ -200,7 +205,7 @@ const getVisibleOptions = (options, isExpanded, isSearching, searchText) => {
} else if (isExpanded) { } else if (isExpanded) {
return options; return options;
} else { } else {
return options.filter(e => e.index >= 0); return options.slice(0, DEFAULT_OPTIONS_COUNT);
} }
}; };
......
...@@ -32,13 +32,13 @@ describe("EngineWidget", () => { ...@@ -32,13 +32,13 @@ describe("EngineWidget", () => {
userEvent.click(screen.getByText("Show more options")); userEvent.click(screen.getByText("Show more options"));
expect(screen.getByText("MySQL")).toBeInTheDocument(); expect(screen.getByText("MySQL")).toBeInTheDocument();
expect(screen.getByText("PostgreSQL")).toBeInTheDocument(); expect(screen.getByText("H2")).toBeInTheDocument();
expect(screen.getByText("SQL Server")).toBeInTheDocument(); expect(screen.getByText("Presto")).toBeInTheDocument();
userEvent.click(screen.getByText("Show less options")); userEvent.click(screen.getByText("Show less options"));
expect(screen.getByText("MySQL")).toBeInTheDocument(); expect(screen.getByText("MySQL")).toBeInTheDocument();
expect(screen.getByText("PostgreSQL")).toBeInTheDocument(); expect(screen.getByText("H2")).toBeInTheDocument();
expect(screen.queryByText("SQL Server")).not.toBeInTheDocument(); expect(screen.queryByText("Presto")).not.toBeInTheDocument();
}); });
it("should allow searching for a database", () => { it("should allow searching for a database", () => {
...@@ -86,18 +86,35 @@ const getOptions = () => [ ...@@ -86,18 +86,35 @@ const getOptions = () => [
name: "MySQL", name: "MySQL",
value: "mysql", value: "mysql",
index: 1, index: 1,
official: true, },
{
name: "PostgreSQL",
value: "postgres",
index: 2,
}, },
{ {
name: "SQL Server", name: "SQL Server",
value: "sqlserver", value: "sqlserver",
index: 3,
},
{
name: "Amazon Redshift",
value: "redshift",
index: 4,
},
{
name: "Snowflake",
value: "snowflake",
index: 5,
},
{
name: "H2",
value: "h2",
index: -1, index: -1,
official: true,
}, },
{ {
name: "PostgreSQL", name: "Presto",
value: "postgres", value: "presto-jdbc",
index: 0, index: -1,
official: false,
}, },
]; ];
...@@ -61,7 +61,7 @@ export function getElevatedEngines() { ...@@ -61,7 +61,7 @@ export function getElevatedEngines() {
"postgres", "postgres",
"sqlserver", "sqlserver",
"redshift", "redshift",
"bigquery", "bigquery-cloud-sdk",
"snowflake", "snowflake",
]; ];
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment