diff --git a/src/components/ConditionalFilter/FilterElement/Condition.test.ts b/src/components/ConditionalFilter/FilterElement/Condition.test.ts new file mode 100644 index 000000000..17d5d49e8 --- /dev/null +++ b/src/components/ConditionalFilter/FilterElement/Condition.test.ts @@ -0,0 +1,99 @@ +import { InitialStateResponse } from "../API/InitialStateResponse"; +import { STATIC_CONDITIONS } from "../constants"; +import { UrlToken } from "../ValueProvider/UrlToken"; +import { Condition } from "./Condition"; + +describe("ConditionalFilter / FilterElement / Condition", () => { + it("creates empty condition", () => { + // Arrange + const condition = Condition.createEmpty() + + // Act & Assert + expect(condition.isEmpty()).toBeTruthy() + expect(condition.options).toEqual([]) + }); + + it("creates empty for given slug", () => { + // Arrange + const condition = Condition.emptyFromSlug("category") + + // Act & Assert + expect(condition.isEmpty()).toBeTruthy() + expect(condition.options).toEqual(STATIC_CONDITIONS.category) + }); + + + // Arrange + it.each([ + { + token: new UrlToken("category", ["cat1"], "s", "is"), + response: new InitialStateResponse([{ + label: "Cat1", + value: "cat-1-id", + slug: "cat1" + }]), + expected: { + options: [ + { type: 'combobox', label: 'is', value: 'input-1' }, + { type: 'multiselect', label: 'in', value: 'input-2' } + ], + selected: { + value: { label: 'Cat1', value: 'cat-1-id', slug: 'cat1' }, + conditionValue: { type: 'combobox', label: 'is', value: 'input-1' }, + options: [], + loading: false + }, + loading: false + } + }, + { + token: new UrlToken("some-attr1", ["some-attr-1z"], "m", "in"), + response: new InitialStateResponse( + [], + { + "some-attr1": { + choices: [{ + label: "Some attr 1z", + value: "some-attr-1z", + slug: "some-attr-1z", + originalSlug: "some-attr-1z", + }], + inputType: "MULTISELECT", + label: "Some attr 1", + slug: "some-attr1", + value: "some-attr1" + } + } + ), + expected: { + options: [{ + label: "in", + type: "multiselect", + value: "input-2", + }], + selected: { + conditionValue: { + label: "in", + type: "multiselect", + value: "input-2" + }, + value: [{ + label: "Some attr 1z", + originalSlug: "some-attr-1z", + slug: "some-attr-1z", + value: "some-attr-1z" + }], + options: [], + loading: false, + }, + loading: false + } + } + ])("creates instance from url token", ({ token, response, expected }) => { + // Act + const condition = Condition.fromUrlToken(token, response) + + // Assert + expect(condition).toEqual(expected) + }); +}); diff --git a/src/components/ConditionalFilter/FilterElement/FilterElement.test.ts b/src/components/ConditionalFilter/FilterElement/FilterElement.test.ts new file mode 100644 index 000000000..2c34098aa --- /dev/null +++ b/src/components/ConditionalFilter/FilterElement/FilterElement.test.ts @@ -0,0 +1,55 @@ +import { FilterElement } from "./FilterElement"; + +describe("ConditionalFilter / FilterElement / FilterElement", () => { + it("creates empty filter element", () => { + // Arrange + const element = FilterElement.createEmpty() + + // Act & Assert + expect(element.isEmpty()).toBeTruthy() + }); + + + it("creates for slug", () => { + // Arrange + const element = FilterElement.createStaticBySlug("category") + + // Act & Assert + expect(element).toEqual({ + condition: { + loading: false, + options: [ + { + label: "is", + type: "combobox", + value: "input-1", + }, + { + label: "in", + type: "multiselect", + value: "input-2", + }, + ], + selected: { + conditionValue: { + label: "is", + type: "combobox", + value: "input-1", + }, + loading: false, + options: [], + value: "", + }, + }, + constraint: undefined, + loading: false, + value: { + label: "Category", + type: "category", + value: "category", + }, + }); + }) + + +}); diff --git a/src/components/ConditionalFilter/FilterElement/FilterElement.ts b/src/components/ConditionalFilter/FilterElement/FilterElement.ts index 192cf56e4..c50c3ea77 100644 --- a/src/components/ConditionalFilter/FilterElement/FilterElement.ts +++ b/src/components/ConditionalFilter/FilterElement/FilterElement.ts @@ -177,10 +177,6 @@ export class FilterElement { ); } - public static fromValueEntry(valueEntry: any) { - return new FilterElement(valueEntry.value, valueEntry.condition, false); - } - public static createEmpty() { return new FilterElement( ExpressionValue.emptyStatic(), diff --git a/src/components/ConditionalFilter/ValueProvider/UrlToken.ts b/src/components/ConditionalFilter/ValueProvider/UrlToken.ts index 061b28649..518e9bc70 100644 --- a/src/components/ConditionalFilter/ValueProvider/UrlToken.ts +++ b/src/components/ConditionalFilter/ValueProvider/UrlToken.ts @@ -97,7 +97,7 @@ export class UrlEntry { } export class UrlToken { - private constructor( + constructor( public name: string, public value: string | string[], public type: TokenTypeValue,