|
| 1 | +import 'reflect-metadata'; |
| 2 | +import React, {FunctionComponent, useState} from "react"; |
| 3 | +import {create} from "react-test-renderer"; |
| 4 | +import {useValidation, ValidatorProvider} from "../index"; |
| 5 | +import {mount} from "enzyme"; |
| 6 | +import {IsNotEmpty} from "class-validator"; |
| 7 | +import toJson from "enzyme-to-json"; |
| 8 | + |
| 9 | +class ContactFormValidation { |
| 10 | + |
| 11 | + @IsNotEmpty({ |
| 12 | + message: 'First name cannot be empty' |
| 13 | + }) |
| 14 | + public firstName: string; |
| 15 | + |
| 16 | + @IsNotEmpty({ |
| 17 | + message: 'Last name cannot be empty' |
| 18 | + }) |
| 19 | + public lastName: string; |
| 20 | + |
| 21 | +} |
| 22 | + |
| 23 | +const ContactForm: FunctionComponent = () => { |
| 24 | + |
| 25 | + const [firstName, setFirstName] = useState(''); |
| 26 | + const [lastName, setLastName] = useState(''); |
| 27 | + |
| 28 | + const [validate, errorMessages] = useValidation(ContactFormValidation); |
| 29 | + |
| 30 | + return ( |
| 31 | + <form id="form" onSubmit={async (evt) => { |
| 32 | + evt.preventDefault(); |
| 33 | + await validate({firstName, lastName}); |
| 34 | + }}> |
| 35 | + <input id="fname-input" value={firstName} onChange={({target: {value}}) => setFirstName(value)} |
| 36 | + onBlur={() => validate({firstName}, ['firstName'])}/> |
| 37 | + {errorMessages.firstName && errorMessages.firstName.map((error, index) => ( |
| 38 | + <strong key={index}>{error}</strong> |
| 39 | + ))} |
| 40 | + <input id="lname-input" value={lastName} onChange={({target: {value}}) => setLastName(value)} |
| 41 | + onBlur={() => validate({lastName}, ['lastName'])}/> |
| 42 | + {errorMessages.lastName && errorMessages.lastName.map((error, index) => ( |
| 43 | + <strong key={index}>{error}</strong> |
| 44 | + ))} |
| 45 | + </form> |
| 46 | + ); |
| 47 | + |
| 48 | +}; |
| 49 | + |
| 50 | +describe('context', () => { |
| 51 | + |
| 52 | + it('provider should mount correctly', () => { |
| 53 | + |
| 54 | + const tree = create( |
| 55 | + <ValidatorProvider options={{resultType: 'map'}}> |
| 56 | + <ContactForm/> |
| 57 | + </ValidatorProvider> |
| 58 | + ).toJSON(); |
| 59 | + |
| 60 | + expect(tree).toMatchSnapshot(); |
| 61 | + |
| 62 | + }); |
| 63 | + |
| 64 | + it('validation success on form submit', async () => { |
| 65 | + |
| 66 | + const wrapper = mount( |
| 67 | + <ValidatorProvider options={{resultType: 'map'}}> |
| 68 | + <ContactForm/> |
| 69 | + </ValidatorProvider> |
| 70 | + ); |
| 71 | + |
| 72 | + const firstNameInput = wrapper.find('#fname-input'); |
| 73 | + firstNameInput.simulate('change', {target: {value: 'Nick'}}); |
| 74 | + |
| 75 | + const lastNameInput = wrapper.find('#lname-input'); |
| 76 | + lastNameInput.simulate('change', {target: {value: 'Fury'}}); |
| 77 | + |
| 78 | + const form = wrapper.find('#form'); |
| 79 | + form.simulate('submit'); |
| 80 | + |
| 81 | + expect(toJson(wrapper)).toMatchSnapshot(); |
| 82 | + |
| 83 | + }); |
| 84 | + |
| 85 | + it('validation error on form submit', async () => { |
| 86 | + |
| 87 | + const wrapper = mount( |
| 88 | + <ValidatorProvider options={{resultType: 'map'}}> |
| 89 | + <ContactForm/> |
| 90 | + </ValidatorProvider> |
| 91 | + ); |
| 92 | + |
| 93 | + const firstNameInput = wrapper.find('#fname-input'); |
| 94 | + firstNameInput.simulate('change', {target: {value: 'Nick'}}); |
| 95 | + |
| 96 | + const form = wrapper.find('#form'); |
| 97 | + form.simulate('submit'); |
| 98 | + |
| 99 | + expect(toJson(wrapper)).toMatchSnapshot(); |
| 100 | + |
| 101 | + }); |
| 102 | + |
| 103 | + it('validation error on blur field', async () => { |
| 104 | + |
| 105 | + const wrapper = mount( |
| 106 | + <ValidatorProvider options={{resultType: 'map'}}> |
| 107 | + <ContactForm/> |
| 108 | + </ValidatorProvider> |
| 109 | + ); |
| 110 | + |
| 111 | + const firstNameInput = wrapper.find('#fname-input'); |
| 112 | + firstNameInput.simulate('blur'); |
| 113 | + |
| 114 | + expect(toJson(wrapper)).toMatchSnapshot(); |
| 115 | + |
| 116 | + }); |
| 117 | + |
| 118 | + it('validation error custom handler', async () => { |
| 119 | + |
| 120 | + const wrapper = mount( |
| 121 | + <ValidatorProvider options={{ |
| 122 | + onErrorMessage() { |
| 123 | + return ['this is a custom error'] |
| 124 | + }, |
| 125 | + resultType: 'map', |
| 126 | + }}> |
| 127 | + <ContactForm/> |
| 128 | + </ValidatorProvider> |
| 129 | + ); |
| 130 | + |
| 131 | + const form = wrapper.find('#form'); |
| 132 | + form.simulate('submit'); |
| 133 | + |
| 134 | + expect(toJson(wrapper)).toMatchSnapshot(); |
| 135 | + |
| 136 | + }); |
| 137 | + |
| 138 | +}); |
0 commit comments