-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(InlineEditor): value not updated on escape when controlled (#4379)
- Loading branch information
1 parent
dde7995
commit 039e057
Showing
3 changed files
with
144 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,122 @@ | ||
import { render } from "@testing-library/react"; | ||
import { describe, expect, it } from "vitest"; | ||
import { useState } from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { HvInlineEditor } from "./InlineEditor"; | ||
import { HvInlineEditor, HvInlineEditorProps } from "./InlineEditor"; | ||
|
||
const controlledValue = "My value"; | ||
const controlledLabel = "Label"; | ||
const Controlled = ({ | ||
onBlur, | ||
onChange, | ||
onKeyDown, | ||
...others | ||
}: Partial<HvInlineEditorProps>) => { | ||
const [value, setValue] = useState(controlledValue); | ||
|
||
return ( | ||
<HvInlineEditor | ||
label={controlledLabel} | ||
value={value} | ||
onChange={(event, newValue) => { | ||
setValue(newValue); | ||
onChange?.(event, newValue); | ||
}} | ||
onBlur={(event, newValue) => { | ||
setValue(newValue); | ||
onBlur?.(event, newValue); | ||
}} | ||
onKeyDown={(event, newValue) => { | ||
setValue(newValue); | ||
onKeyDown?.(event, newValue); | ||
}} | ||
{...others} | ||
/> | ||
); | ||
}; | ||
|
||
describe("InlineEditor", () => { | ||
it("renders the component as expected", () => { | ||
const value = "VALUE123"; | ||
const { getByText } = render(<HvInlineEditor defaultValue={value} />); | ||
|
||
const container = getByText(value); | ||
render(<HvInlineEditor defaultValue={value} />); | ||
|
||
const container = screen.getByText(value); | ||
expect(container).toBeInTheDocument(); | ||
expect(container).toBeVisible(); | ||
}); | ||
|
||
it("should trigger onKeyDown and show the previous value when pressing ESC (controlled)", async () => { | ||
const user = userEvent.setup(); | ||
const mockFn = vi.fn(); | ||
render(<Controlled onKeyDown={mockFn} />); | ||
|
||
const inputButton = screen.getByRole("button", { name: controlledValue }); | ||
await user.click(inputButton); | ||
|
||
const input = screen.getByRole("textbox", { name: controlledLabel }); | ||
const type = "123"; | ||
await user.type(input, type); | ||
await user.keyboard("{Escape}"); | ||
|
||
expect(mockFn).toHaveBeenCalledTimes(type.length + 1); | ||
expect( | ||
screen.getByRole("button", { name: controlledValue }), | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.queryByRole("button", { name: type }), | ||
).not.toBeInTheDocument(); | ||
}); | ||
|
||
it("should trigger onChange when typing content (controlled)", async () => { | ||
const user = userEvent.setup(); | ||
const mockFn = vi.fn(); | ||
render(<Controlled onChange={mockFn} />); | ||
|
||
const inputButton = screen.getByRole("button", { name: controlledValue }); | ||
await user.click(inputButton); | ||
|
||
const input = screen.getByRole("textbox", { name: controlledLabel }); | ||
const type = "123"; | ||
await user.type(input, type); | ||
|
||
expect(mockFn).toHaveBeenCalledTimes(type.length); | ||
}); | ||
|
||
it("should trigger onBlur and show the new value when blurring the input with new content (controlled)", async () => { | ||
const user = userEvent.setup(); | ||
const mockFn = vi.fn(); | ||
render(<Controlled onBlur={mockFn} />); | ||
|
||
const inputButton = screen.getByRole("button", { name: controlledValue }); | ||
await user.click(inputButton); | ||
|
||
const input = screen.getByRole("textbox", { name: controlledLabel }); | ||
const type = "123"; | ||
await user.type(input, type); | ||
await user.tab(); | ||
|
||
expect(mockFn).toHaveBeenCalledOnce(); | ||
expect( | ||
screen.getByRole("button", { name: controlledValue + type }), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("should trigger onBlur and show the previous value when blurring the input with empty content (controlled)", async () => { | ||
const user = userEvent.setup(); | ||
const mockFn = vi.fn(); | ||
render(<Controlled onBlur={mockFn} />); | ||
|
||
const inputButton = screen.getByRole("button", { name: controlledValue }); | ||
await user.click(inputButton); | ||
|
||
const input = screen.getByRole("textbox", { name: controlledLabel }); | ||
await user.clear(input); | ||
await user.tab(); | ||
|
||
expect(mockFn).toHaveBeenCalledOnce(); | ||
expect( | ||
screen.getByRole("button", { name: controlledValue }), | ||
).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters