Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexShukel committed Sep 12, 2023
1 parent 412823a commit e0e5bf9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/x/src/IntegerFieldI18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const defaultIntegerFieldI18n: IntegerFieldI18n = {
required: 'Field is required',
invalidInput: 'Must be integer',
minValue: (min: number) => `Value should not be less than ${min.toFixed(0)}`,
maxValue: (max: number) => `Value should not be more than ${max.toFixed(0)}`,
maxValue: (max: number) => `Value should not be greater than ${max.toFixed(0)}`,
};

export const IntegerFieldI18nContext = createContext<IntegerFieldI18n>(defaultIntegerFieldI18n);
Expand Down
5 changes: 0 additions & 5 deletions packages/x/src/useIntegerField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import { FieldConfig, useFieldValidator } from '@reactive-forms/core';
import { IntegerFieldI18nContext } from './IntegerFieldI18n';
import { ConversionError, ConverterFieldBag, useConverterField } from './useConverterField';

export const defaultRequiredError = 'Field is required';
export const defaultInvalidInputError = 'Must be integer';
export const defaultMinValueError = (min: number) => `Value should not be less than ${min.toFixed(0)}`;
export const defaultMaxValueError = (max: number) => `Value should not be more than ${max.toFixed(0)}`;

const INTEGER_REGEX = /^-?\d+$/;

const formatInteger = (value: number | null | undefined) => {
Expand Down
21 changes: 7 additions & 14 deletions packages/x/tests/useIntegerField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ import React from 'react';
import { ReactiveFormProvider, useForm } from '@reactive-forms/core';
import { act, renderHook, waitFor } from '@testing-library/react';

import { IntegerFieldI18n, IntegerFieldI18nContextProvider } from '../src/IntegerFieldI18n';
import {
defaultInvalidInputError,
defaultMaxValueError,
defaultMinValueError,
defaultRequiredError,
IntegerFieldConfig,
useIntegerField,
} from '../src/useIntegerField';
import { defaultIntegerFieldI18n, IntegerFieldI18n, IntegerFieldI18nContextProvider } from '../src/IntegerFieldI18n';
import { IntegerFieldConfig, useIntegerField } from '../src/useIntegerField';

type Config = Omit<IntegerFieldConfig, 'name'> & {
initialValue?: number | null;
Expand Down Expand Up @@ -64,7 +57,7 @@ describe('Integer field', () => {

await waitFor(() => {
expect(result.current.value).toBe(0);
expect(result.current.meta.error?.$error).toBe(defaultInvalidInputError);
expect(result.current.meta.error?.$error).toBe(defaultIntegerFieldI18n.invalidInput);
});

act(() => {
Expand All @@ -73,7 +66,7 @@ describe('Integer field', () => {

await waitFor(() => {
expect(result.current.value).toBe(0);
expect(result.current.meta.error?.$error).toBe(defaultInvalidInputError);
expect(result.current.meta.error?.$error).toBe(defaultIntegerFieldI18n.invalidInput);
});

act(() => {
Expand All @@ -94,7 +87,7 @@ describe('Integer field', () => {
});

await waitFor(() => {
expect(result.current.meta.error?.$error).toBe(defaultRequiredError);
expect(result.current.meta.error?.$error).toBe(defaultIntegerFieldI18n.required);
});
});

Expand All @@ -106,7 +99,7 @@ describe('Integer field', () => {
});

await waitFor(() => {
expect(result.current.meta.error?.$error).toBe(defaultMinValueError(0));
expect(result.current.meta.error?.$error).toBe(defaultIntegerFieldI18n.minValue(0));
});

act(() => {
Expand All @@ -126,7 +119,7 @@ describe('Integer field', () => {
});

await waitFor(() => {
expect(result.current.meta.error?.$error).toBe(defaultMaxValueError(0));
expect(result.current.meta.error?.$error).toBe(defaultIntegerFieldI18n.maxValue(0));
});

act(() => {
Expand Down

0 comments on commit e0e5bf9

Please sign in to comment.