Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Etheryte committed Sep 26, 2024
1 parent 84737d6 commit 55afbdc
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 26 deletions.
8 changes: 4 additions & 4 deletions web/html/src/components/input/InputBase.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { render, screen } from "utils/test-utils";

import { Form } from "./form/Form";
import { InputBase } from "./InputBase";
import { Validate } from "./validate";
import { Validation } from "./validation/validation";

describe("InputBase", () => {
// Use these to test model changes in tests
Expand Down Expand Up @@ -41,7 +41,7 @@ describe("InputBase", () => {
};

renderWithForm(
<InputBase name="foo" validate={Validate.minLength(2)}>
<InputBase name="foo" validate={Validation.minLength(2)}>
{({ setValue }) => {
if (isFirstFire) {
// Realistically this should be with a user interaction, but we manually fire it off to see if it propagates
Expand All @@ -64,7 +64,7 @@ describe("InputBase", () => {
};

renderWithForm(
<InputBase name="username" label="Username" validate={Validate.minLength(2)}>
<InputBase name="username" label="Username" validate={Validation.minLength(2)}>
{({ setValue }) => {
if (isFirstFire) {
setValue("username", "fo");
Expand All @@ -87,7 +87,7 @@ describe("InputBase", () => {
};

renderWithForm(
<InputBase name={["firstname", "lastname"]} label="User" validate={Validate.minLength(2)}>
<InputBase name={["firstname", "lastname"]} label="User" validate={Validation.minLength(2)}>
{({ setValue }) => {
if (isFirstFire) {
setValue("firstname", "John");
Expand Down
2 changes: 1 addition & 1 deletion web/html/src/components/input/InputBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import _isNil from "lodash/isNil";
import { FormContext } from "./form/Form";
import { FormGroup } from "./FormGroup";
import { Label } from "./Label";
import { ValidationResult, Validator } from "./validate";
import { ValidationResult, Validator } from "./validation/validation";

export type InputBaseProps<ValueType = string> = {
/** name of the field to map in the form model.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";

import { SubmitButton } from "components/buttons";
import { Form, Text, Validate } from "components/input";
import { Form, Text, Validation } from "components/input";

import { FormMultiInput } from "./FormMultiInput";

Expand Down Expand Up @@ -47,15 +47,15 @@ export default () => {
required
labelClass="col-md-3"
divClass="col-md-6"
validate={[Validate.minLength(2)]}
validate={[Validation.minLength(2)]}
/>
<Text
name={`user${index}_lastname`}
label={t("Last Name")}
required
labelClass="col-md-3"
divClass="col-md-6"
validate={[Validate.minLength(2)]}
validate={[Validation.minLength(2)]}
/>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";

import { SubmitButton } from "components/buttons";
import { Form, FormMultiInput, Text, Validate } from "components/input";
import { Form, FormMultiInput, Text, Validation } from "components/input";

export default () => {
const [model, setModel] = useState({ user0_login: "jdoe" });
Expand Down Expand Up @@ -43,7 +43,7 @@ export default () => {
required
labelClass="col-md-3"
divClass="col-md-6"
validate={Validate.minLength(2)}
validate={Validation.minLength(2)}
/>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";

import { SubmitButton } from "components/buttons";
import { Form, FormMultiInput, Text, Validate } from "components/input";
import { Form, FormMultiInput, Text, Validation } from "components/input";

export default () => {
const [model, setModel] = useState({ user0_firstname: "John", user0_lastname: "Doe", user0_age: 42 });
Expand Down Expand Up @@ -53,15 +53,15 @@ export default () => {
name={`user${index}_firstname`}
required
divClass="col-md-12"
validate={Validate.minLength(2)}
validate={Validation.minLength(2)}
className="col-md-4"
/>
<Text
name={`user${index}_lastname`}
required
divClass="col-md-12"
className="col-md-4"
validate={Validate.minLength(2)}
validate={Validation.minLength(2)}
/>
<Text name={`user${index}_age`} required divClass="col-md-12" className="col-md-4" />
</>
Expand Down
4 changes: 2 additions & 2 deletions web/html/src/components/input/form/Form.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";

import { SubmitButton } from "components/buttons";
import { Form, Text, Validate } from "components/input";
import { Form, Text, Validation } from "components/input";

export default () => {
const [model, setModel] = useState({
Expand All @@ -22,7 +22,7 @@ export default () => {
required
labelClass="col-md-3"
divClass="col-md-6"
validate={Validate.minLength(2)}
validate={Validation.minLength(2)}
/>
<SubmitButton className="btn-success" text={t("Submit")} />
</Form>
Expand Down
2 changes: 1 addition & 1 deletion web/html/src/components/input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export * from "./radio/Radio";
export * from "./range/Range";
export * from "./select/Select";
export * from "./text/Text";
export * from "./validate";
export * from "./validation/validation";
4 changes: 2 additions & 2 deletions web/html/src/components/input/password/Password.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";

import { SubmitButton } from "components/buttons";
import { Form, Password, Validate } from "components/input";
import { Form, Password, Validation } from "components/input";

export default () => {
const [model, setModel] = useState({
Expand All @@ -22,7 +22,7 @@ export default () => {
required
labelClass="col-md-3"
divClass="col-md-6"
validate={Validate.minLength(4)}
validate={Validation.minLength(4)}
/>
<SubmitButton className="btn-success" text={t("Submit")} />
</Form>
Expand Down
6 changes: 3 additions & 3 deletions web/html/src/components/input/text/Text.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";

import { SubmitButton } from "components/buttons";
import { Form, Text, Validate } from "components/input";
import { Form, Text, Validation } from "components/input";

export default () => {
const [model, setModel] = useState({
Expand All @@ -22,10 +22,10 @@ export default () => {
required
labelClass="col-md-3"
divClass="col-md-6"
validate={Validate.minLength(2)}
validate={Validation.minLength(2)}
/>
<p className="col-md-offset-3 offset-md-3 col-md-6">(Last name has no label.)</p>
<Text name="lastname" required divClass="col-md-6 col-md-offset-3 offset-md-3" validate={Validate.minLength(2)} />
<Text name="lastname" required divClass="col-md-6 col-md-offset-3 offset-md-3" validate={Validation.minLength(2)} />
<SubmitButton className="btn-success" text={t("Submit")} />
</Form>
);
Expand Down
8 changes: 4 additions & 4 deletions web/html/src/components/input/validation/helpers.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { useState } from "react";

import { Form, Text, Validate } from "components/input";
import { Form, Text, Validation } from "components/input";

export default () => {
const [model, setModel] = useState({ foo: "Foo", bar: "3", tea: "Hi" });

return (
<Form model={model} onChange={setModel}>
<p>There are numerous validation helpers:</p>
<Text name="foo" validate={[Validate.matches(/^F/), Validate.minLength(3)]} />
<Text name="bar" validate={Validate.isInt()} />
<Text name="tea" validate={Validate.matches(/[a-z]/, "Must include a lowercase letter")} />
<Text name="foo" validate={[Validation.matches(/^F/), Validation.minLength(3)]} />
<Text name="bar" validate={Validation.isInt()} />
<Text name="tea" validate={Validation.matches(/[a-z]/, "Must include a lowercase letter")} />
</Form>
);
};
76 changes: 76 additions & 0 deletions web/html/src/components/input/validation/validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Validation } from "./validation";

const errorMessage = "error message";

describe("validation", () => {
test("minLength string", () => {
const validator = Validation.minLength(3, errorMessage);

expect(validator("foo")).toEqual(undefined);
expect(validator("fo")).toEqual(errorMessage);
});

test("minLength object", () => {
const validator = Validation.minLength(3, errorMessage);

expect(
validator({
foo: "foo",
bar: "bar",
tea: "tea",
})
).toEqual(undefined);

expect(
validator({
foo: "foo",
bar: "ba",
tea: "tea",
})
).toEqual(errorMessage);
});

test("maxLength string", () => {
const validator = Validation.maxLength(3, errorMessage);

expect(validator("foo")).toEqual(undefined);
expect(validator("fooo")).toEqual(errorMessage);
});

test("maxLength object", () => {
const validator = Validation.maxLength(3, errorMessage);

expect(
validator({
foo: "foo",
bar: "bar",
tea: "tea",
})
).toEqual(undefined);

expect(
validator({
foo: "foo",
bar: "barr",
tea: "tea",
})
).toEqual(errorMessage);
});

test("isInt", () => {
const validator = Validation.isInt();

// If you want the value to be required, set the `required` flag instead
expect(validator("")).toEqual(true);
expect(validator("0")).toEqual(true);
expect(validator("42")).toEqual(true);
expect(validator("42.")).toEqual(false);
expect(validator("4.2")).toEqual(false);
expect(validator("0x1")).toEqual(false);
expect(validator("foo")).toEqual(false);
});

test("matches", () => {
// TODO: Implement
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const isInt =
}
};

export const Validate = {
export const Validation = {
matches,
minLength,
maxLength,
Expand Down

0 comments on commit 55afbdc

Please sign in to comment.