Skip to content

Commit

Permalink
fix(): #34 fix empty value for customRegexp rule
Browse files Browse the repository at this point in the history
  • Loading branch information
horprogs committed Dec 31, 2021
1 parent 2aae907 commit 73cd1d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ class JustValidate {
break;
}

if (!regexp.test(String(elemValue))) {
const str = String(elemValue);

if (str !== '' && !regexp.test(str)) {
this.setFieldInvalid(field, fieldRule);
}

Expand Down
31 changes: 29 additions & 2 deletions src/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1296,15 +1296,17 @@ describe('Validation', () => {
expect(getElem('button')).toBeEnabled();
});

expect(onSubmit).not.toHaveBeenCalled();
expect(onSubmit).toHaveBeenCalled();
onSubmit.mockReset();
expect(getElemByTestId('error-label-#name')).not.toBeInTheDocument();

expect(getElemByTestId('error-label-#name')).toBeInTheDocument();
changeTextBySelector('#name', 'asgda');
clickBySelector('#submit-btn');
await waitFor(() => {
expect(getElem('button')).toBeEnabled();
});
expect(onSubmit).not.toHaveBeenCalled();
onSubmit.mockReset();
expect(getElemByTestId('error-label-#name')).toBeInTheDocument();

changeTextBySelector('#name', '123123sdf');
Expand All @@ -1313,6 +1315,7 @@ describe('Validation', () => {
expect(getElem('button')).toBeEnabled();
});
expect(onSubmit).not.toHaveBeenCalled();
onSubmit.mockReset();
expect(getElemByTestId('error-label-#name')).toBeInTheDocument();

changeTextBySelector('#name', '123123sAA');
Expand All @@ -1321,6 +1324,7 @@ describe('Validation', () => {
expect(getElem('button')).toBeEnabled();
});
expect(onSubmit).not.toHaveBeenCalled();
onSubmit.mockReset();
expect(getElemByTestId('error-label-#name')).toBeInTheDocument();

changeTextBySelector('#name', 'AAAA');
Expand All @@ -1332,6 +1336,29 @@ describe('Validation', () => {
expect(onSubmit).toHaveBeenCalledTimes(1);
});
expect(getElemByTestId('error-label-#name')).toBeNull();

onSubmit.mockReset();
validation.addField('#name', [
{
rule: 'required' as Rules,
},
{
rule: 'customRegexp' as Rules,
value: /^[A-Z]+$/,
},
]);

changeTextBySelector('#name', '');

clickBySelector('#submit-btn');

await waitFor(() => {
expect(getElem('button')).toBeEnabled();
});

expect(onSubmit).not.toHaveBeenCalled();
onSubmit.mockReset();
expect(getElemByTestId('error-label-#name')).toBeInTheDocument();
});

test('should be able to change language', async () => {
Expand Down

0 comments on commit 73cd1d7

Please sign in to comment.