-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(filter): adds ability to match only specific filter data propert…
…ies (#9541) **Related Issue:** #5063 ## Summary - add matchFields argument to filter utility - allows filtering fields at the first level of an object. - Nested object fields will not be filtered. - Could be enhanced to do so in the future by enhancing `matchFields` to be an object - add filter utility spec tests (thank you co-pilot) - add `matchFields` property to `filter` component - add e2e tests for filter
- Loading branch information
Showing
5 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { filter } from "./filter"; | ||
|
||
describe("filter function", () => { | ||
it("warns and returns empty array for empty data", () => { | ||
const spy = jest.spyOn(console, "warn"); | ||
const result = filter([], "test"); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(result).toEqual([]); | ||
spy.mockRestore(); | ||
}); | ||
|
||
it("returns empty array when no objects match", () => { | ||
const data = [{ name: "John" }, { name: "Jane" }]; | ||
const result = filter(data, "Doe"); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("returns matching objects", () => { | ||
const data = [{ name: "John" }, { name: "Jane" }]; | ||
const result = filter(data, "Jane"); | ||
expect(result).toEqual([{ name: "Jane" }]); | ||
}); | ||
|
||
it("considers only specified fields for matching", () => { | ||
const data = [ | ||
{ name: "John", age: 30 }, | ||
{ name: "Jane", age: 25 }, | ||
]; | ||
const result = filter(data, "25", ["age"]); | ||
expect(result).toEqual([{ name: "Jane", age: 25 }]); | ||
|
||
const result2 = filter(data, "John", ["age"]); | ||
expect(result2).toEqual([]); | ||
}); | ||
|
||
it("ignores functions and null values in objects", () => { | ||
const data = [{ name: "John", action: () => {}, value: null }]; | ||
const result = filter(data, "John"); | ||
expect(result).toEqual([{ name: "John", action: expect.any(Function), value: null }]); | ||
}); | ||
|
||
it("returns empty array when searching with 'null'", () => { | ||
const data = [{ name: "John", action: () => {}, value: null }]; | ||
const result = filter(data, "null"); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("searches nested objects correctly", () => { | ||
const data = [{ name: "John", details: { age: 30 } }]; | ||
const result = filter(data, "30"); | ||
expect(result).toEqual([{ name: "John", details: { age: 30 } }]); | ||
}); | ||
|
||
it("searches arrays in objects correctly", () => { | ||
const data = [{ name: "John", tags: ["developer", "tester"] }]; | ||
const result = filter(data, "tester"); | ||
expect(result).toEqual([{ name: "John", tags: ["developer", "tester"] }]); | ||
}); | ||
|
||
it("always includes objects with filterDisabled set to true", () => { | ||
const data = [{ name: "John" }, { name: "Jane", filterDisabled: true }]; | ||
const result = filter(data, "Doe"); | ||
expect(result).toEqual([{ name: "Jane", filterDisabled: true }]); | ||
}); | ||
|
||
it("always includes objects with constant set to true", () => { | ||
const data = [{ name: "John" }, { name: "Jane", constant: true }]; | ||
const result = filter(data, "Doe"); | ||
expect(result).toEqual([{ name: "Jane", constant: true }]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters