Skip to content

autoFocus not being applied #27

@mischnic

Description

@mischnic
  • preact-testing-library version: 2.0.0
  • preact version: 10.4.7
  • node version: 14
  • npm (or yarn) version: .22.4

Relevant code or config

import React from "preact/compat";
import { render, fireEvent } from "@testing-library/preact";

// works:
// import React from "react";
// import { render, fireEvent } from "@testing-library/react";

test("test", () => {
  const { getByTestId } = render(
    <div>
      <input data-testid="input" autoFocus />
    </div>
  );

  expect(document.activeElement).toBe(getByTestId("input"));
});

What you did:

See code

What happened:

 FAIL  ./test.js
  ✕ test (25 ms)

  ● test

    expect(received).toBe(expected) // Object.is equality

    - Expected  - 0
    + Received  + 6

    + <body>
    +   <div>
    +     <div>
            <input
              autofocus="true"
              data-testid="input"
            />
    +     </div>
    +   </div>
    + </body>

      12 |   );
      13 |
    > 14 |   expect(document.activeElement).toBe(getByTestId("input"));
         |                                  ^
      15 | });
      16 |

      at Object.<anonymous> (test.js:14:34)

Reproduction repository:

https://codesandbox.io/s/beautiful-cannon-gw4ee?file=/src/index.test.js

Problem description:

Apparently, the <input autoFocus> element isn't focused. The example works when changing out the imports to use React.

Suggested solution:

Autofocus the element....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions