Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/dom/src/lib/ElementAssertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Copy link
Contributor

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" ?

});

const invertedError = new AssertionError({
actual: this.actual,
expected: document.activeElement,
message: "Expected the element not to have focus.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also in here, what do you think about the messaging to be:
"Expected the element NOT to be focused" ?

});

return this.execute({
assertWhen: hasFocus,
error,
invertedError,
});
}

private getClassList(): string[] {
return this.actual.className.split(/\s+/).filter(Boolean);
}
Expand Down
33 changes: 33 additions & 0 deletions packages/dom/test/unit/lib/ElementAssertion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion.
If you dont add .focus() , any element is focused so, I think the test could be easier like

Suggested change
const input1 = getByTestId("input1");
const input2 = getByTestId("input2");
input1.focus();
const test = new ElementAssertion(input2);
const input1 = getByTestId("input1");
const test = new ElementAssertion(input1);


expect(() => test.toHaveFocus())
.toThrowError(AssertionError)
.toHaveMessage("Expected the element to have focus.");

expect(test.not.toHaveFocus()).toBeEqual(test);
});
});
});
});
10 changes: 10 additions & 0 deletions packages/dom/test/unit/lib/fixtures/focusTestComponent.tsx
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>
);
}