-
Notifications
You must be signed in to change notification settings - Fork 0
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
Include zero count active facets in facet options #79
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2f91568
remove enzyme from FacetDisplay test
ChristopherChudzicki cf37f49
automatically include zero-count active facets
ChristopherChudzicki a70a9ec
remove commented out import
ChristopherChudzicki 129f141
remove a console.log
ChristopherChudzicki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,200 @@ | ||
import React from "react" | ||
import { shallow } from "enzyme" | ||
import { render, screen, within } from "@testing-library/react" | ||
import user from "@testing-library/user-event" | ||
|
||
import { | ||
default as FacetDisplay, | ||
AvailableFacets, | ||
getDepartmentName, | ||
getLevelName | ||
} from "./FacetDisplay" | ||
import { FacetManifest, Facets } from "./types" | ||
import FacetDisplay, { getDepartmentName } from "./FacetDisplay" | ||
import type { FacetDisplayProps } from "./FacetDisplay" | ||
import { Bucket, FacetManifest, Facets } from "./types" | ||
|
||
describe("FacetDisplay component", () => { | ||
const facetMap: FacetManifest = [ | ||
const defaultFacetMap: FacetManifest = [ | ||
{ | ||
name: "topic", | ||
title: "Topics", | ||
useFilterableFacet: false, | ||
expandedOnLoad: false | ||
}, | ||
{ | ||
name: "resource_type", | ||
title: "Types", | ||
useFilterableFacet: false, | ||
expandedOnLoad: false | ||
expandedOnLoad: true | ||
}, | ||
{ | ||
name: "department", | ||
title: "Departments", | ||
useFilterableFacet: false, | ||
expandedOnLoad: true, | ||
labelFunction: getDepartmentName | ||
}, | ||
{ | ||
name: "level", | ||
title: "Level", | ||
useFilterableFacet: false, | ||
expandedOnLoad: true, | ||
labelFunction: getLevelName | ||
expandedOnLoad: true | ||
} | ||
] | ||
|
||
function setup() { | ||
const defaultAggregations = { | ||
topic: [ | ||
{ key: "Cats", doc_count: 10 }, | ||
{ key: "Dogs", doc_count: 20 }, | ||
{ key: "Monkeys", doc_count: 30 } | ||
], | ||
department: [ | ||
{ key: "1", doc_count: 100 }, | ||
{ key: "2", doc_count: 200 } | ||
] | ||
} | ||
|
||
const setup = ({ | ||
props, | ||
aggregations = defaultAggregations | ||
}: { | ||
props?: Partial<FacetDisplayProps> | ||
aggregations?: Record<string, Bucket[]> | ||
} = {}) => { | ||
const activeFacets = {} | ||
const facetOptions = jest.fn() | ||
const facetOptions = (group: string) => aggregations[group] ?? [] | ||
const clearAllFilters = jest.fn() | ||
const onFacetChange = jest.fn() | ||
|
||
const render = (props = {}) => | ||
shallow( | ||
const view = render( | ||
<FacetDisplay | ||
facetMap={defaultFacetMap} | ||
facetOptions={facetOptions} | ||
activeFacets={activeFacets} | ||
clearAllFilters={clearAllFilters} | ||
onFacetChange={onFacetChange} | ||
{...props} | ||
/> | ||
) | ||
const spies = { | ||
onFacetChange, | ||
clearAllFilters | ||
} | ||
const rerender = (newProps: Partial<FacetDisplayProps>) => { | ||
view.rerender( | ||
<FacetDisplay | ||
facetMap={facetMap} | ||
facetMap={defaultFacetMap} | ||
facetOptions={facetOptions} | ||
activeFacets={activeFacets} | ||
clearAllFilters={clearAllFilters} | ||
onFacetChange={onFacetChange} | ||
{...props} | ||
{...newProps} | ||
/> | ||
) | ||
return { render, clearAllFilters } | ||
} | ||
return { view, spies, rerender } | ||
} | ||
|
||
test("renders a FacetDisplay with expected FilterableFacets", async () => { | ||
const { render } = setup() | ||
const wrapper = render() | ||
const facets = wrapper.find(AvailableFacets).dive().children() | ||
expect(facets).toHaveLength(4) | ||
facets.map((facet, key) => { | ||
expect(facet.prop("name")).toBe(facetMap[key].name) | ||
expect(facet.prop("title")).toBe(facetMap[key].title) | ||
expect(facet.prop("expandedOnLoad")).toBe(facetMap[key].expandedOnLoad) | ||
test("Renders facets with expected titles", async () => { | ||
setup() | ||
screen.getByRole("button", { name: defaultFacetMap[0].title }) | ||
screen.getByRole("button", { name: defaultFacetMap[1].title }) | ||
}) | ||
|
||
test.each([{ expandedOnLoad: false }, { expandedOnLoad: true }])( | ||
"Initially expanded based on expandedOnLoad ($expandedOnLoad)", | ||
({ expandedOnLoad }) => { | ||
const facetMap = [{ ...defaultFacetMap[0], expandedOnLoad }] | ||
setup({ | ||
props: { facetMap } | ||
}) | ||
screen.getByRole("button", { | ||
name: facetMap[0].title, | ||
expanded: expandedOnLoad | ||
}) | ||
const checkboxes = screen.queryAllByRole("checkbox") | ||
expect(checkboxes).toHaveLength(expandedOnLoad ? 3 : 0) | ||
} | ||
) | ||
|
||
test("Clicking facet title toggles expanded state", async () => { | ||
setup({ | ||
props: { facetMap: [defaultFacetMap[0]] } | ||
}) | ||
const button = screen.getByRole("button", { | ||
name: defaultFacetMap[0].title | ||
}) | ||
const checkboxCount = () => screen.queryAllByRole("checkbox").length | ||
await user.click(button) | ||
expect(button.getAttribute("aria-expanded")).toBe("false") | ||
expect(checkboxCount()).toBe(0) | ||
await user.click(button) | ||
expect(button.getAttribute("aria-expanded")).toBe("true") | ||
expect(checkboxCount()).toBe(3) | ||
}) | ||
|
||
test("shows filters which are active", () => { | ||
test("Shows filters which are active", async () => { | ||
const activeFacets: Facets = { | ||
topic: ["Academic Writing", "Accounting", "Aerodynamics"], | ||
resource_type: [], | ||
department: ["1", "2"] | ||
topic: ["Cats", "Dogs"], | ||
department: ["1"] | ||
} | ||
const { spies } = setup({ props: { activeFacets } }) | ||
const activeDisplay = document.querySelector(".active-search-filters") | ||
if (!(activeDisplay instanceof HTMLElement)) { | ||
throw new Error("Expected activeDisplay to exist") | ||
} | ||
within(activeDisplay).getByText("Cats") | ||
within(activeDisplay).getByText("Dogs") | ||
within(activeDisplay).getByText("1") | ||
|
||
const { render, clearAllFilters } = setup() | ||
const wrapper = render({ | ||
activeFacets | ||
await user.click(screen.getByRole("button", { name: "Clear All" })) | ||
expect(spies.clearAllFilters).toHaveBeenCalled() | ||
await user.click(screen.getAllByRole("button", { name: "clear filter" })[0]) | ||
expect(spies.onFacetChange).toHaveBeenCalledWith("topic", "Cats", false) | ||
}) | ||
|
||
test("Clicking a facet checkbox calls onFacetChange", async () => { | ||
const { spies } = setup({ | ||
props: { | ||
facetMap: [ | ||
{ | ||
...defaultFacetMap[0], | ||
expandedOnLoad: true | ||
} | ||
] | ||
} | ||
}) | ||
expect( | ||
wrapper | ||
.find(".active-search-filters") | ||
.find("SearchFilter") | ||
.map(el => el.prop("value")) | ||
).toEqual(["Academic Writing", "Accounting", "Aerodynamics", "1", "2"]) | ||
wrapper.find(".clear-all-filters-button").simulate("click") | ||
expect(clearAllFilters).toHaveBeenCalled() | ||
await user.click(screen.getByRole("checkbox", { name: "Cats" })) | ||
expect(spies.onFacetChange).toHaveBeenCalledWith("topic", "Cats", true) | ||
}) | ||
|
||
test("it accepts a label function to convert codes to names", () => { | ||
setup({ | ||
props: { | ||
facetMap: [ | ||
{ | ||
...defaultFacetMap[1], | ||
labelFunction: getDepartmentName, | ||
expandedOnLoad: true | ||
} | ||
] | ||
} | ||
}) | ||
screen.getByRole("checkbox", { | ||
name: "Civil and Environmental Engineering" | ||
}) | ||
screen.getByRole("checkbox", { | ||
name: "Mechanical Engineering" | ||
}) | ||
}) | ||
|
||
test("Automically includes a zero-count option for active facets with no matches", () => { | ||
const activeFacets: Facets = { | ||
topic: [], | ||
resource_type: [], | ||
department: ["1"], | ||
level: ["graduate"] | ||
topic: ["Cats"] | ||
} | ||
|
||
const { render, clearAllFilters } = setup() | ||
const wrapper = render({ | ||
activeFacets | ||
const { rerender } = setup({ | ||
props: { | ||
activeFacets, | ||
facetMap: [ | ||
{ | ||
...defaultFacetMap[0], | ||
expandedOnLoad: true | ||
} | ||
] | ||
} | ||
}) | ||
const checkboxCount = () => screen.getAllByRole("checkbox").length | ||
expect(checkboxCount()).toBe(3) | ||
screen.getByRole("checkbox", { name: "Cats" }) | ||
screen.getByRole("checkbox", { name: "Dogs" }) | ||
screen.getByRole("checkbox", { name: "Monkeys" }) | ||
rerender({ | ||
activeFacets: { | ||
topic: ["Cats", "Dragons"] | ||
} | ||
}) | ||
expect( | ||
wrapper | ||
.find(".active-search-filters") | ||
.find("SearchFilter") | ||
.map(el => | ||
el.render().find(".active-search-filter-label").first().html() | ||
) | ||
).toEqual(["Civil and Environmental Engineering", "Graduate"]) | ||
wrapper.find(".clear-all-filters-button").simulate("click") | ||
expect(clearAllFilters).toHaveBeenCalled() | ||
expect(checkboxCount()).toBe(4) | ||
screen.getByRole("checkbox", { name: "Dragons" }) | ||
}) | ||
}) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The vast majority of the line changes in this PR are from removing enzyme in this test file, and adding
@testing-library/react
,@testing-library/user-event
.I was going to add more tests in this file, and I did not want to add enzyme tests since they are fragile / don't work with React 17, 18. (In previous PR, I had to alter the test file because I extracted
AvailableFacets
fromFacetDisplay
https://github.com/mitodl/course-search-utils/pull/78/files#diff-0b2ec90271aa9923bd1a4e5747946849aa084238d325cc529cda4848f503b8a3). So I rewrote the enzyme tests with@testing-library
.I also added some tests... E.g., I didn't see tests for clicking the expand/collapse buttons.