Skip to content

Commit

Permalink
Change muiGridFilterToGql fallback operator to 'and' (#2061)
Browse files Browse the repository at this point in the history
MUI uses and per default, not or.

---------

Co-authored-by: Denise Buder <[email protected]>
Co-authored-by: Johannes Obermair <[email protected]>
Co-authored-by: Niko Sams <[email protected]>
  • Loading branch information
4 people authored May 14, 2024
1 parent 5c1ab80 commit a8a098a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fair-hats-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/admin": patch
---

muiGridFilterToGql: change fallback operator to 'and' to match MUI default
78 changes: 78 additions & 0 deletions packages/admin/admin/src/dataGrid/muiGridFilterToGql.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { GridColDef, GridLinkOperator } from "@mui/x-data-grid";
import { GridFilterModel } from "@mui/x-data-grid/models/gridFilterModel";

import { muiGridFilterToGql } from "./muiGridFilterToGql";

const columns: GridColDef<{ tag: string }>[] = [{ field: "tag" }];

const mockedFilterModelAndOperator: GridFilterModel = {
items: [
{
columnField: "tag",
operatorValue: "contains",
id: 1,
value: "de",
},
{
columnField: "tag",
operatorValue: "contains",
id: 2,
value: "en",
},
],
linkOperator: GridLinkOperator.And,
};

const mockedFilterModelOrOperator: GridFilterModel = {
items: [
{
columnField: "tag",
operatorValue: "contains",
id: 1,
value: "de",
},
{
columnField: "tag",
operatorValue: "contains",
id: 2,
value: "en",
},
],
linkOperator: GridLinkOperator.Or,
};
const mockedFilterModelWithoutOperator: GridFilterModel = {
items: [
{
columnField: "tag",
operatorValue: "contains",
id: 1,
value: "de",
},
{
columnField: "tag",
operatorValue: "contains",
id: 2,
value: "en",
},
],
};

describe("muiGridFilterToGql", () => {
it("should use correct and filter for specified and operator", () => {
const result = muiGridFilterToGql(columns, mockedFilterModelAndOperator);

expect(result).toEqual({ filter: { and: [{ tag: { contains: "de" } }, { tag: { contains: "en" } }] } });
});

it("should use correct or filter for specified or operator", () => {
const result = muiGridFilterToGql(columns, mockedFilterModelOrOperator);

expect(result).toEqual({ filter: { or: [{ tag: { contains: "de" } }, { tag: { contains: "en" } }] } });
});

it("should use correct and filter for no operator specified ", () => {
const result = muiGridFilterToGql(columns, mockedFilterModelWithoutOperator);

expect(result).toEqual({ filter: { and: [{ tag: { contains: "de" } }, { tag: { contains: "en" } }] } });
});
});
2 changes: 1 addition & 1 deletion packages/admin/admin/src/dataGrid/muiGridFilterToGql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function muiGridFilterToGql(columns: GridColDef[], filterModel?: GridFilt
};
});
const filter: GqlFilter = {};
const op: "and" | "or" = filterModel.linkOperator ?? "or";
const op: "and" | "or" = filterModel.linkOperator ?? "and";
filter[op] = filterItems;

let search: undefined | string = undefined;
Expand Down

0 comments on commit a8a098a

Please sign in to comment.