Skip to content
Snippets Groups Projects
Unverified Commit c8704344 authored by Dalton's avatar Dalton Committed by GitHub
Browse files

Convert metabase/parameters/utils/ui to TypeScript (#24566)

parent 32002457
No related branches found
No related tags found
No related merge requests found
import _ from "underscore";
import { isEqualsOperator } from "metabase/lib/schema_metadata";
import { UiParameter } from "metabase/parameters/types";
import { getParameterType } from "./parameter-type";
import { deriveFieldOperatorFromParameter } from "./operators";
export function getParameterIconName(parameter) {
export function getParameterIconName(parameter: UiParameter) {
const type = getParameterType(parameter);
switch (type) {
case "date":
......@@ -21,20 +22,25 @@ export function getParameterIconName(parameter) {
}
}
export function buildHiddenParametersSlugSet(hiddenParameterSlugs) {
export function buildHiddenParametersSlugSet(
hiddenParameterSlugs: string | undefined,
) {
return _.isString(hiddenParameterSlugs)
? new Set(hiddenParameterSlugs.split(","))
: new Set();
}
export function getVisibleParameters(parameters, hiddenParameterSlugs) {
export function getVisibleParameters(
parameters: UiParameter[],
hiddenParameterSlugs: string | undefined,
) {
const hiddenParametersSlugSet =
buildHiddenParametersSlugSet(hiddenParameterSlugs);
return parameters.filter(p => !hiddenParametersSlugSet.has(p.slug));
}
export function getParameterWidgetTitle(parameter) {
export function getParameterWidgetTitle(parameter: UiParameter) {
const operator = deriveFieldOperatorFromParameter(parameter);
const { verboseName } = operator || {};
......
......@@ -5,15 +5,23 @@ import {
getParameterWidgetTitle,
} from "./ui";
import { createMockUiParameter } from "metabase/parameters/mock";
describe("parameters/utils/ui", () => {
describe("getParameterIconName", () => {
it("should return an icon name for the given parameter", () => {
expect(getParameterIconName({ type: "category" })).toEqual("string");
expect(getParameterIconName({ type: "date/single" })).toEqual("calendar");
expect(
getParameterIconName(createMockUiParameter({ type: "category" })),
).toEqual("string");
expect(
getParameterIconName(createMockUiParameter({ type: "date/single" })),
).toEqual("calendar");
});
it("should safely default", () => {
expect(getParameterIconName({ type: "???" })).toEqual("label");
expect(
getParameterIconName(createMockUiParameter({ type: "???" })),
).toEqual("label");
});
});
......@@ -24,66 +32,75 @@ describe("parameters/utils/ui", () => {
);
});
it("should return an empty set for any input that is not a string", () => {
it("should return an empty set for an arg that is undefined", () => {
expect(buildHiddenParametersSlugSet(undefined)).toEqual(new Set());
expect(buildHiddenParametersSlugSet(111111)).toEqual(new Set());
});
});
describe("getVisibleParameters", () => {
const parameters = [
{
id: 1,
createMockUiParameter({
id: "1",
slug: "foo",
},
{
id: 2,
}),
createMockUiParameter({
id: "2",
slug: "bar",
},
{
id: 3,
}),
createMockUiParameter({
id: "3",
slug: "baz",
},
{
id: 4,
}),
createMockUiParameter({
id: "4",
slug: "qux",
},
}),
];
const hiddenParameterSlugs = "bar,baz";
it("should return the parameters that are not hidden", () => {
expect(getVisibleParameters(parameters, hiddenParameterSlugs)).toEqual([
{
id: 1,
expect.objectContaining({
id: "1",
slug: "foo",
},
{
id: 4,
}),
expect.objectContaining({
id: "4",
slug: "qux",
},
}),
]);
});
});
describe("getParameterWidgetTitle", () => {
it("should return a title for the given parameter", () => {
expect(getParameterWidgetTitle({ type: "string/starts-with" })).toEqual(
"Starts with…",
);
expect(
getParameterWidgetTitle(
createMockUiParameter({ type: "string/starts-with" }),
),
).toEqual("Starts with…");
expect(getParameterWidgetTitle({ type: "number/between" })).toEqual(
"Between…",
);
expect(
getParameterWidgetTitle(
createMockUiParameter({ type: "number/between" }),
),
).toEqual("Between…");
});
it("should not return a title for equal operator parameters", () => {
expect(getParameterWidgetTitle({ type: "string/=" })).toBeUndefined();
expect(getParameterWidgetTitle({ type: "number/=" })).toBeUndefined();
expect(
getParameterWidgetTitle(createMockUiParameter({ type: "string/=" })),
).toBeUndefined();
expect(
getParameterWidgetTitle(createMockUiParameter({ type: "number/=" })),
).toBeUndefined();
});
it("should default to undefined for parameters without operators", () => {
expect(getParameterWidgetTitle({ type: "category" })).toBeUndefined();
expect(
getParameterWidgetTitle(createMockUiParameter({ type: "category" })),
).toBeUndefined();
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment