-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: Select component: refactor tests
- Loading branch information
Showing
5 changed files
with
109 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
module.exports = require("@tsed/config/craco.config")("react-formio", {}); | ||
module.exports = require("@tsed/config/craco.config")("react-formio", { | ||
coverageThreshold: { | ||
global: { | ||
branches: 44.6, | ||
functions: 60.98, | ||
lines: 60.98, | ||
statements: 62.27 | ||
} | ||
} | ||
}); |
167 changes: 74 additions & 93 deletions
167
packages/react-formio/src/components/select/select.component.spec.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 |
---|---|---|
@@ -1,105 +1,86 @@ | ||
import React from "react"; | ||
import { Sandbox } from "./select.stories"; | ||
import { fireEvent, render } from "@testing-library/react"; | ||
import { Choicesjs, Sandbox } from "./select.stories"; | ||
|
||
describe("select component", () => { | ||
it("should render the select component", () => { | ||
const { getByTestId } = render(<Sandbox {...Sandbox.args} name={"test"} />); | ||
describe("Select", () => { | ||
describe("select component Sandbox version", () => { | ||
it("should render the select component", () => { | ||
const { getByTestId } = render(<Sandbox {...Sandbox.args} name={"test-sandbox"} />); | ||
|
||
expect(getByTestId("select_test")).toBeInTheDocument(); | ||
}); | ||
expect(getByTestId("select_test-sandbox")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render select options with 'Placeholder test' as fisrt value", () => { | ||
const choices = [ | ||
{ label: "test1", value: "value1" }, | ||
{ label: "test2", value: "value2" } | ||
]; | ||
const placeHolderTest = "Placeholder test"; | ||
|
||
const { getByTestId, getByText } = render( | ||
<Sandbox | ||
{...Sandbox.args} | ||
placeholder={placeHolderTest} | ||
choices={choices} | ||
name={"test"} | ||
/> | ||
); | ||
const selectOptions = Array.prototype.map.call( | ||
getByTestId("select_test").children, | ||
(label) => label.textContent | ||
); | ||
|
||
expect(getByTestId("select_test").firstChild.textContent).toBe( | ||
placeHolderTest | ||
); | ||
(selectOptions as Array<string>).map((option) => { | ||
return expect(getByText(option)).toBeInTheDocument(); | ||
it("should render the select component with a different size", () => { | ||
const { getByTestId } = render(<Sandbox {...Sandbox.args} size='small' name={"test-sandbox"} />); | ||
const select = getByTestId("select_test-sandbox"); | ||
expect(select).toBeInTheDocument(); | ||
expect(select).toHaveClass("form-control-small"); | ||
}); | ||
}); | ||
|
||
it("should have Placeholder label as selected option by default", () => { | ||
const choices = [ | ||
{ label: "test1", value: "value1" }, | ||
{ label: "test2", value: "value2" } | ||
]; | ||
const placeHolderTest = "Placeholder test"; | ||
|
||
const { getAllByTestId } = render( | ||
<Sandbox | ||
{...Sandbox.args} | ||
placeholder={placeHolderTest} | ||
choices={choices} | ||
name={"test"} | ||
/> | ||
); | ||
|
||
const selectedElement = Array.prototype.find.call( | ||
getAllByTestId("select-option"), | ||
(option) => { | ||
return option.selected === true; | ||
} | ||
).textContent; | ||
|
||
expect(selectedElement).toEqual(placeHolderTest); | ||
}); | ||
it("should render select options with 'Placeholder test' as first value", () => { | ||
const choices = [ | ||
{ label: "test1", value: "value1" }, | ||
{ label: "test2", value: "value2" } | ||
]; | ||
|
||
const placeHolderTest = "Placeholder test"; | ||
|
||
const { getByRole } = render(<Sandbox {...Sandbox.args} placeholder={placeHolderTest} choices={choices} name={"test-sandbox"} />); | ||
|
||
expect(getByRole("option", { name: "Placeholder test" })).toBeInTheDocument(); | ||
expect(getByRole("option", { name: "test1" })).toBeInTheDocument(); | ||
expect(getByRole("option", { name: "test2" })).toBeInTheDocument(); | ||
}); | ||
|
||
it("should have Placeholder label as selected option by default", () => { | ||
const choices = [ | ||
{ label: "test1", value: "value1" }, | ||
{ label: "test2", value: "value2" } | ||
]; | ||
const placeHolderTest = "Placeholder test"; | ||
|
||
const { getByRole } = render(<Sandbox {...Sandbox.args} placeholder={placeHolderTest} choices={choices} name={"test-sandbox"} />); | ||
const option = getByRole("option", { name: placeHolderTest }) as HTMLOptionElement; | ||
|
||
it("should change the value of the selected option when you click on another choice ", () => { | ||
const choices = [ | ||
{ label: "test1", value: "value1" }, | ||
{ label: "test2", value: "value2" } | ||
]; | ||
const placeHolderTest = "Placeholder test"; | ||
|
||
const { getByTestId, getAllByTestId } = render( | ||
<Sandbox | ||
{...Sandbox.args} | ||
placeholder={placeHolderTest} | ||
choices={choices} | ||
name={"test"} | ||
/> | ||
); | ||
|
||
fireEvent.change(getByTestId("select_test"), { | ||
target: { value: "value1" } | ||
expect(option.selected).toBeTruthy(); | ||
}); | ||
let selectedElement = Array.prototype.find.call( | ||
getAllByTestId("select-option"), | ||
(option) => { | ||
return option.selected === true; | ||
} | ||
).textContent; | ||
expect(selectedElement).toEqual("test1"); | ||
|
||
fireEvent.change(getByTestId("select_test"), { | ||
target: { value: "value2" } | ||
|
||
it("should change the value of the selected option when you click on another choice", () => { | ||
const choices = [ | ||
{ label: "test1", value: "value1" }, | ||
{ label: "test2", value: "value2" } | ||
]; | ||
const placeHolderTest = "Placeholder test"; | ||
const onChange = jest.fn(); | ||
|
||
const { getByRole, getByTestId } = render( | ||
<Sandbox {...Sandbox.args} placeholder={placeHolderTest} choices={choices} name={"test-sandbox"} onChange={onChange} /> | ||
); | ||
|
||
fireEvent.change(getByTestId("select_test-sandbox"), { | ||
target: { value: "value1" } | ||
}); | ||
|
||
const option = getByRole("option", { name: "test1" }) as HTMLOptionElement; | ||
|
||
expect(option.selected).toBeTruthy(); | ||
expect(onChange).toHaveBeenCalledWith("test-sandbox", "value1"); | ||
}); | ||
}); | ||
|
||
describe("select component Choicesjs version", () => { | ||
it("should render select options with 'test1' as first value", () => { | ||
const choices = [ | ||
{ label: "test1", value: "value1" }, | ||
{ label: "test2", value: "value2" } | ||
]; | ||
const placeHolderTest = "Placeholder test"; | ||
|
||
const { getByRole } = render( | ||
<Choicesjs {...Choicesjs.args} layout={"choicesjs"} choices={choices} placeholder={placeHolderTest} name={"test-choicesjs"} /> | ||
); | ||
|
||
expect(getByRole("option", { name: "test1" })).toBeInTheDocument(); | ||
}); | ||
selectedElement = Array.prototype.find.call( | ||
getAllByTestId("select-option"), | ||
(option) => { | ||
return option.selected === true; | ||
} | ||
).textContent; | ||
|
||
expect(selectedElement).toEqual("test2"); | ||
}); | ||
}); |
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