|
| 1 | +import React, {useState} from 'react'; |
| 2 | +import TextFieldTestKit from '../TextField.driver'; |
| 3 | +import {render, waitFor} from '@testing-library/react-native'; |
| 4 | +import TextField from '../index'; |
| 5 | +import View from '../../../components/view'; |
| 6 | + |
| 7 | +const TEXT_FIELD_TEST_ID = 'text_field_test_id'; |
| 8 | + |
| 9 | +function renderWrapperScreenWithTextField(textFieldProps) { |
| 10 | + return render(<ScreenWithTextField {...textFieldProps}/>); |
| 11 | +} |
| 12 | + |
| 13 | +function ScreenWithTextField(textFieldProps) { |
| 14 | + const [value, setValue] = useState(textFieldProps.value); |
| 15 | + return (<View> |
| 16 | + <TextField {...textFieldProps} testID={TEXT_FIELD_TEST_ID} value={value} onChangeText={setValue}/> |
| 17 | + </View>); |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +describe('TextField', () => { |
| 22 | + it('should render textField', async () => { |
| 23 | + const wrapperComponent = renderWrapperScreenWithTextField({}); |
| 24 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 25 | + expect(textFieldDriver.exists()).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should render textField with correct content', async () => { |
| 29 | + const wrapperComponent = renderWrapperScreenWithTextField({value: 'aa'}); |
| 30 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 31 | + expect(textFieldDriver.content()).toEqual('aa'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should change the text correctly', async () => { |
| 35 | + const wrapperComponent = renderWrapperScreenWithTextField({value: 'aa'}); |
| 36 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 37 | + expect(textFieldDriver.content()).toEqual('aa'); |
| 38 | + textFieldDriver.changeText('bb'); |
| 39 | + await waitFor(() => expect(textFieldDriver.content()).toEqual('bb')); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('editable', () => { |
| 43 | + it('should be editable', async () => { |
| 44 | + const wrapperComponent = renderWrapperScreenWithTextField({}); |
| 45 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 46 | + expect(textFieldDriver.isDisabled()).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should render textField that is not editable', async () => { |
| 50 | + const wrapperComponent = renderWrapperScreenWithTextField({editable: false}); |
| 51 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 52 | + expect(textFieldDriver.isDisabled()).toBe(true); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('placeholder', () => { |
| 57 | + it('should render placeholder with correct text', async () => { |
| 58 | + const wrapperComponent = renderWrapperScreenWithTextField({placeholder: 'mock placeholder'}); |
| 59 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 60 | + expect(textFieldDriver.isPlaceholderVisible()).toBe(true); |
| 61 | + expect(textFieldDriver.getPlaceholderContent()).toEqual('mock placeholder'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should not render placeholder', async () => { |
| 65 | + const wrapperComponent = renderWrapperScreenWithTextField({}); |
| 66 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 67 | + expect(textFieldDriver.isPlaceholderVisible()).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should not render placeholder after user changing the input text(no floating prop)', async () => { |
| 71 | + const wrapperComponent = renderWrapperScreenWithTextField({placeholder: 'mock placeholder'}); |
| 72 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 73 | + expect(textFieldDriver.isPlaceholderVisible()).toBe(true); |
| 74 | + textFieldDriver.changeText('mock input value'); |
| 75 | + await waitFor(() => expect(textFieldDriver.isPlaceholderVisible()).toBe(false)); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should render placeholder(floating) after user changing text if floatingPlaceholder prop sent', async () => { |
| 79 | + const wrapperComponent = renderWrapperScreenWithTextField({placeholder: 'mock placeholder', floatingPlaceholder: true}); |
| 80 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 81 | + expect(textFieldDriver.isPlaceholderVisible()).toBe(true); |
| 82 | + textFieldDriver.changeText('mock input value'); |
| 83 | + await waitFor(() => expect(textFieldDriver.isPlaceholderVisible()).toBe(true)); |
| 84 | + await waitFor(() => expect(textFieldDriver.getPlaceholderContent()).toEqual('mock placeholder')); |
| 85 | + |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('Label', () => { |
| 90 | + it('should not render label if prop is not passed', async () => { |
| 91 | + const wrapperComponent = renderWrapperScreenWithTextField({}); |
| 92 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 93 | + expect(textFieldDriver.isLabelExists()).toBe(false); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should render a label', async () => { |
| 97 | + const wrapperComponent = renderWrapperScreenWithTextField({label: 'mock label'}); |
| 98 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 99 | + expect(textFieldDriver.isLabelExists()).toBe(true); |
| 100 | + expect(textFieldDriver.getLabelContent()).toEqual('mock label'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not render label if floatingPlaceholder prop is passed', async () => { |
| 104 | + const wrapperComponent = renderWrapperScreenWithTextField({label: 'mock label', floatingPlaceholder: true}); |
| 105 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 106 | + expect(textFieldDriver.isLabelExists()).toBe(false); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('validation message', () => { |
| 111 | + it('should not render validationMessage if enableErrors prop not supplied', async () => { |
| 112 | + const wrapperComponent = renderWrapperScreenWithTextField({value: '', validate: 'required', validationMessage: 'mock message', validateOnStart: true}); |
| 113 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 114 | + expect(textFieldDriver.isValidationMsgExists()).toBe(false); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should render validationMessage on start if input required and validateOnStart passed', async () => { |
| 118 | + const wrapperComponent = renderWrapperScreenWithTextField({value: '', validate: 'required', validationMessage: 'mock message', enableErrors: true, validateOnStart: true}); |
| 119 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 120 | + expect(textFieldDriver.isValidationMsgExists()).toBe(true); |
| 121 | + expect(textFieldDriver.getValidationMsgContent()).toEqual('mock message'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should render validationMessage when input is requires after changing the input to empty string', async () => { |
| 125 | + const wrapperComponent = renderWrapperScreenWithTextField({value: 'mock value', validate: 'required', validationMessage: 'mock message', enableErrors: true, validateOnChange: true}); |
| 126 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 127 | + expect(textFieldDriver.isValidationMsgExists()).toBe(false); |
| 128 | + expect(textFieldDriver.getValidationMsgContent()).toEqual(''); |
| 129 | + textFieldDriver.changeText(''); |
| 130 | + await waitFor(() => expect(textFieldDriver.isValidationMsgExists()).toBe(true)); |
| 131 | + expect(textFieldDriver.getValidationMsgContent()).toEqual('mock message'); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('char counter', () => { |
| 136 | + it('should render char counter.', async () => { |
| 137 | + const wrapperComponent = renderWrapperScreenWithTextField({showCharCounter: true, maxLength: 10}); |
| 138 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 139 | + expect(textFieldDriver.isCharCounterExists()).toBe(true); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should not render counter if maxLength prop not supplied', async () => { |
| 143 | + const wrapperComponent = renderWrapperScreenWithTextField({showCharCounter: true}); |
| 144 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 145 | + expect(textFieldDriver.isCharCounterExists()).toBe(false); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should not render counter if showCharCounter prop not supplied', async () => { |
| 149 | + const wrapperComponent = renderWrapperScreenWithTextField({maxLength: 10}); |
| 150 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 151 | + expect(textFieldDriver.isCharCounterExists()).toBe(false); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should render char counter, with "0/10" if value not supplied', async () => { |
| 155 | + const wrapperComponent = renderWrapperScreenWithTextField({showCharCounter: true, maxLength: 10}); |
| 156 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 157 | + expect(textFieldDriver.getCharCounterContent()).toEqual('0/10'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should render char counter with correct content supplied', async () => { |
| 161 | + const wrapperComponent = renderWrapperScreenWithTextField({value: 'abc', showCharCounter: true, maxLength: 10}); |
| 162 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 163 | + expect(textFieldDriver.getCharCounterContent()).toEqual('3/10'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should update char counter after changing the text', async () => { |
| 167 | + const wrapperComponent = renderWrapperScreenWithTextField({value: 'ab', showCharCounter: true, maxLength: 10}); |
| 168 | + const textFieldDriver = await TextFieldTestKit({wrapperComponent, testID: TEXT_FIELD_TEST_ID}); |
| 169 | + expect(textFieldDriver.getCharCounterContent()).toEqual('2/10'); |
| 170 | + textFieldDriver.changeText('abcd'); |
| 171 | + await waitFor(() => expect(textFieldDriver.getCharCounterContent()).toEqual('4/10')); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments