Skip to content

Commit

Permalink
style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
charmingduchess committed Nov 8, 2023
1 parent 42bb608 commit e2f18cc
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 73 deletions.
129 changes: 127 additions & 2 deletions __test__/fixtures/testAggregations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const aggs = [
export const normalAggs = [
{
"@type": "nypl:Aggregation",
"@id": "res:location",
Expand Down Expand Up @@ -80,4 +80,129 @@ const aggs = [
},
]

export default aggs
export const aggsWithRepeatedValues = [
{
"@type": "nypl:Aggregation",
"@id": "res:location",
id: "location",
field: "location",
values: [
{
value: "loc:mym32",
count: 8,
label: "Performing Arts Research Collections - Music",
},
],
},
{
"@type": "nypl:Aggregation",
"@id": "res:format",
id: "format",
field: "format",
values: [],
},
{
"@type": "nypl:Aggregation",
"@id": "res:status",
id: "status",
field: "status",
values: [
{
value: "status:a",
count: 4,
label: "Available",
},
{
value: "status:a",
count: 4,
label: "Available ",
},
],
},
]

export const aggsWithMissingProperties = [
{
"@id": "res:location",
"@type": "nypl:Aggregation",
field: "location",
id: "location",
values: [
{
count: 4,
value: "loc:maj03",
label: "SASB M1 - General Research - Room 315",
},
{
count: 12,
label: "Offsite",
value: "loc:rc2ma",
},
{
count: 12,
label: "Off site",
value: "loc:rc2ma",
},
{
count: 12,
label: "Off-site",
value: "loc:rc2ma",
},
{
count: 12,
label: "off-site",
value: "loc:rc2ma",
},
{
count: 12,
label: "off site",
value: "loc:rc2ma",
},
{
count: 2,
value: "offsite",
label: "Offsite",
},
{
count: 2,
value: "blank",
label: "",
},
{
count: 2,
value: "blaaaank",
},
],
},
{
"@id": "res:format",
"@type": "nypl:Aggregation",
field: "format",
id: "format",
values: [
{
count: 12,
label: "Text",
value: "Text",
},
],
},
{
"@id": "res:status",
"@type": "nypl:Aggregation",
field: "status",
id: "status",
values: [
{
count: 12,
label: "Available",
value: "status:a",
},
{
count: 12,
label: "Not Available (ReCAP",
value: "status:na",
},
],
},
]
4 changes: 2 additions & 2 deletions pages/search/advanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import type {
import { getQueryString } from "../../src/utils/searchUtils"

import ItemFilterContainer from "../../src/components/ItemFilters/FiltersContainer"
import testItemAggs from "../../__test__/fixtures/testAggregations"
import { normalAggs } from "../../__test__/fixtures/testAggregations"

/**
* The Advanced Search page is responsible for displaying the Advanced Search form fields and
Expand Down Expand Up @@ -125,7 +125,7 @@ export default function AdvancedSearch() {
}
/>
)}
<ItemFilterContainer itemAggs={testItemAggs} />
<ItemFilterContainer itemAggs={normalAggs} />
<Heading level="two">Advanced Search</Heading>
<Form
id="advancedSearchForm"
Expand Down
4 changes: 2 additions & 2 deletions src/components/ItemFilters/ItemFilterButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ const ItemFilterButtons = ({
data-testid={`clear-${field}-button`}
key={`clear-${field}-button`}
buttonType="text"
id="clear-filter-button"
id={`clear-${field}-button`}
onClick={clearFilter}
>
Clear
</Button>
<Button
key={`apply-${field}-button`}
id="apply-filter-button"
id={`apply-${field}-button`}
onClick={applyFilter}
>
Apply
Expand Down
4 changes: 1 addition & 3 deletions src/components/ItemFilters/ItemFilterLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Button, Icon, Spacer } from "@nypl/design-system-react-components"
import type { Dispatch } from "react"

import styles from "../../../styles/components/ItemFilters.module.scss"
import type { Option } from "../../types/filterTypes"

interface ItemFilterLabelProps {
Expand All @@ -20,9 +19,8 @@ const ItemFilterLabel = ({
const fieldFormatted = field[0].toUpperCase() + field.substring(1)
return (
<Button
className={styles.itemFilterButton}
sx={{
borderColor: "var(--nypl-colors-ui-gray-medium)",
borderColor: "ui.gray.medium",
color: "black",
width: "100%",
}}
Expand Down
95 changes: 42 additions & 53 deletions src/components/ItemFilters/ItemFilters.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mockRouter from "next-router-mock"
import FiltersContainer from "./FiltersContainer"
import userEvent from "@testing-library/user-event"

import aggs from "../../../__test__/fixtures/testAggregations"
import { normalAggs } from "../../../__test__/fixtures/testAggregations"

// Mock next router
jest.mock("next/router", () => jest.requireActual("next-router-mock"))
Expand All @@ -16,12 +16,12 @@ describe("Filters container", () => {
})

it("renders a single filter", () => {
render(<FiltersContainer itemAggs={[aggs[0]]} />)
render(<FiltersContainer itemAggs={[normalAggs[0]]} />)
const filters = screen.getAllByTestId(/item-filter/)
expect(filters.length).toBe(1)
})
it("renders three filter boxes", () => {
render(<FiltersContainer itemAggs={aggs} />)
render(<FiltersContainer itemAggs={normalAggs} />)
const filters = screen.getAllByTestId(/item-filter/)
expect(filters.length).toBe(3)
})
Expand All @@ -32,15 +32,12 @@ describe("Filters container", () => {
item_format: "Text",
item_status: "status:a",
}
render(<FiltersContainer itemAggs={aggs} />)
const [
locationCheckboxGroupButton,
statusCheckboxGroupButton,
formatCheckboxGroupButton,
] = filterButtons()
await filterHasSelected(locationCheckboxGroupButton, ["Offsite"])
await filterHasSelected(formatCheckboxGroupButton, ["Text"])
await filterHasSelected(statusCheckboxGroupButton, ["Available"])
render(<FiltersContainer itemAggs={normalAggs} />)
const { locationFilterButton, statusFilterButton, formatFilterButton } =
filterButtons()
await filterHasSelected(locationFilterButton, ["Offsite"])
await filterHasSelected(formatFilterButton, ["Text"])
await filterHasSelected(statusFilterButton, ["Available"])
})

it("clears selection per filter", async () => {
Expand All @@ -49,64 +46,56 @@ describe("Filters container", () => {
item_format: "Text",
item_status: "status:a,status:na",
}
render(<FiltersContainer itemAggs={aggs} />)
const [
locationCheckboxGroupButton,
statusCheckboxGroupButton,
formatCheckboxGroupButton,
] = filterButtons()
render(<FiltersContainer itemAggs={normalAggs} />)
const { locationFilterButton, statusFilterButton, formatFilterButton } =
filterButtons()

// the values start selected
await filterHasSelected(locationCheckboxGroupButton, ["Offsite"])
await filterHasSelected(formatCheckboxGroupButton, ["Text"])
await filterHasSelected(statusCheckboxGroupButton, [
"Available",
"Not available",
])
await filterHasSelected(locationFilterButton, ["Offsite"])
await filterHasSelected(formatFilterButton, ["Text"])
await filterHasSelected(statusFilterButton, ["Available", "Not available"])
const clearStatusButton = screen.getByTestId("clear-status-button")

await act(async () => {
await userEvent.click(clearStatusButton)
// these filters should be unchanged
await filterHasSelected(locationCheckboxGroupButton, ["Offsite"])
await filterHasSelected(formatCheckboxGroupButton, ["Text"])
await filterHasSelected(locationFilterButton, ["Offsite"])
await filterHasSelected(formatFilterButton, ["Text"])

// Status values should be unchecked
await filterHasNotSelected(statusCheckboxGroupButton, [
await filterHasNotSelected(statusFilterButton, [
"Available",
"Not available",
])
})
})

it("does not persist selection if filter closes without applying", async () => {
render(<FiltersContainer itemAggs={aggs} />)
const [locationCheckboxGroupButton, statusCheckboxGroupButton] =
filterButtons()
render(<FiltersContainer itemAggs={normalAggs} />)
const { locationFilterButton, statusFilterButton } = filterButtons()
await act(async () => {
await userEvent.click(locationCheckboxGroupButton)
await userEvent.click(locationFilterButton)
const checkbox = screen.getAllByRole("checkbox")[0]
await userEvent.click(checkbox)

// clicking other filter label closes the location filter
await userEvent.click(statusCheckboxGroupButton)
await userEvent.click(locationCheckboxGroupButton)
await userEvent.click(statusFilterButton)
await userEvent.click(locationFilterButton)
const applyButton = screen.getByTestId("clear-location-button")
expect(applyButton).toBeDisabled()
})
})

it.only("persists previously applied selection after closing filter without applying", async () => {
it("persists previously applied selection after closing filter without applying", async () => {
mockRouter.query = {
item_location: "loc:rc2ma",
item_format: "Text",
item_status: "status:a,status:na",
}
render(<FiltersContainer itemAggs={aggs} />)
const [locationCheckboxGroupButton, statusCheckboxGroupButton] =
filterButtons()
render(<FiltersContainer itemAggs={normalAggs} />)
const { locationFilterButton, statusFilterButton } = filterButtons()
await act(async () => {
await userEvent.click(locationCheckboxGroupButton)
await userEvent.click(locationFilterButton)
const checkbox = screen.getByLabelText(/Main Reading Room/)
const offsiteCheckbox = screen.getByLabelText("Offsite")
// uncheck Offsite (set from query parsing)
Expand All @@ -116,18 +105,18 @@ describe("Filters container", () => {
// don't apply

// clicking other filter label closes the location filter
await userEvent.click(statusCheckboxGroupButton)
await userEvent.click(locationCheckboxGroupButton)
await userEvent.click(statusFilterButton)
await userEvent.click(locationFilterButton)

filterHasSelected(locationCheckboxGroupButton, ["Offsite"])
filterHasNotSelected(locationCheckboxGroupButton, [
filterHasSelected(locationFilterButton, ["Offsite"])
filterHasNotSelected(locationFilterButton, [
"Schwarzman Building - Main Reading Room 315",
])
})
})
it("closes open filters when user clicks outside of the filter", async () => {
render(<FiltersContainer itemAggs={aggs} />)
const [locationFilterButton] = filterButtons()
render(<FiltersContainer itemAggs={normalAggs} />)
const { locationFilterButton } = filterButtons()
const outsideOfTheFilter = screen.getByTestId("filter-text")
await act(async () => {
await userEvent.click(locationFilterButton)
Expand All @@ -152,15 +141,15 @@ const filterHasSelected = async (checkboxGroupButton, values: string[]) => {
}

const filterButtons = () => {
const locationCheckboxGroupButton = screen.getByTestId("location-item-filter")
const statusCheckboxGroupButton = screen.getByTestId("status-item-filter")
const formatCheckboxGroupButton = screen.getByTestId("format-item-filter")

return [
locationCheckboxGroupButton,
statusCheckboxGroupButton,
formatCheckboxGroupButton,
]
const locationFilterButton = screen.getByTestId("location-item-filter")
const statusFilterButton = screen.getByTestId("status-item-filter")
const formatFilterButton = screen.getByTestId("format-item-filter")

return {
locationFilterButton,
statusFilterButton,
formatFilterButton,
}
}

const filterHasNotSelected = async (checkboxGroupButton, values: string[]) => {
Expand Down
9 changes: 4 additions & 5 deletions src/utils/itemFilterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ export const buildItemFilterQueryParams = (
if (loc === "Offsite") return recapLocations
else return loc
})
const location_query = location.length
? "item_location=" + locs.join(",")
: ""
const location_query = locs.length ? "item_location=" + locs.join(",") : ""
const format_query = format.length ? "&item_format=" + format.join(",") : ""
const status_query = status.length ? "&item_status=" + status.join(",") : ""

const query = encodeURI(`?${location_query}${format_query}${status_query}`)
return query.length > 3 ? query : ""
const query = `${location_query}${format_query}${status_query}`
if (query.length) return encodeURI("?" + query)
else return ""
}

export const buildAppliedFiltersString = (
Expand Down
Loading

0 comments on commit e2f18cc

Please sign in to comment.