-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a test to showcase the difference in behavior by just subscribi…
…ng to the isValid state
- Loading branch information
Christoph Gruber
committed
May 22, 2024
1 parent
bed340c
commit a92cfe7
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |