This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { TaskFilesQuery } from "gql/generated/types"; | ||
import { Unpacked } from "types/utils"; | ||
|
||
export type GroupedFiles = Unpacked< | ||
TaskFilesQuery["task"]["taskFiles"]["groupedFiles"] | ||
>; |
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,151 @@ | ||
import { filterGroupedFiles } from "./utils"; | ||
|
||
describe("filterGroupedFiles", () => { | ||
it("should return an empty array if groupedFiles is empty", () => { | ||
const groupedFiles = []; | ||
const search = ""; | ||
const result = filterGroupedFiles(groupedFiles, search); | ||
expect(result).toStrictEqual([]); | ||
}); | ||
it("should return the original array if search is empty", () => { | ||
const groupedFiles = [ | ||
{ | ||
taskName: "some_task_name", | ||
files: [ | ||
{ | ||
name: "some_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
]; | ||
const search = ""; | ||
const result = filterGroupedFiles(groupedFiles, search); | ||
expect(result).toStrictEqual(groupedFiles); | ||
}); | ||
it("should filter the array if search is not empty", () => { | ||
const groupedFiles = [ | ||
{ | ||
taskName: "some_task_name", | ||
files: [ | ||
{ | ||
name: "some_file_name", | ||
link: "some_url", | ||
}, | ||
{ | ||
name: "some_other_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
]; | ||
const search = "some_file_name"; | ||
const result = filterGroupedFiles(groupedFiles, search); | ||
expect(result).toStrictEqual([ | ||
{ | ||
taskName: "some_task_name", | ||
files: [ | ||
{ | ||
name: "some_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
]); | ||
}); | ||
it("should filter across multiple groups", () => { | ||
const groupedFiles = [ | ||
{ | ||
taskName: "some_task_name", | ||
files: [ | ||
{ | ||
name: "some_file_name", | ||
link: "some_url", | ||
}, | ||
{ | ||
name: "some_other_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
{ | ||
taskName: "some_other_task_name", | ||
files: [ | ||
{ | ||
name: "some_file_name", | ||
link: "some_url", | ||
}, | ||
{ | ||
name: "some_other_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
]; | ||
const search = "some_file_name"; | ||
const result = filterGroupedFiles(groupedFiles, search); | ||
expect(result).toStrictEqual([ | ||
{ | ||
taskName: "some_task_name", | ||
files: [ | ||
{ | ||
name: "some_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
{ | ||
taskName: "some_other_task_name", | ||
files: [ | ||
{ | ||
name: "some_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
]); | ||
}); | ||
it("should not return groups that have no matching files", () => { | ||
const groupedFiles = [ | ||
{ | ||
taskName: "some_task_name", | ||
files: [ | ||
{ | ||
name: "some_matching_file_name", | ||
link: "some_url", | ||
}, | ||
{ | ||
name: "some_other_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
{ | ||
taskName: "some_other_task_name", | ||
files: [ | ||
{ | ||
name: "some_file_name", | ||
link: "some_url", | ||
}, | ||
{ | ||
name: "some_other_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
]; | ||
const search = "some_matching_file_name"; | ||
const result = filterGroupedFiles(groupedFiles, search); | ||
expect(result).toStrictEqual([ | ||
{ | ||
taskName: "some_task_name", | ||
files: [ | ||
{ | ||
name: "some_matching_file_name", | ||
link: "some_url", | ||
}, | ||
], | ||
}, | ||
]); | ||
}); | ||
}); |
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,23 @@ | ||
import type { GroupedFiles } from "./types"; | ||
|
||
/** | ||
* `filterGroupedFiles` filters the groupedFiles array from the TaskFilesQuery | ||
* @param groupedFiles - the groupedFiles array from the TaskFilesQuery | ||
* @param search - the search string | ||
* @returns - a new array of groupedFiles that contain the search string | ||
*/ | ||
const filterGroupedFiles = (groupedFiles: GroupedFiles[], search: string) => | ||
groupedFiles.reduce((acc, groupedFile) => { | ||
const filteredFiles = groupedFile?.files?.filter((file) => | ||
file?.name?.toLowerCase().includes(search.toLowerCase()) | ||
); | ||
if (filteredFiles?.length) { | ||
acc.push({ | ||
...groupedFile, | ||
files: filteredFiles, | ||
}); | ||
} | ||
return acc; | ||
}, [] as GroupedFiles[]); | ||
|
||
export { filterGroupedFiles }; |