|
1 | 1 | import userEvent from "@testing-library/user-event";
|
2 | 2 | import { render, screen } from "@testing-library/react";
|
3 |
| -import React, { useEffect } from "react"; |
| 3 | +import React, { FC, useEffect, useState } from "react"; |
4 | 4 |
|
| 5 | +import Button from "components/Button"; |
| 6 | +import Input from "components/Input"; |
5 | 7 | import Modal from "./Modal";
|
6 | 8 |
|
7 | 9 | describe("Modal ", () => {
|
@@ -189,4 +191,35 @@ describe("Modal ", () => {
|
189 | 191 | await userEvent.click(container);
|
190 | 192 | expect(handleCloseModal).toHaveBeenCalledTimes(0);
|
191 | 193 | });
|
| 194 | + |
| 195 | + it("doesn't lose focus when close prop changes", async () => { |
| 196 | + const TestComponent: FC = () => { |
| 197 | + const [inputText, setInputText] = useState(""); |
| 198 | + |
| 199 | + const handleCloseModal = () => { |
| 200 | + setInputText(""); |
| 201 | + }; |
| 202 | + |
| 203 | + return ( |
| 204 | + <div> |
| 205 | + <Modal title="Delete item1" close={handleCloseModal}> |
| 206 | + <Button>Show help</Button> |
| 207 | + <p>Type "delete item1" to confirm.</p> |
| 208 | + <Input |
| 209 | + type="text" |
| 210 | + value={inputText} |
| 211 | + onChange={(event) => setInputText(event.target.value)} |
| 212 | + /> |
| 213 | + </Modal> |
| 214 | + </div> |
| 215 | + ); |
| 216 | + }; |
| 217 | + |
| 218 | + const { container } = render(<TestComponent />); |
| 219 | + |
| 220 | + const input = container.querySelector("input"); |
| 221 | + await userEvent.type(container.querySelector("input"), "delete item1"); |
| 222 | + expect(input).toHaveFocus(); |
| 223 | + expect(input).toHaveValue("delete item1"); |
| 224 | + }); |
192 | 225 | });
|
0 commit comments