Skip to content

Commit 3f14189

Browse files
author
Said Shah
committed
Added tests for text input
1 parent 61ffc08 commit 3f14189

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed
Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
import React from "react";
2+
import { TextInput } from "./text-input";
3+
import faker from "faker";
4+
import { render, fireEvent } from "@testing-library/react";
5+
import uuid from "uuid";
26

37
describe("TextInput", () => {
4-
test.skip("TODO - https://github.com/AndcultureCode/AndcultureCode.JavaScript.React.Components/issues/9", () => {});
8+
test("when default props, renders input", () => {
9+
// Arrange
10+
const dataTestId = "dataTestId";
11+
12+
// Act
13+
const { getByTestId } = render(
14+
<TextInput testId={dataTestId} onChange={() => {}} id={uuid()} />
15+
);
16+
17+
// Assert
18+
expect(getByTestId(dataTestId)).not.toBeNil();
19+
});
20+
21+
test("when onChange set, calls handler upon change", () => {
22+
// Arrange
23+
let isChecked = false;
24+
const dataTestId = "dataTestId";
25+
const handleChange = () => (isChecked = true);
26+
27+
// Act
28+
const { getByTestId } = render(
29+
<TextInput
30+
testId={dataTestId}
31+
onChange={handleChange}
32+
id={uuid()}
33+
/>
34+
);
35+
36+
fireEvent.change(getByTestId(dataTestId), {
37+
target: { value: faker.random.word() },
38+
});
39+
40+
// Assert
41+
expect(isChecked).toBeTrue();
42+
});
43+
44+
test("when maxLength prop set, renders with given value", () => {
45+
// Arrange
46+
const dataTestId = "dataTestId";
47+
const maximumLength = 999;
48+
49+
// Act
50+
const { getByTestId } = render(
51+
<TextInput
52+
testId={dataTestId}
53+
onChange={() => {}}
54+
id={uuid()}
55+
maxLength={maximumLength}
56+
/>
57+
);
58+
59+
// Assert
60+
expect(getByTestId(dataTestId).getAttribute("maxLength")).toBe(
61+
maximumLength.toString()
62+
);
63+
});
564
});

src/atoms/forms/text-input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const TextInput: React.FC<TextInputProps> = (props: TextInputProps) => {
4040
return (
4141
<input
4242
aria-labelledby={ariaLabelledBy}
43-
data-test-id={testId}
43+
data-testid={testId}
4444
disabled={disabled}
4545
id={id}
4646
placeholder={placeholder}

0 commit comments

Comments
 (0)