Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Dec 23, 2023
1 parent 638744d commit ce83830
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
24 changes: 13 additions & 11 deletions src/lib/mobx-form/form-has-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from "./form-has-error.ts";
import { validators } from "./validator.ts";

const isRequiredMessage = 'is required';

test("isFormTouchedAndHasError", () => {
const f1 = {
a: new TextField("a"),
Expand All @@ -24,8 +26,8 @@ test("isFormTouchedAndHasError", () => {
).toBeTruthy();

const f2 = {
a: new TextField("a", validators.required()),
b: [new TextField("b", validators.required()), new TextField("d")],
a: new TextField("a", validators.required(isRequiredMessage)),
b: [new TextField("b", validators.required(isRequiredMessage)), new TextField("d")],
};

f2.a.touch();
Expand All @@ -49,8 +51,8 @@ test("isFormTouchedAndHasError", () => {

test("is form dirty", () => {
const f = {
a: new TextField("a", validators.required()),
b: [new TextField("b", validators.required()), new TextField("d")],
a: new TextField("a", validators.required(isRequiredMessage)),
b: [new TextField("b", validators.required(isRequiredMessage)), new TextField("d")],
};

expect(isFormTouched(f)).toBeFalsy();
Expand All @@ -61,7 +63,7 @@ test("is form dirty", () => {

test("is form invalid by default", () => {
const f = {
a: new TextField("", validators.required()),
a: new TextField("", validators.required(isRequiredMessage)),
};

expect(isFormValid(f)).toBeFalsy();
Expand All @@ -83,8 +85,8 @@ test("is boolean form dirty", () => {

test("is form empty", () => {
const f = {
a: new TextField("a", validators.required()),
b: [new TextField("b", validators.required()), new TextField("d")],
a: new TextField("a", validators.required(isRequiredMessage)),
b: [new TextField("b", validators.required(isRequiredMessage)), new TextField("d")],
};

expect(isFormEmpty(f)).toBeFalsy();
Expand All @@ -99,10 +101,10 @@ test("is form empty", () => {

test("very nested form - only fields", () => {
const f = {
a: new TextField("a", validators.required()),
a: new TextField("a", validators.required(isRequiredMessage)),
b: {
c: {
d: new TextField("d", validators.required()),
d: new TextField("d", validators.required(isRequiredMessage)),
},
},
};
Expand All @@ -118,11 +120,11 @@ test("very nested form - only fields", () => {

test("very nested form - any fields", () => {
const f = {
a: new TextField("a", validators.required()),
a: new TextField("a", validators.required(isRequiredMessage)),
num: 12,
b: {
c: {
d: new TextField("d", validators.required()),
d: new TextField("d", validators.required(isRequiredMessage)),
k: null,
},
},
Expand Down
4 changes: 1 addition & 3 deletions src/lib/mobx-form/validator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// @ts-nocheck
import { t } from "../../translations/t.ts";

// https://codesandbox.io/s/github/final-form/react-final-form/tree/master/examples/field-level-validation?file=/index.js
export const validators = {
required:
(errorMessage = t("validation_required")) =>
(errorMessage) =>
(value) =>
value ? undefined : errorMessage,
number: (value) => (isNaN(value) ? t("validation_number") : undefined),
all:
(...validators) =>
(value) =>
Expand Down
7 changes: 7 additions & 0 deletions src/store/deck-form-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ vi.mock("../lib/telegram/show-alert.ts", () => {
};
});

vi.mock("../translations/t.ts", () => {
return {
t: (val: string) => val,
};
});


vi.mock("../api/api.ts", () => {
return {
upsertDeckRequest: mocks.upsertDeckRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/store/deck-form-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const createDeckTitleField = (value: string) => {
};

export const createCardSideField = (value: string) => {
return new TextField(value, validators.required());
return new TextField(value, validators.required(t('validation_required')));
};

const createUpdateForm = (
Expand Down

0 comments on commit ce83830

Please sign in to comment.