Skip to content

Commit

Permalink
added a test to showcase the difference in behavior by just subscribi…
Browse files Browse the repository at this point in the history
…ng to the isValid state
  • Loading branch information
Christoph Gruber committed May 22, 2024
1 parent bed340c commit a92cfe7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import FormStateWithSchema from './formStateWithSchema';
import SetValueWithSchema from './setValueWithSchema';
import SetValueWithTrigger from './setValueWithTrigger';
import IsValid from './isValid';
import IsValidBreaksValidation from './isValidBreaksValidation';
import Controller from './controller';
import UseFieldArray from './useFieldArray';
import UseFieldArrayNested from './useFieldArrayNested';
Expand Down Expand Up @@ -94,6 +95,10 @@ const App: React.FC = () => {
element={<FormStateWithSchema />}
/>
<Route path="/isValid/:mode/:defaultValues" element={<IsValid />} />
<Route
path="/isValidBreaksValidation"
element={<IsValidBreaksValidation />}
/>
<Route path="/default-values" element={<DefaultValues />} />
<Route path="/default-values-async" element={<DefaultValuesAsync />} />
<Route path="/trigger-validation" element={<TriggerValidation />} />
Expand Down
69 changes: 69 additions & 0 deletions app/src/isValidBreaksValidation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { debounce } from '@material-ui/core';
import React from 'react';
import { useForm } from 'react-hook-form';
import { useSearchParams } from 'react-router-dom';

export function debouncePromise<F extends (...args: any[]) => Promise<any>>(
func: F,
wait?: number,
) {
const debounced = debounce(
(resolve: any, reject: any, args: Parameters<F>) => {
func(...args)
.then(resolve)
.catch(reject);
},
wait,
);

const promise = (...args: Parameters<F>): ReturnType<F> =>
new Promise((resolve, reject) => {
debounced(resolve, reject, args);
}) as ReturnType<F>;

return promise;
}

function wait() {
return new Promise((resolve) =>
setTimeout(() => {
resolve(null);
}, 2000),
);
}

const IsValidBreaksValidation: React.FC = () => {
const [params] = useSearchParams();

const { register, formState } = useForm({
mode: 'onChange',
defaultValues: { password: '' },
});

const { errors } = formState;

if (params.get('useIsValid')) {
// this should be find for testing
const { isValid } = formState;
}

return (
<form>
<input
type="text"
{...register('password', {
validate: debouncePromise(async (value) => {
await wait();
if (value.length < 5) {
return 'error';
}
return undefined;
}, 1000),
})}
/>
<p>{errors.password?.message}</p>
</form>
);
};

export default IsValidBreaksValidation;
17 changes: 17 additions & 0 deletions cypress/e2e/isValidBreaksValidation.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe('isValidBreaksValidation', () => {
it('it should validate the debounced field validation', () => {
cy.visit('http://localhost:3000/isValidBreaksValidation');

cy.get('input[name="password"]').type('1234').blur();

cy.get('input[name="password"] + p').contains('error');
});

it('should still work if the isValid field state is used', () => {
cy.visit('http://localhost:3000/isValidBreaksValidation?useIsValid=true');

cy.get('input[name="password"]').type('1234').blur();

cy.get('input[name="password"] + p').contains('error');
});
});

0 comments on commit a92cfe7

Please sign in to comment.