11import { Formik } from 'formik' ;
22import React from 'react' ;
3- import { Form , Input } from '../src' ;
3+ import { Form , Input , ResetButton , SubmitButton } from '../src' ;
44import { render , waitFor } from '@testing-library/react' ;
55import userEvent from '@testing-library/user-event' ;
6+ import { LabelProps , SemanticShorthandItem } from 'semantic-ui-react' ;
7+ import * as Yup from 'yup' ;
68
79interface Props {
810 label ?: string ;
11+ inputLabel ?: SemanticShorthandItem < LabelProps > ;
912 value : string ;
1013 validate ?: any ;
14+ validationSchema ?: any ;
1115}
1216
13- const Container = ( { label, value, validate } : Props ) => {
17+ const Container = ( {
18+ label,
19+ inputLabel,
20+ value,
21+ validate,
22+ validationSchema,
23+ } : Props ) => {
1424 return (
15- < Formik initialValues = { { field : value } } onSubmit = { ( ) => { } } >
25+ < Formik
26+ initialValues = { { field : value } }
27+ validationSchema = { validationSchema }
28+ onSubmit = { ( ) => { } }
29+ >
1630 < Form >
1731 < Input
32+ id = "input-test"
1833 data-testid = "input"
1934 name = "field"
2035 label = { label }
36+ inputLabel = { inputLabel }
2137 validate = { validate }
38+ errorPrompt
2239 />
40+ < SubmitButton content = "Submit" />
41+ < ResetButton content = "Reset" />
2342 </ Form >
2443 </ Formik >
2544 ) ;
2645} ;
2746
2847describe ( 'Input' , ( ) => {
2948 it ( 'should render Input component' , async function ( ) {
30- const { getByTestId, getByText, getByRole } = render (
49+ const { getByTestId, getByText, getByRole, getByLabelText } = render (
3150 < Container label = "Testing" value = "value" /> ,
3251 ) ;
3352 expect ( getByTestId ( 'input' ) ) . toBeInTheDocument ( ) ;
53+ // Get input element by label text
54+ expect ( getByLabelText ( 'Testing' ) ) . toBeInTheDocument ( ) ;
3455 expect ( getByRole ( 'textbox' ) ) . toHaveValue ( 'value' ) ;
3556 expect ( getByText ( 'Testing' ) ) . toBeInTheDocument ( ) ;
3657 } ) ;
3758
59+ it ( 'should render Input label' , function ( ) {
60+ const { getByLabelText, getByText } = render (
61+ < Container inputLabel = { 'Testing' } value = { '' } /> ,
62+ ) ;
63+ expect ( getByText ( 'Testing' ) ) . toBeInTheDocument ( ) ;
64+ // Get input element by label text
65+ expect ( getByLabelText ( 'Testing' ) ) . toBeInTheDocument ( ) ;
66+ } ) ;
67+
3868 it ( 'should change value' , async function ( ) {
3969 const { getByRole } = render ( < Container value = "" /> ) ;
4070 expect ( getByRole ( 'textbox' ) ) . toHaveValue ( '' ) ;
@@ -48,4 +78,27 @@ describe('Input', () => {
4878 await userEvent . type ( getByRole ( 'textbox' ) , 'new value' ) ;
4979 await waitFor ( ( ) => expect ( validate ) . toBeCalledTimes ( 'new value' . length ) ) ;
5080 } ) ;
81+
82+ it ( 'should reset value after reset button is clicked' , async function ( ) {
83+ const { getByRole } = render ( < Container value = { '' } /> ) ;
84+ expect ( getByRole ( 'textbox' ) ) . toHaveValue ( '' ) ;
85+ await userEvent . type ( getByRole ( 'textbox' ) , 'new value' ) ;
86+ await waitFor ( ( ) => expect ( getByRole ( 'textbox' ) ) . toHaveValue ( 'new value' ) ) ;
87+ await userEvent . click ( getByRole ( 'button' , { name : 'Reset' } ) ) ;
88+ await waitFor ( ( ) => expect ( getByRole ( 'textbox' ) ) . toHaveValue ( '' ) ) ;
89+ } ) ;
90+
91+ it ( 'should show error prompt if input is invalid' , async function ( ) {
92+ const validationSchema = Yup . object ( {
93+ field : Yup . string ( ) . required ( 'Required' ) ,
94+ } ) ;
95+ const { getByRole } = render (
96+ < Container value = { '' } validationSchema = { validationSchema } /> ,
97+ ) ;
98+ expect ( getByRole ( 'button' , { name : 'Submit' } ) ) . toBeInTheDocument ( ) ;
99+ await userEvent . click ( getByRole ( 'button' , { name : 'Submit' } ) ) ;
100+ await waitFor ( ( ) =>
101+ expect ( getByRole ( 'alert' ) ) . toHaveTextContent ( 'Required' ) ,
102+ ) ;
103+ } ) ;
51104} ) ;
0 commit comments