Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Jan 8, 2020
1 parent ee26a35 commit f2fa330
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 15 additions & 0 deletions tests/form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,18 @@ test('Form works without rules object passed', async () => {
//ensure the validation shows up
expect(name.value).toEqual('testing');
});

test('Empty input value gets passed as empty string to rule fn', async () => {
const spy = jest.fn();
let { queryByPlaceholderText, queryByText } = render(
<TestForm nameRule={spy} />,
);
const submit = queryByText('Submit Form');

//press the submit button
submit.click();

//ensure that the value given to the rule is an empty string if it wasnt touched
expect(spy.mock.calls[0][0]).toEqual('');
expect(spy.mock.calls[0][1]).toEqual({ email: 'test' });
});
7 changes: 4 additions & 3 deletions tests/helpers/form.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect } from 'react';
import { Form } from '../../src/form';
import { useState } from 'react';
import { required, email } from '../../src/rules';
import { required, email, RuleFn } from '../../src/rules';
import Input from './input';
import Submit from './submit';

Expand All @@ -14,6 +14,7 @@ const greaterThanDate2 = (value, values) => {

type Props = {
noRules?: boolean;
nameRule?: RuleFn;
onSubmit?: (values: any) => any;
};

Expand All @@ -23,7 +24,7 @@ type TestValues = {
name?: string;
};

export const TestForm = ({ onSubmit, noRules }: Props) => {
export const TestForm = ({ onSubmit, noRules, nameRule }: Props) => {
let [values, setValues] = useState<TestValues>({ email: 'test' });

return (
Expand All @@ -36,7 +37,7 @@ export const TestForm = ({ onSubmit, noRules }: Props) => {
: {
email: [required, email],
date1: [greaterThanDate2],
name: [required],
name: [nameRule || required],
}
}
>
Expand Down

0 comments on commit f2fa330

Please sign in to comment.