From f2fa3303a684f2c98f17e401846126dedd90fa6f Mon Sep 17 00:00:00 2001 From: Zach Silveira Date: Wed, 8 Jan 2020 07:52:48 -0800 Subject: [PATCH] Add test --- tests/form.test.js | 15 +++++++++++++++ tests/helpers/form.tsx | 7 ++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/form.test.js b/tests/form.test.js index f263077..444f91f 100644 --- a/tests/form.test.js +++ b/tests/form.test.js @@ -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( + , + ); + 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' }); +}); diff --git a/tests/helpers/form.tsx b/tests/helpers/form.tsx index a915809..1cf2446 100644 --- a/tests/helpers/form.tsx +++ b/tests/helpers/form.tsx @@ -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'; @@ -14,6 +14,7 @@ const greaterThanDate2 = (value, values) => { type Props = { noRules?: boolean; + nameRule?: RuleFn; onSubmit?: (values: any) => any; }; @@ -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({ email: 'test' }); return ( @@ -36,7 +37,7 @@ export const TestForm = ({ onSubmit, noRules }: Props) => { : { email: [required, email], date1: [greaterThanDate2], - name: [required], + name: [nameRule || required], } } >