Skip to content

Commit 135ebab

Browse files
committed
fix(keppel): fix input variable for TextInput
1 parent a5a2599 commit 135ebab

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from "react"
22

33
export const TextInput = ({ testid = "", value, isEditable, onChange }) => {
4-
value = value || ""
4+
const inputValue = value || ""
55
if (!isEditable) {
6-
if (value === "") {
6+
if (inputValue === "") {
77
return <em>Any</em>
88
}
9-
return <code>{value || ""}</code>
9+
return <code>{inputValue || ""}</code>
1010
}
11-
return <input data-testid={testid} type="text" className="form-control" onChange={(e) => onChange(e)} />
11+
return <input data-testid={testid} type="text" value={inputValue} className="form-control" onChange={(e) => onChange(e)} />
1212
}

plugins/keppel/app/javascript/widgets/app/components/componentHelpers/TextInput.test.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,41 @@ import { render, fireEvent, screen } from "@testing-library/react"
33
import { TextInput } from "./TextInput" // Adjust the import according to your file structure
44

55
describe("TextInput", () => {
6-
it("renders input element when isEditable is true", () => {
6+
it("renders input element when isEditable is true", async () => {
77
const mockOnChange = jest.fn()
8-
render(<TextInput testid="input" value="" isEditable={true} onChange={mockOnChange} />)
8+
let value = ""
9+
10+
const { rerender } = render(
11+
<TextInput
12+
testid="input"
13+
value={value}
14+
isEditable={true}
15+
onChange={(e) => {
16+
value = e.target.value
17+
mockOnChange(e)
18+
}}
19+
/>
20+
)
921
const inputElement = screen.getByTestId("input")
1022
expect(inputElement).not.toBeNull()
23+
1124
fireEvent.change(inputElement, { target: { value: "New Value" } })
25+
rerender(
26+
<TextInput
27+
testid="input"
28+
value={value}
29+
isEditable={true}
30+
onChange={(e) => {
31+
value = e.target.value
32+
mockOnChange(e)
33+
}}
34+
/>
35+
)
1236
expect(mockOnChange).toHaveBeenCalledTimes(1)
37+
expect(inputElement.value).toBe("New Value")
1338
})
1439

15-
it('renders input element when isEditable is false', () => {
40+
it("renders input element when isEditable is false", () => {
1641
render(<TextInput value="" isEditable={false} onChange={() => {}} />)
1742
expect(screen.getByText("Any")).not.toBeNull()
1843
})

0 commit comments

Comments
 (0)