-
Notifications
You must be signed in to change notification settings - Fork 3
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
21 changed files
with
919 additions
and
55 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
58 changes: 58 additions & 0 deletions
58
spotlight-client/src/DemographicFilterSelect/DemographicFilterSelect.tsx
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,58 @@ | ||
// Recidiviz - a data platform for criminal justice reform | ||
// Copyright (C) 2021 Recidiviz, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// ============================================================================= | ||
|
||
import { action } from "mobx"; | ||
import { observer } from "mobx-react-lite"; | ||
import React from "react"; | ||
import HistoricalPopulationBreakdownMetric from "../contentModels/HistoricalPopulationBreakdownMetric"; | ||
import { DemographicView, isDemographicView } from "../metricsApi"; | ||
import { Dropdown } from "../UiLibrary"; | ||
|
||
type DemographicFilterOption = { id: DemographicView; label: string }; | ||
|
||
type DemographicFilterSelectProps = { | ||
metric: HistoricalPopulationBreakdownMetric; | ||
}; | ||
|
||
const DemographicFilterSelect: React.FC<DemographicFilterSelectProps> = ({ | ||
metric, | ||
}) => { | ||
const options: DemographicFilterOption[] = [ | ||
{ id: "total", label: "Total" }, | ||
{ id: "race", label: "Race" }, | ||
{ id: "gender", label: "Gender" }, | ||
{ id: "age", label: "Age" }, | ||
]; | ||
|
||
const onChange = action("change demographic filter", (newFilter: string) => { | ||
if (isDemographicView(newFilter)) { | ||
// eslint-disable-next-line no-param-reassign | ||
metric.demographicView = newFilter; | ||
} | ||
}); | ||
|
||
return ( | ||
<Dropdown | ||
label="View" | ||
onChange={onChange} | ||
options={options} | ||
selectedId={metric.demographicView} | ||
/> | ||
); | ||
}; | ||
|
||
export default observer(DemographicFilterSelect); |
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,18 @@ | ||
// Recidiviz - a data platform for criminal justice reform | ||
// Copyright (C) 2021 Recidiviz, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// ============================================================================= | ||
|
||
export { default } from "./DemographicFilterSelect"; |
109 changes: 109 additions & 0 deletions
109
spotlight-client/src/UiLibrary/Dropdown/Dropdown.test.tsx
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,109 @@ | ||
// Recidiviz - a data platform for criminal justice reform | ||
// Copyright (C) 2020 Recidiviz, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// ============================================================================= | ||
|
||
import { render, fireEvent, screen } from "@testing-library/react"; | ||
import React from "react"; | ||
import Dropdown from "./Dropdown"; | ||
|
||
const testLabel = "Test Label"; | ||
const mockOnChange = jest.fn(); | ||
const testOptions = [ | ||
{ id: "1", label: "option one" }, | ||
{ id: "2", label: "option two" }, | ||
{ id: "3", label: "option three" }, | ||
]; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test("select from menu", () => { | ||
render( | ||
<Dropdown | ||
label={testLabel} | ||
options={testOptions} | ||
onChange={mockOnChange} | ||
selectedId="1" | ||
/> | ||
); | ||
|
||
const menuButton = screen.getByRole("button", { | ||
name: `${testLabel} ${testOptions[0].label}`, | ||
}); | ||
|
||
screen | ||
.queryAllByRole("option") | ||
.forEach((option) => expect(option).not.toBeInTheDocument()); | ||
|
||
fireEvent.click(menuButton); | ||
|
||
testOptions.forEach((opt) => { | ||
expect(screen.getByRole("option", { name: opt.label })).toBeVisible(); | ||
}); | ||
|
||
const newOption = screen.getByRole("option", { name: testOptions[2].label }); | ||
fireEvent.click(newOption); | ||
|
||
expect(mockOnChange.mock.calls[0][0]).toBe(testOptions[2].id); | ||
}); | ||
|
||
test("selection prop updates menu", () => { | ||
const { rerender } = render( | ||
<Dropdown | ||
label={testLabel} | ||
options={testOptions} | ||
onChange={mockOnChange} | ||
selectedId="1" | ||
/> | ||
); | ||
|
||
const menuButton = screen.getByRole("button", { | ||
name: `${testLabel} ${testOptions[0].label}`, | ||
}); | ||
|
||
rerender( | ||
<Dropdown | ||
label={testLabel} | ||
options={testOptions} | ||
onChange={mockOnChange} | ||
selectedId="2" | ||
/> | ||
); | ||
|
||
expect(menuButton).toHaveTextContent(testOptions[1].label); | ||
}); | ||
|
||
test("can be disabled", () => { | ||
render( | ||
<Dropdown | ||
label={testLabel} | ||
options={testOptions} | ||
onChange={mockOnChange} | ||
selectedId="1" | ||
disabled | ||
/> | ||
); | ||
const menuButton = screen.getByRole("button", { | ||
name: `${testLabel} ${testOptions[0].label}`, | ||
}); | ||
expect(menuButton).toBeDisabled(); | ||
|
||
fireEvent.click(menuButton); | ||
screen | ||
.queryAllByRole("option") | ||
.forEach((option) => expect(option).not.toBeInTheDocument()); | ||
}); |
Oops, something went wrong.