-
Notifications
You must be signed in to change notification settings - Fork 322
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
feat: add autoFocus to CheckBox #2059
Changes from 7 commits
5d7c0f7
dc6d90d
594a606
b43fc1b
54fd0d5
ebccf66
dbaa866
3990d74
1588d55
313e042
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import React from "react"; | ||
import "@testing-library/jest-dom"; | ||
import { fireEvent, render, cleanup, screen } from "@testing-library/react"; | ||
import Checkbox from "../Checkbox"; | ||
|
||
|
@@ -39,6 +40,8 @@ type RenderHelper = { | |
option3Text: string; | ||
option3Value: string; | ||
onChangeMock3: jest.MockedFunction<MockedFunction>; | ||
defaultChecked?: boolean; | ||
autoFocusOption2?: boolean; | ||
}; | ||
|
||
function renderCheckboxes({ | ||
|
@@ -54,18 +57,26 @@ function renderCheckboxes({ | |
checkbox3Name, | ||
option3Text, | ||
option3Value, | ||
onChangeMock3 | ||
onChangeMock3, | ||
defaultChecked, | ||
autoFocusOption2 | ||
}: RenderHelper) { | ||
render( | ||
<form name={formName}> | ||
<Checkbox | ||
name={checkbox1Name} | ||
value={option1Value} | ||
label={option1Text} | ||
defaultChecked={true} | ||
defaultChecked={defaultChecked} | ||
onChange={onChangeMock1} | ||
/> | ||
<Checkbox name={checkbox2Name} value={option2Value} label={option2Text} onChange={onChangeMock2} /> | ||
<Checkbox | ||
name={checkbox2Name} | ||
value={option2Value} | ||
label={option2Text} | ||
onChange={onChangeMock2} | ||
autoFocus={autoFocusOption2} | ||
/> | ||
<Checkbox name={checkbox3Name} value={option3Value} label={option3Text} onChange={onChangeMock3} /> | ||
</form> | ||
); | ||
|
@@ -87,23 +98,24 @@ function testUnselectFirstOption( | |
} | ||
|
||
describe("Checkbox tests", () => { | ||
describe("regular checkbox tests", () => { | ||
const { | ||
formName, | ||
checkbox1Name, | ||
option1Value, | ||
option1Text, | ||
checkbox2Name, | ||
option2Value, | ||
option2Text, | ||
checkbox3Name, | ||
option3Value, | ||
option3Text | ||
} = createCheckboxesVariables(); | ||
const { | ||
formName, | ||
checkbox1Name, | ||
option1Value, | ||
option1Text, | ||
checkbox2Name, | ||
option2Value, | ||
option2Text, | ||
checkbox3Name, | ||
option3Value, | ||
option3Text | ||
} = createCheckboxesVariables(); | ||
|
||
let onChangeMock1: jest.MockedFunction<MockedFunction>, | ||
onChangeMock2: jest.MockedFunction<MockedFunction>, | ||
onChangeMock3: jest.MockedFunction<MockedFunction>; | ||
let onChangeMock1: jest.MockedFunction<MockedFunction>, | ||
onChangeMock2: jest.MockedFunction<MockedFunction>, | ||
onChangeMock3: jest.MockedFunction<MockedFunction>; | ||
|
||
describe("regular checkbox tests with default checked", () => { | ||
beforeEach(() => { | ||
onChangeMock1 = jest.fn(); | ||
onChangeMock2 = jest.fn(); | ||
|
@@ -122,7 +134,8 @@ describe("Checkbox tests", () => { | |
option1Value, | ||
checkbox1Name, | ||
checkbox2Name, | ||
checkbox3Name | ||
checkbox3Name, | ||
defaultChecked: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the existing tests, the tests had defaultChecked set to true, I needed it to be false in my tests so I can check the autoFocus so I added this as optional |
||
}); | ||
}); | ||
|
||
|
@@ -139,7 +152,7 @@ describe("Checkbox tests", () => { | |
expect(option3.checked).toBeFalsy(); | ||
}); | ||
|
||
it("should unselect 1st option", () => { | ||
it("should unselect 1st option", () => { | ||
testUnselectFirstOption(option1Text, option2Text, option3Text); | ||
}); | ||
|
||
|
@@ -179,6 +192,51 @@ describe("Checkbox tests", () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe("regular checkbox tests with no default checked", () => { | ||
it("should auto focus 2nd checkbox", () => { | ||
chensara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
renderCheckboxes({ | ||
formName, | ||
onChangeMock1, | ||
onChangeMock2, | ||
onChangeMock3, | ||
option3Text, | ||
option3Value, | ||
option2Text, | ||
option2Value, | ||
option1Text, | ||
option1Value, | ||
checkbox1Name, | ||
checkbox2Name, | ||
checkbox3Name, | ||
autoFocusOption2: true | ||
}); | ||
|
||
const option1 = screen.getByLabelText<HTMLInputElement>(option1Text); | ||
const option2 = screen.getByLabelText<HTMLInputElement>(option2Text); | ||
const option3 = screen.getByLabelText<HTMLInputElement>(option3Text); | ||
expect(option1).not.toHaveFocus(); | ||
expect(option2).toHaveFocus(); | ||
expect(option3).not.toHaveFocus(); | ||
}); | ||
}); | ||
|
||
describe("a11y", () => { | ||
it("should add the label", () => { | ||
const ariaLabel = "Lable Name"; | ||
const { getByLabelText } = render(<Checkbox label={ariaLabel} />); | ||
const checkboxComponent = getByLabelText(ariaLabel); | ||
expect(checkboxComponent).toBeTruthy(); | ||
}); | ||
|
||
it("should be the same text", () => { | ||
const ariaLabel = "Lable Name"; | ||
const { getByText } = render(<Checkbox label={ariaLabel} />); | ||
const checkboxComponentText = getByText(ariaLabel); | ||
expect(checkboxComponentText).toBeTruthy(); | ||
}); | ||
}); | ||
chensara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
describe("specific firefox checkbox tests", () => { | ||
const { | ||
formName, | ||
|
@@ -230,7 +288,8 @@ describe("Checkbox tests", () => { | |
option1Value, | ||
checkbox1Name, | ||
checkbox2Name, | ||
checkbox3Name | ||
checkbox3Name, | ||
defaultChecked: true | ||
}); | ||
}); | ||
|
||
|
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.
autoFocusOption2 ? :)
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.
:D, I can send just autoFocus and put it on the first option but I'm not sure it will be clear from outside.. WDYT?