// (C) 2019 GoodData Corporation import { buildProject } from "../../../../schema/builder"; import { schema } from "../../../../schema/fixtures/dummySchema"; import { getFilteredAttributeElements } from "../headerItems"; import { ElementMap } from "../../../../schema/builders/ElementsBuilder"; const ATTR_ID = "2345"; const DF_ID = "2345.df"; function uriOf(attr: string, elementIndex: number): string { return `/gdc/md/mockproject/obj/${attr}/elements?id=${elementIndex}`; } function valuesOf(map: ElementMap, attr: string): string[] { return map.get(attr).map(e => e.element.title); } describe("getFilteredAttributeElements", () => { it("filters nothing if negative filter is empty", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { negativeAttributeFilter: { displayForm: { identifier: DF_ID, }, notIn: [], }, }, ]); expect(valuesOf(result, ATTR_ID)).toEqual(["Country 1", "Country 2", "Country 3", "Country 4"]); }); it("filters out all elements if positive filter is empty", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { positiveAttributeFilter: { displayForm: { identifier: DF_ID, }, in: [], }, }, ]); expect(valuesOf(result, ATTR_ID)).toEqual([]); }); it("filters out all but element selected on positive filter", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { positiveAttributeFilter: { displayForm: { identifier: DF_ID, }, in: [uriOf(ATTR_ID, 1)], }, }, ]); expect(valuesOf(result, ATTR_ID)).toEqual(["Country 1"]); }); it("filters out element selected on negative filter", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { negativeAttributeFilter: { displayForm: { identifier: DF_ID, }, notIn: [uriOf(ATTR_ID, 1)], }, }, ]); expect(valuesOf(result, ATTR_ID)).toEqual(["Country 2", "Country 3", "Country 4"]); }); it("filters out all but element selected by ref on positive filter", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { positiveAttributeFilter: { displayForm: { identifier: DF_ID, }, in: { uris: [uriOf(ATTR_ID, 1)] }, }, }, ]); expect(valuesOf(result, ATTR_ID)).toEqual(["Country 1"]); }); it("filters out element selected by ref on negative filter", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { negativeAttributeFilter: { displayForm: { identifier: DF_ID, }, notIn: { uris: [uriOf(ATTR_ID, 1)] }, }, }, ]); expect(valuesOf(result, ATTR_ID)).toEqual(["Country 2", "Country 3", "Country 4"]); }); it("filters out all but element by value selected on positive filter", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { positiveAttributeFilter: { displayForm: { identifier: DF_ID, }, in: { values: ["Country 1"] }, }, }, ]); expect(valuesOf(result, ATTR_ID)).toEqual(["Country 1"]); }); it("filters out element by value selected on negative filter", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { negativeAttributeFilter: { displayForm: { identifier: DF_ID, }, notIn: { values: ["Country 1"] }, }, }, ]); expect(valuesOf(result, ATTR_ID)).toEqual(["Country 2", "Country 3", "Country 4"]); }); it("applies value and URI filters with AND logic", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { negativeAttributeFilter: { displayForm: { identifier: DF_ID, }, notIn: { values: ["Country 1", "Country2"] }, }, }, // this leaves all elements except Country 1 and Country 2 { positiveAttributeFilter: { displayForm: { identifier: DF_ID, }, in: { uris: [uriOf(ATTR_ID, 3)] }, }, }, // out of those two filter out country 2 ]); expect(valuesOf(result, ATTR_ID)).toEqual(["Country 3"]); }); it("applies value and URI filters with AND logic - mutually exclusive filters", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { negativeAttributeFilter: { displayForm: { identifier: DF_ID, }, notIn: { values: ["Country 1", "Country 2"] }, }, }, // this leaves all elements except Country 1 and Country 2 { positiveAttributeFilter: { displayForm: { identifier: DF_ID, }, in: { uris: [uriOf(ATTR_ID, 2)] }, }, }, // out of those two filter out country 2 ]); expect(valuesOf(result, ATTR_ID)).toEqual([]); }); it("applies value filters with AND logic", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { negativeAttributeFilter: { displayForm: { identifier: DF_ID, }, notIn: { values: ["Country 1", "Country 2"] }, }, }, // this leaves all elements except Country 1 and Country 2 { positiveAttributeFilter: { displayForm: { identifier: DF_ID, }, in: { values: ["Country 3"] }, }, }, // out of those two filter out country 2 ]); expect(valuesOf(result, ATTR_ID)).toEqual(["Country 3"]); }); it("applies value filters with AND logic - mutually exclusive filters", () => { const result = getFilteredAttributeElements(buildProject(schema), [ { negativeAttributeFilter: { displayForm: { identifier: DF_ID, }, notIn: { values: ["Country 1", "Country 2"] }, }, }, // this leaves all elements except Country 1 and Country 2 { positiveAttributeFilter: { displayForm: { identifier: DF_ID, }, in: { values: ["Country 2"] }, }, }, ]); expect(valuesOf(result, ATTR_ID)).toEqual([]); }); });