Skip to content

Commit

Permalink
Update filter operator as well, if needed. Add an additional test for…
Browse files Browse the repository at this point in the history
… this case.
  • Loading branch information
jonathangreen committed Mar 28, 2024
1 parent 0e696bc commit 1645480
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/components/AdvancedSearchFilterInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ export default function AdvancedSearchFilterInput({
setFilterKey(value);
const selected = fields.find((field) => field.value === value);

// Set the value to either the default option, or blank when changing the filter type
// Set the value to either the first option, or blank.
if (selected.options && selected.options.length) {
setFilterValue(selected.options[0]);
} else {
setFilterValue("");
}

// Update operator if the filter doesn't support it.
if (
selected.operators &&
selected.operators.length &&
!selected.operators.find((op) => op === filterOp)
) {
setFilterOp(selected.operators[0]);
}
};

const handleOpChange = () => {
Expand Down
37 changes: 37 additions & 0 deletions src/components/__tests__/AdvancedSearchFilterInput-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,43 @@ describe("AdvancedSearchFilterInput", () => {
expect(options).to.have.length(fictionField.operators.length);
});

it("should use the currently selected operator when changing filters", () => {
const sourceFilterButton = wrapper.find(
'input[type="radio"][value="data_source"]'
);
const fictionFilterButton = wrapper.find(
'input[type="radio"][value="fiction"]'
);
const fictionField = fields.find((element) => {
return element.value === "fiction";
});

const operatorOptions = wrapper.find(".filter-operator select");
operatorOptions.getDOMNode().value = "regex";
operatorOptions.simulate("change");

sourceFilterButton.getDOMNode().checked = true;
sourceFilterButton.simulate("change");

expect(operatorOptions.getDOMNode().value).to.equal("regex");

// unless the operator is not supported by the filter.
fictionFilterButton.getDOMNode().checked = true;
fictionFilterButton.simulate("change");

expect(operatorOptions.getDOMNode().value).to.equal(
fictionField.operators[0]
);

// the new operator will be persisted when changing back to source filter
sourceFilterButton.getDOMNode().checked = true;
sourceFilterButton.simulate("change");

expect(operatorOptions.getDOMNode().value).to.equal(
fictionField.operators[0]
);
});

it("should render a text input for value entry", () => {
const valueSelect = wrapper.find(".filter-value select");
const valueInput = wrapper.find('.filter-value input[type="text"]');
Expand Down

0 comments on commit 1645480

Please sign in to comment.