-
Notifications
You must be signed in to change notification settings - Fork 2
feat(dom): DOM - toHaveFocus #151
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -142,6 +142,34 @@ export class ElementAssertion<T extends Element> extends Assertion<T> { | |
); | ||
} | ||
|
||
/** | ||
* Check if the provided element is currently focused in the document. | ||
* | ||
* @returns The assertion instance. | ||
*/ | ||
public toHaveFocus(): this { | ||
|
||
const hasFocus = this.actual === document.activeElement; | ||
|
||
const error = new AssertionError({ | ||
actual: this.actual, | ||
expected: document.activeElement, | ||
message: "Expected the element to have focus.", | ||
}); | ||
|
||
const invertedError = new AssertionError({ | ||
actual: this.actual, | ||
expected: document.activeElement, | ||
message: "Expected the element not to have focus.", | ||
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. also in here, what do you think about the messaging to be: |
||
}); | ||
|
||
return this.execute({ | ||
assertWhen: hasFocus, | ||
error, | ||
invertedError, | ||
}); | ||
} | ||
|
||
private getClassList(): string[] { | ||
return this.actual.className.split(/\s+/).filter(Boolean); | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ import { render } from "@testing-library/react"; | |||||||||||||
|
||||||||||||||
import { ElementAssertion } from "../../../src/lib/ElementAssertion"; | ||||||||||||||
|
||||||||||||||
import { FocusTestComponent } from "./fixtures/focusTestComponent"; | ||||||||||||||
import { HaveClassTestComponent } from "./fixtures/haveClassTestComponent"; | ||||||||||||||
import { NestedElementsTestComponent } from "./fixtures/nestedElementsTestComponent"; | ||||||||||||||
import { SimpleTestComponent } from "./fixtures/simpleTestComponent"; | ||||||||||||||
|
@@ -265,4 +266,36 @@ describe("[Unit] ElementAssertion.test.ts", () => { | |||||||||||||
}); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
describe(".toHaveFocus", () => { | ||||||||||||||
context("when the element has focus", () => { | ||||||||||||||
it("returns the assertion instance", () => { | ||||||||||||||
const { getByTestId } = render(<FocusTestComponent />); | ||||||||||||||
const input1 = getByTestId("input1"); | ||||||||||||||
input1.focus(); | ||||||||||||||
const test = new ElementAssertion(input1); | ||||||||||||||
|
||||||||||||||
expect(test.toHaveFocus()).toBeEqual(test); | ||||||||||||||
|
||||||||||||||
expect(() => test.not.toHaveFocus()) | ||||||||||||||
.toThrowError(AssertionError) | ||||||||||||||
.toHaveMessage("Expected the element not to have focus."); | ||||||||||||||
}); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
context("when the element does not have focus", () => { | ||||||||||||||
it("throws an assertion error", () => { | ||||||||||||||
const { getByTestId } = render(<FocusTestComponent />); | ||||||||||||||
const input1 = getByTestId("input1"); | ||||||||||||||
const input2 = getByTestId("input2"); | ||||||||||||||
input1.focus(); | ||||||||||||||
const test = new ElementAssertion(input2); | ||||||||||||||
Comment on lines
+288
to
+291
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. Just a suggestion.
Suggested change
|
||||||||||||||
|
||||||||||||||
expect(() => test.toHaveFocus()) | ||||||||||||||
.toThrowError(AssertionError) | ||||||||||||||
.toHaveMessage("Expected the element to have focus."); | ||||||||||||||
|
||||||||||||||
expect(test.not.toHaveFocus()).toBeEqual(test); | ||||||||||||||
}); | ||||||||||||||
}); | ||||||||||||||
}); | ||||||||||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ReactElement } from "react"; | ||
|
||
export function FocusTestComponent(): ReactElement { | ||
return ( | ||
<div> | ||
<input data-testid="input1" /> | ||
<input data-testid="input2" /> | ||
</div> | ||
); | ||
} |
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.
What do you think about the messaging to be:
"Expected the element to be focused" ?