Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/default to past year filter #58

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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("<FileMetadataSearchBar />", () => {
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(
<Provider store={store}>
<FileMetadataSearchBar />
Expand All @@ -37,9 +43,34 @@ describe("<FileMetadataSearchBar />", () => {
).to.be.true;
});

it("renders with past year date filter by default", async () => {
aswallace marked this conversation as resolved.
Show resolved Hide resolved
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(
<Provider store={store}>
<FileMetadataSearchBar />
</Provider>
);
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(
<Provider store={store}>
<FileMetadataSearchBar />
Expand All @@ -48,7 +79,7 @@ describe("<FileMetadataSearchBar />", () => {
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);
Expand All @@ -57,7 +88,9 @@ describe("<FileMetadataSearchBar />", () => {
// 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;
});
Expand Down Expand Up @@ -129,8 +162,12 @@ describe("<FileMetadataSearchBar />", () => {

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(
<Provider store={store}>
<FileMetadataSearchBar />
</Provider>
Expand All @@ -147,18 +184,19 @@ describe("<FileMetadataSearchBar />", () => {
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();

// Assert
expect(
actions.includesMatch(
selection.actions.addFileFilter(
new FileFilter(AnnotationName.UPLOADED, expectedRange)
)
selection.actions.setFileFilters([
new FileFilter(AnnotationName.UPLOADED, expectedRange),
])
)
).to.be.true;
});
Expand Down
6 changes: 6 additions & 0 deletions packages/core/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export const RELATIVE_DATE_RANGES = [
sort: undefined,
},
];
export const INITIAL_DATE_FILTER = [
aswallace marked this conversation as resolved.
Show resolved Hide resolved
new FileFilter(
AnnotationName.UPLOADED,
`RANGE(${DATE_LAST_YEAR.toISOString()},${END_OF_TODAY.toISOString()})`
),
];

export const TOP_LEVEL_FILE_ANNOTATIONS = [
new Annotation({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/state/selection/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, INITIAL_DATE_FILTER } from "../../constants";
import Annotation from "../../entity/Annotation";
import FileFilter from "../../entity/FileFilter";
import FileFolder from "../../entity/FileFolder";
Expand Down Expand Up @@ -58,7 +58,7 @@ export const initialState = {
},
displayAnnotations: [],
fileSelection: new FileSelection(),
filters: [],
filters: [...INITIAL_DATE_FILTER],
openFileFolders: [],
shouldDisplaySmallFont: false,
sortColumn: new FileSort(AnnotationName.UPLOADED, SortOrder.DESC),
Expand Down
3 changes: 2 additions & 1 deletion packages/core/state/selection/test/logics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ describe("Selection logics", () => {
logics: selectionLogics,
state: initialState,
});
const currentFilters = initialState.selection.filters;

// act
const filter = new FileFilter("foo", 2);
Expand All @@ -804,7 +805,7 @@ describe("Selection logics", () => {
expect(
actions.includes({
type: SET_FILE_FILTERS,
payload: [filter],
payload: [filter, ...currentFilters],
})
).to.equal(true);
});
Expand Down
Loading