From 8c530768a4b19e229fb35caf1cd0caf35d5f6a15 Mon Sep 17 00:00:00 2001 From: Anya Wallace Date: Wed, 14 Feb 2024 15:48:03 -0800 Subject: [PATCH 1/4] give initial state a 1 year filter by default --- packages/core/state/selection/reducer.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/core/state/selection/reducer.ts b/packages/core/state/selection/reducer.ts index 69e6860cf..fda0fe941 100644 --- a/packages/core/state/selection/reducer.ts +++ b/packages/core/state/selection/reducer.ts @@ -2,7 +2,7 @@ import { makeReducer } from "@aics/redux-utils"; import { castArray, difference, omit, without } from "lodash"; import interaction from "../interaction"; -import { AnnotationName } from "../../constants"; +import { AnnotationName, RELATIVE_DATE_RANGES } from "../../constants"; import Annotation from "../../entity/Annotation"; import FileFilter from "../../entity/FileFilter"; import FileFolder from "../../entity/FileFolder"; @@ -46,6 +46,10 @@ export interface SelectionStateBranch { tutorial?: Tutorial; } +const initialDateFilter = RELATIVE_DATE_RANGES.filter((relativeDateRange) => + relativeDateRange.name.toLowerCase().includes("last year") +)[0].filters; + export const initialState = { annotationHierarchy: [], availableAnnotationsForHierarchy: [], @@ -58,7 +62,7 @@ export const initialState = { }, displayAnnotations: [], fileSelection: new FileSelection(), - filters: [], + filters: [...initialDateFilter], openFileFolders: [], shouldDisplaySmallFont: false, sortColumn: new FileSort(AnnotationName.UPLOADED, SortOrder.DESC), From 1d147c164a3682af048fb58ec9d1de5ca8f38e2d Mon Sep 17 00:00:00 2001 From: Anya Wallace Date: Wed, 14 Feb 2024 15:49:24 -0800 Subject: [PATCH 2/4] update unit tests to account for default year filter --- .../test/FileMetadataSearchBar.test.tsx | 64 +++++++++++++++---- .../core/state/selection/test/logics.test.ts | 3 +- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/packages/core/components/FileMetadataSearchBar/test/FileMetadataSearchBar.test.tsx b/packages/core/components/FileMetadataSearchBar/test/FileMetadataSearchBar.test.tsx index 6a43d579d..86438ae00 100644 --- a/packages/core/components/FileMetadataSearchBar/test/FileMetadataSearchBar.test.tsx +++ b/packages/core/components/FileMetadataSearchBar/test/FileMetadataSearchBar.test.tsx @@ -7,14 +7,20 @@ import { Provider } from "react-redux"; import FileMetadataSearchBar, { extractDateFromDateString } from "../"; import FileFilter from "../../../entity/FileFilter"; import { AnnotationName, TOP_LEVEL_FILE_ANNOTATIONS } from "../../../constants"; -import { initialState, selection } from "../../../state"; +import { initialState, reducer, reduxLogics, selection } from "../../../state"; describe("", () => { const ENTER_KEY = { keyCode: 13 }; - it("submits default file attribute when input is typed and submitted", async () => { + it("submits default file attribute when input is typed and submitted with no filters applied", async () => { // Arrange - const { actions, logicMiddleware, store } = configureMockStore({ state: initialState }); + const state = { + ...initialState, + selection: { + filters: [], + }, + }; + const { actions, logicMiddleware, store } = configureMockStore({ state }); const { getByRole } = render( @@ -37,9 +43,34 @@ describe("", () => { ).to.be.true; }); + it("renders with past year date filter by default", async () => { + const { store } = configureMockStore({ state: initialState }); + const dateUpper = new Date(); + const dateLower = new Date(); + const upperYear = dateUpper.getFullYear(); + dateLower.setFullYear(upperYear - 1); + const upperDateString = dateUpper.toDateString(); + const lowerDateString = dateLower.toDateString(); + const { getByText } = render( + + + + ); + const uploadedDisplayName = + TOP_LEVEL_FILE_ANNOTATIONS.find((a) => a.name === AnnotationName.UPLOADED) + ?.displayName || ""; + expect(getByText(uploadedDisplayName)).to.not.be.empty; + expect(getByText(upperDateString)).to.not.be.empty; + expect(getByText(lowerDateString)).to.not.be.empty; + }); + it("submits newly chosen file attribute when input is typed and submitted", async () => { // Arrange - const { actions, logicMiddleware, store } = configureMockStore({ state: initialState }); + const { actions, logicMiddleware, store } = configureMockStore({ + state: initialState, + reducer, + logics: reduxLogics, + }); const { getByRole, getByText } = render( @@ -48,7 +79,7 @@ describe("", () => { const searchQuery = "21304404.czi"; // Act - fireEvent.click(getByText("File name")); + fireEvent.click(getByText("Uploaded")); fireEvent.click(getByText("File ID")); fireEvent.change(getByRole("searchbox"), { target: { value: searchQuery } }); fireEvent.keyDown(getByRole("searchbox"), ENTER_KEY); @@ -57,7 +88,9 @@ describe("", () => { // Assert expect( actions.includesMatch( - selection.actions.addFileFilter(new FileFilter(AnnotationName.FILE_ID, searchQuery)) + selection.actions.setFileFilters([ + new FileFilter(AnnotationName.FILE_ID, searchQuery), + ]) ) ).to.be.true; }); @@ -129,8 +162,12 @@ describe("", () => { it("creates RANGE() file filter of RANGE(day,day+1) when only start date is selected", async () => { // Arrange - const { actions, logicMiddleware, store } = configureMockStore({ state: initialState }); - const { getByText } = render( + const { actions, logicMiddleware, store } = configureMockStore({ + state: initialState, + reducer, + logics: reduxLogics, + }); + const { getByText, getAllByRole, getByRole } = render( @@ -147,8 +184,9 @@ describe("", () => { const expectedRange = `RANGE(${startDate.toISOString()},${endDate.toISOString()})`; // Act - fireEvent.click(getByText("File name")); - fireEvent.click(getByText("Uploaded")); + const dropdownComponent = getAllByRole("combobox").at(0) as HTMLElement; + fireEvent.click(dropdownComponent); + fireEvent.click(getByRole("option", { name: "Uploaded" })); fireEvent.click(getByText("Start of date range")); fireEvent.click(getByText(day)); await logicMiddleware.whenComplete(); @@ -156,9 +194,9 @@ describe("", () => { // Assert expect( actions.includesMatch( - selection.actions.addFileFilter( - new FileFilter(AnnotationName.UPLOADED, expectedRange) - ) + selection.actions.setFileFilters([ + new FileFilter(AnnotationName.UPLOADED, expectedRange), + ]) ) ).to.be.true; }); diff --git a/packages/core/state/selection/test/logics.test.ts b/packages/core/state/selection/test/logics.test.ts index 29242d8b7..c94ea5923 100644 --- a/packages/core/state/selection/test/logics.test.ts +++ b/packages/core/state/selection/test/logics.test.ts @@ -794,6 +794,7 @@ describe("Selection logics", () => { logics: selectionLogics, state: initialState, }); + const currentFilters = initialState.selection.filters; // act const filter = new FileFilter("foo", 2); @@ -804,7 +805,7 @@ describe("Selection logics", () => { expect( actions.includes({ type: SET_FILE_FILTERS, - payload: [filter], + payload: [filter, ...currentFilters], }) ).to.equal(true); }); From 61b6d6184b5a53177a8c4dbd946cf23c434a9da4 Mon Sep 17 00:00:00 2001 From: Anya Wallace Date: Wed, 21 Feb 2024 15:57:05 -0800 Subject: [PATCH 3/4] convert initial date range into a constant --- packages/core/constants/index.ts | 6 ++++++ packages/core/state/selection/reducer.ts | 8 ++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/core/constants/index.ts b/packages/core/constants/index.ts index 026a0fb46..9a1442e6b 100644 --- a/packages/core/constants/index.ts +++ b/packages/core/constants/index.ts @@ -106,6 +106,12 @@ export const RELATIVE_DATE_RANGES = [ sort: undefined, }, ]; +export const INITIAL_DATE_FILTER = [ + new FileFilter( + AnnotationName.UPLOADED, + `RANGE(${DATE_LAST_YEAR.toISOString()},${END_OF_TODAY.toISOString()})` + ), +]; export const TOP_LEVEL_FILE_ANNOTATIONS = [ new Annotation({ diff --git a/packages/core/state/selection/reducer.ts b/packages/core/state/selection/reducer.ts index fda0fe941..e885eb595 100644 --- a/packages/core/state/selection/reducer.ts +++ b/packages/core/state/selection/reducer.ts @@ -2,7 +2,7 @@ import { makeReducer } from "@aics/redux-utils"; import { castArray, difference, omit, without } from "lodash"; import interaction from "../interaction"; -import { AnnotationName, RELATIVE_DATE_RANGES } from "../../constants"; +import { AnnotationName, INITIAL_DATE_FILTER } from "../../constants"; import Annotation from "../../entity/Annotation"; import FileFilter from "../../entity/FileFilter"; import FileFolder from "../../entity/FileFolder"; @@ -46,10 +46,6 @@ export interface SelectionStateBranch { tutorial?: Tutorial; } -const initialDateFilter = RELATIVE_DATE_RANGES.filter((relativeDateRange) => - relativeDateRange.name.toLowerCase().includes("last year") -)[0].filters; - export const initialState = { annotationHierarchy: [], availableAnnotationsForHierarchy: [], @@ -62,7 +58,7 @@ export const initialState = { }, displayAnnotations: [], fileSelection: new FileSelection(), - filters: [...initialDateFilter], + filters: [...INITIAL_DATE_FILTER], openFileFolders: [], shouldDisplaySmallFont: false, sortColumn: new FileSort(AnnotationName.UPLOADED, SortOrder.DESC), From 15f3dbbb87ecdfe6459c1c3fc69d817be524a38e Mon Sep 17 00:00:00 2001 From: Anya Wallace Date: Thu, 22 Feb 2024 11:22:45 -0800 Subject: [PATCH 4/4] update past year filter constant --- packages/core/constants/index.ts | 17 +++++------------ packages/core/state/selection/reducer.ts | 4 ++-- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/core/constants/index.ts b/packages/core/constants/index.ts index 9a1442e6b..e875bd763 100644 --- a/packages/core/constants/index.ts +++ b/packages/core/constants/index.ts @@ -38,17 +38,16 @@ const DATE_LAST_MONTH = new Date(BEGINNING_OF_TODAY); DATE_LAST_MONTH.setMonth(BEGINNING_OF_TODAY.getMonth() - 1); const DATE_LAST_WEEK = new Date(BEGINNING_OF_TODAY); DATE_LAST_WEEK.setDate(BEGINNING_OF_TODAY.getDate() - 7); +export const PAST_YEAR_FILTER = new FileFilter( + AnnotationName.UPLOADED, + `RANGE(${DATE_LAST_YEAR.toISOString()},${END_OF_TODAY.toISOString()})` +); export const RELATIVE_DATE_RANGES = [ { name: "Uploaded in last year", description: "This will automatically filter files down to those uploaded within the last year", - filters: [ - new FileFilter( - AnnotationName.UPLOADED, - `RANGE(${DATE_LAST_YEAR.toISOString()},${END_OF_TODAY.toISOString()})` - ), - ], + filters: [PAST_YEAR_FILTER], hierarchy: undefined, sort: undefined, }, @@ -106,12 +105,6 @@ export const RELATIVE_DATE_RANGES = [ sort: undefined, }, ]; -export const INITIAL_DATE_FILTER = [ - new FileFilter( - AnnotationName.UPLOADED, - `RANGE(${DATE_LAST_YEAR.toISOString()},${END_OF_TODAY.toISOString()})` - ), -]; export const TOP_LEVEL_FILE_ANNOTATIONS = [ new Annotation({ diff --git a/packages/core/state/selection/reducer.ts b/packages/core/state/selection/reducer.ts index e885eb595..0148d01a2 100644 --- a/packages/core/state/selection/reducer.ts +++ b/packages/core/state/selection/reducer.ts @@ -2,7 +2,7 @@ import { makeReducer } from "@aics/redux-utils"; import { castArray, difference, omit, without } from "lodash"; import interaction from "../interaction"; -import { AnnotationName, INITIAL_DATE_FILTER } from "../../constants"; +import { AnnotationName, PAST_YEAR_FILTER } from "../../constants"; import Annotation from "../../entity/Annotation"; import FileFilter from "../../entity/FileFilter"; import FileFolder from "../../entity/FileFolder"; @@ -58,7 +58,7 @@ export const initialState = { }, displayAnnotations: [], fileSelection: new FileSelection(), - filters: [...INITIAL_DATE_FILTER], + filters: [PAST_YEAR_FILTER], openFileFolders: [], shouldDisplaySmallFont: false, sortColumn: new FileSort(AnnotationName.UPLOADED, SortOrder.DESC),