Tests for Condition and FilterElement (#4043)
* Filters testing * Filters testing
This commit is contained in:
parent
41fde64fc8
commit
8fb3b43295
4 changed files with 155 additions and 5 deletions
|
@ -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)
|
||||
});
|
||||
});
|
|
@ -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",
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
});
|
|
@ -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(),
|
||||
|
|
|
@ -97,7 +97,7 @@ export class UrlEntry {
|
|||
}
|
||||
|
||||
export class UrlToken {
|
||||
private constructor(
|
||||
constructor(
|
||||
public name: string,
|
||||
public value: string | string[],
|
||||
public type: TokenTypeValue,
|
||||
|
|
Loading…
Reference in a new issue