-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide unit tests for new form components
- Loading branch information
Showing
4 changed files
with
206 additions
and
2 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
69 changes: 69 additions & 0 deletions
69
packages/core/components/DateRangePicker/test/DateRangePicker.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,69 @@ | ||
import { render, fireEvent } from "@testing-library/react"; | ||
import { expect } from "chai"; | ||
import { noop } from "lodash"; | ||
import * as React from "react"; | ||
import sinon from "sinon"; | ||
|
||
import DateRangePicker from ".."; | ||
import FileFilter from "../../../entity/FileFilter"; | ||
|
||
describe("<RangePicker />", () => { | ||
it("renders inputs for start and end dates with selectable date pickers", () => { | ||
// Arrange | ||
const onSearch = sinon.spy(); | ||
const { getAllByLabelText, getAllByRole, getByLabelText, getByRole } = render( | ||
<DateRangePicker onSearch={onSearch} onReset={noop} currentRange={undefined} /> | ||
); | ||
|
||
// Should render both input fields | ||
expect(getAllByRole("combobox").length).to.equal(2); | ||
expect(getAllByLabelText(/start/).length).to.equal(1); | ||
expect(getAllByLabelText(/end/).length).to.equal(1); | ||
|
||
// Select a start date | ||
expect(onSearch.called).to.equal(false); | ||
fireEvent.click(getByLabelText(/start/)); | ||
fireEvent.click(getByRole("button", { name: /^18,\s/ })); | ||
expect(onSearch.called).to.equal(true); | ||
|
||
// Reset spy to isolate assertions) | ||
onSearch.resetHistory(); | ||
|
||
// Select an end date | ||
expect(onSearch.called).to.equal(false); | ||
fireEvent.click(getByLabelText(/end/)); | ||
fireEvent.click(getByRole("button", { name: /^20,\s/ })); | ||
expect(onSearch.called).to.equal(true); | ||
}); | ||
|
||
it("initializes to values passed through props if provided", () => { | ||
// Arrange | ||
const currentRange = new FileFilter( | ||
"date", | ||
`RANGE(2024-02-21T00:00:00.000Z,2024-03-21T00:00:00.000Z)` | ||
); | ||
const { getByText } = render( | ||
<DateRangePicker onSearch={noop} onReset={noop} currentRange={currentRange} /> | ||
); | ||
|
||
expect(getByText("Wed Feb 21 2024")).to.exist; | ||
// We currently subtract a day to account for exclusive upper date range | ||
expect(getByText("Wed Mar 20 2024")).to.exist; | ||
}); | ||
|
||
it("renders a 'Clear' button if given a callback", () => { | ||
// Arrange | ||
const onSearch = noop; | ||
const onReset = sinon.spy(); | ||
|
||
// Act / Assert | ||
const { getByTitle } = render( | ||
<DateRangePicker onSearch={onSearch} onReset={onReset} currentRange={undefined} /> | ||
); | ||
|
||
// Hit reset | ||
expect(onReset.called).to.equal(false); | ||
fireEvent.click(getByTitle("Clear")); | ||
expect(onReset.called).to.equal(true); | ||
}); | ||
}); |
135 changes: 135 additions & 0 deletions
135
packages/core/components/RangePicker/test/RangePicker.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,135 @@ | ||
import { render, fireEvent, screen } from "@testing-library/react"; | ||
import { expect } from "chai"; | ||
import { noop } from "lodash"; | ||
import * as React from "react"; | ||
import sinon from "sinon"; | ||
|
||
import RangePicker, { ListItem } from ".."; | ||
import FileFilter from "../../../entity/FileFilter"; | ||
|
||
describe("<RangePicker />", () => { | ||
it("renders input fields for min and max values initialized to overall min/max", () => { | ||
// Arrange | ||
const items: ListItem[] = ["0", "20"].map((val) => ({ | ||
displayValue: val, | ||
value: val, | ||
})); | ||
|
||
const { getAllByTitle } = render( | ||
<RangePicker items={items} onSearch={noop} onReset={noop} currentRange={undefined} /> | ||
); | ||
|
||
// Should render both input fields | ||
expect(getAllByTitle(/^Min/).length).to.equal(1); | ||
expect(getAllByTitle(/^Max/).length).to.equal(1); | ||
|
||
// Should initialize to min and max item provided, respectively | ||
expect(screen.getByTitle<HTMLInputElement>(/^Min/).value).to.equal("0"); | ||
expect(screen.getByTitle<HTMLInputElement>(/^Max/).value).to.equal("20"); | ||
}); | ||
|
||
it("initializes to values passed through props if provided", () => { | ||
// Arrange | ||
const currentRange = new FileFilter("foo", "RANGE(0, 12.34)"); | ||
const items: ListItem[] = ["-20", "20"].map((val) => ({ | ||
displayValue: val, | ||
value: val, | ||
})); | ||
|
||
render( | ||
<RangePicker items={items} onSearch={noop} onReset={noop} currentRange={currentRange} /> | ||
); | ||
|
||
// Should initialize to min and max item provided, respectively | ||
expect(screen.getByTitle<HTMLInputElement>(/^Min/).value).to.equal("0"); | ||
expect(screen.getByTitle<HTMLInputElement>(/^Max/).value).to.equal("12.34"); | ||
}); | ||
|
||
it("renders a 'Reset' button if given a callback", () => { | ||
// Arrange | ||
const onSearch = noop; | ||
const onReset = sinon.spy(); | ||
const items: ListItem[] = ["0", "20"].map((val) => ({ | ||
displayValue: val, | ||
value: val, | ||
})); | ||
|
||
// Act / Assert | ||
const { getByText } = render( | ||
<RangePicker | ||
items={items} | ||
onSearch={onSearch} | ||
onReset={onReset} | ||
currentRange={undefined} | ||
/> | ||
); | ||
|
||
// Sanity check | ||
expect(screen.getByTitle<HTMLInputElement>(/^Min/).value).to.equal("0"); | ||
expect(screen.getByTitle<HTMLInputElement>(/^Max/).value).to.equal("20"); | ||
|
||
// Hit reset | ||
expect(onReset.called).to.equal(false); | ||
fireEvent.click(getByText("Reset")); | ||
expect(onReset.called).to.equal(true); | ||
|
||
// Should clear min and max values | ||
expect(screen.getByTitle<HTMLInputElement>(/^Min/).value).to.equal(""); | ||
expect(screen.getByTitle<HTMLInputElement>(/^Max/).value).to.equal(""); | ||
}); | ||
|
||
it("renders a 'Select Full Range' button that updates min and max values", () => { | ||
// Arrange | ||
const items: ListItem[] = ["0", "20"].map((val) => ({ | ||
selected: true, // start with all items selected | ||
displayValue: val, | ||
value: val, | ||
})); | ||
const { getByTitle, getByText } = render( | ||
<RangePicker items={items} onReset={noop} onSearch={noop} currentRange={undefined} /> | ||
); | ||
|
||
// Enter values | ||
fireEvent.change(getByTitle(/^Min/), { | ||
target: { | ||
value: 5, | ||
}, | ||
}); | ||
fireEvent.change(getByTitle(/^Max/), { | ||
target: { | ||
value: 10, | ||
}, | ||
}); | ||
|
||
// Sanity check | ||
expect(screen.getByTitle<HTMLInputElement>(/^Min/).value).to.equal("5"); | ||
expect(screen.getByTitle<HTMLInputElement>(/^Max/).value).to.equal("10"); | ||
|
||
// Act | ||
fireEvent.click(getByText("Select Full Range")); | ||
|
||
// Assert | ||
expect(screen.getByTitle<HTMLInputElement>(/^Min/).value).to.equal("0"); | ||
expect(screen.getByTitle<HTMLInputElement>(/^Max/).value).to.equal("20"); | ||
}); | ||
|
||
it("displays available min and max of items", () => { | ||
// Arrange | ||
const items: ListItem[] = ["-2", "1", "20", "42"].map((val) => ({ | ||
displayValue: val, | ||
value: val, | ||
})); | ||
const { getByText } = render( | ||
<RangePicker items={items} onReset={noop} onSearch={noop} currentRange={undefined} /> | ||
); | ||
|
||
// Act / Assert | ||
expect( | ||
getByText( | ||
`Full range available: ${items[0].displayValue}, ${ | ||
items[items.length - 1].displayValue | ||
}` | ||
) | ||
).to.exist; | ||
}); | ||
}); |
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