Skip to content

Commit

Permalink
test(useDateFieldProps): initialize field via options
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Mar 22, 2024
1 parent 289479b commit 9d70a70
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/fields/date-field/dateField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const InitializedInput = formStory({
<DateInput
field={fields.dueDate}
label="Due Date"
initialValue={new Date()}
initialValue={new Date(2024, 2, 30)}
/>
),
},
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/use-date-field-props/useDateFieldProps.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import { dateField } from "../../fields/date-field";
import { getDateString } from "../../fields/date-field/DateInput.mock";

describe("useDateFieldProps", () => {
it("initializes the field via options", () => {
const field = dateField();
const initialValue = new Date("2024/03/31");

const { result: props } = renderHook(() =>
useDateFieldProps(field, { initialValue }),
);

expect(props.current.value).toBe(initialValue);
});

it("reads empty value as undefined", async () => {
const field = dateField({ value: new Date() });
const { result } = renderHook(() => useDateFieldProps(field));
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/use-number-field-props/useNumberFieldProps.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ describe("useNumberFieldProps()", () => {
it("initializes the field via options", async () => {
const field = numberField();

const props = renderHook(() =>
const { result: props } = renderHook(() =>
useNumberFieldProps(field, { initialValue: 42 }),
);

expect(props.result.current.value).toBe(42);
expect(props.current.value).toBe(42);
});

it("reads empty input value as undefined with props.value as empty string", async () => {
Expand Down
17 changes: 0 additions & 17 deletions src/hooks/use-text-field-props/useTextFieldProps.test.ts

This file was deleted.

29 changes: 29 additions & 0 deletions src/hooks/use-text-field-props/useTextFieldProps.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { act, render, renderHook, screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { describe, expect, it } from "vitest";

import { useTextFieldProps } from "./useTextFieldProps";
import { textField } from "../../fields";

describe("useTextFieldProps()", () => {
it("initializes the field via options", async () => {
const field = textField();

const { result: props } = renderHook(() =>
useTextFieldProps(field, { initialValue: "hello" }),
);

expect(props.current.value).toBe("hello");
});

it("reads value as string", async () => {
const field = textField();

const { result: props } = renderHook(() => useTextFieldProps(field));
render(<input {...props.current} />);

await act(() => userEvent.type(screen.getByRole("textbox"), "x"));

expect(props.current.value).toBe("x");
});
});

0 comments on commit 9d70a70

Please sign in to comment.