Skip to content

Commit e7a333b

Browse files
authored
TextField - clean tests (#3118)
* move all driver tests to one file * delete old test file * delete old driver * NumberInput - update driver and tests * remove duplications and edit test cases * adding shouldPlaceholderFloat tests * add getColorByState tests
1 parent 8115472 commit e7a333b

File tree

6 files changed

+447
-378
lines changed

6 files changed

+447
-378
lines changed
Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
import {NumberInputProps} from './index';
2-
import {ComponentDriver, ComponentDriverArgs} from '../../testkit/Component.driver';
3-
import {TextFieldDriver} from '../textField/TextField.driver';
1+
import {ComponentProps} from '../../testkit/new/Component.driver';
2+
import {TextFieldDriver} from '../textField/TextField.driver.new';
43

5-
export class NumberInputDriver extends ComponentDriver<NumberInputProps> {
6-
private readonly maskedInputDriver: TextFieldDriver;
7-
private readonly visualTextFieldDriver: TextFieldDriver;
4+
export const NumberInputDriver = (props: ComponentProps) => {
5+
const maskedInputDriver = TextFieldDriver({
6+
renderTree: props.renderTree,
7+
testID: `${props.testID}`
8+
});
89

9-
constructor(componentDriverArgs: ComponentDriverArgs) {
10-
super(componentDriverArgs);
10+
const textFieldDriver = TextFieldDriver({
11+
renderTree: props.renderTree,
12+
testID: `${props.testID}.visual`
13+
});
14+
15+
const exists = (): boolean => {
16+
return maskedInputDriver.exists();
17+
};
1118

12-
this.maskedInputDriver = new TextFieldDriver({...componentDriverArgs, testID: `${this.testID}`});
13-
this.visualTextFieldDriver = new TextFieldDriver({...componentDriverArgs, testID: `${this.testID}.visual`});
14-
}
19+
const changeText = async (text: string) => {
20+
await maskedInputDriver.changeText(text);
21+
};
1522

16-
changeText = async (text: string) => {
17-
await this.maskedInputDriver.changeText(text);
23+
const getValue = (): string | undefined => {
24+
return textFieldDriver.getValue();
1825
};
1926

20-
getText = async () => {
21-
return await this.visualTextFieldDriver.getText();
27+
return {
28+
exists,
29+
changeText,
30+
getValue
2231
};
23-
}
32+
};
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import React from 'react';
2-
import NumberInput from '../index';
2+
import {render} from '@testing-library/react-native';
33
import {NumberInputDriver} from '../NumberInput.driver';
4+
import NumberInput from '../index';
45

6+
const TEST_ID = 'numberInput';
57
const onChangeNumber = () => {};
68

79
const defaultProps = {
8-
testID: 'field',
10+
testID: TEST_ID,
911
placeholder: 'Placeholder',
1012
centered: true,
1113
onChangeNumber
@@ -16,22 +18,24 @@ const TestCase = props => {
1618
};
1719

1820
describe('NumberInput', () => {
19-
it('Should update number when fractionDigits changes', async () => {
20-
const component = <TestCase/>;
21-
const numberInputDriver = new NumberInputDriver({component, testID: 'field'});
22-
expect(await numberInputDriver.exists()).toBe(true);
21+
it('Should display formatted number', async () => {
22+
const renderTree = render(<TestCase/>);
23+
const numberInputDriver = NumberInputDriver({renderTree, testID: TEST_ID});
24+
25+
expect(numberInputDriver.exists()).toBe(true);
26+
27+
numberInputDriver.changeText('1234567');
28+
expect(await numberInputDriver.getValue()).toEqual('12,345.67');
29+
});
30+
31+
it('Should update number when fractionDigits change', async () => {
32+
const renderTree = render(<TestCase/>);
33+
const numberInputDriver = NumberInputDriver({renderTree, testID: TEST_ID});
34+
2335
numberInputDriver.changeText('1234567');
24-
expect(await numberInputDriver.getText()).toEqual('12,345.67');
25-
// TODO: add changing fractionDigits once we support rerender in our drivers
26-
27-
28-
// const renderTree = render(<TestCase/>);
29-
// const input = renderTree.getByTestId('field');
30-
// fireEvent(input, 'focus');
31-
// fireEvent.changeText(input, '1234567');
32-
// fireEvent(input, 'blur');
33-
// renderTree.getByDisplayValue('1,234.67');
34-
// renderTree.rerender(<TestCase fractionDigits={3}/>);
35-
// renderTree.getByDisplayValue('123.457');
36+
expect(await numberInputDriver.getValue()).toEqual('12,345.67');
37+
38+
renderTree.rerender(<TestCase fractionDigits={4}/>);
39+
expect(await numberInputDriver.getValue()).toEqual('123.4567');
3640
});
3741
});

src/components/textField/TextField.driver.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/components/textField/__tests__/Presenter.spec.js

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,100 @@
1+
import _ from 'lodash';
2+
import Colors from '../../../style/colors';
13
import * as uut from '../Presenter';
24

35
describe('TextField:Presenter', () => {
6+
describe('get color by state', () => {
7+
const colorsByState = {
8+
default: 'purple',
9+
focus: 'green',
10+
error: 'orange',
11+
readonly: 'pink',
12+
disabled: 'lightblue'
13+
};
14+
15+
const defaultContext = {
16+
isFocused: false,
17+
hasValue: false,
18+
isValid: true,
19+
failingValidatorIndex: undefined,
20+
disabled: false,
21+
readonly: false,
22+
validateField: _.noop,
23+
checkValidity: () => true,
24+
isMandatory: false
25+
};
26+
27+
it('should return passed color when is a string', () => {
28+
expect(uut.getColorByState('blue', defaultContext)).toEqual('blue');
29+
});
30+
31+
it('should return passed color when is design token', () => {
32+
expect(uut.getColorByState(Colors.$backgroundDark, defaultContext)).toEqual(Colors.$backgroundDark);
33+
});
34+
35+
it('should return textDefault color when color is an empty object', () => {
36+
expect(uut.getColorByState({}, defaultContext)).toEqual(Colors.$textDefault);
37+
});
38+
39+
it('should return textDefault color when no default color passed in color object', () => {
40+
expect(uut.getColorByState({error: 'red'}, defaultContext)).toEqual(Colors.$textDefault);
41+
});
42+
43+
it('should return default color', () => {
44+
expect(uut.getColorByState(colorsByState, defaultContext)).toEqual(colorsByState.default);
45+
});
46+
47+
it('should return focused color', () => {
48+
const context = {...defaultContext, isFocused: true};
49+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.focus);
50+
});
51+
52+
it('should return error color', () => {
53+
const context = {...defaultContext, isValid: false};
54+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.error);
55+
});
56+
57+
it('should return readonly color', () => {
58+
const context = {...defaultContext, readonly: true};
59+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.readonly);
60+
});
61+
62+
it('should return disabled color', () => {
63+
const context = {...defaultContext, disabled: true};
64+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.disabled);
65+
});
66+
67+
it('should return disabled color, over readonly', () => {
68+
const context = {...defaultContext, disabled: true, readonly: true};
69+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.disabled);
70+
});
71+
72+
it('should return disabled color, over error', () => {
73+
const context = {...defaultContext, disabled: true, isValid: false};
74+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.disabled);
75+
});
76+
77+
it('should return disabled color, over focused', () => {
78+
const context = {...defaultContext, disabled: true, isFocused: false};
79+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.disabled);
80+
});
81+
82+
it('should return readonly color, over error', () => {
83+
const context = {...defaultContext, readonly: true, isValid: false};
84+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.readonly);
85+
});
86+
87+
it('should return readonly color, over focused', () => {
88+
const context = {...defaultContext, readonly: true, isFocused: false};
89+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.readonly);
90+
});
91+
92+
it('should return error color, over focused', () => {
93+
const context = {...defaultContext, isValid: false, isFocused: true};
94+
expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.error);
95+
});
96+
});
97+
498
describe('validate', () => {
599
it('should return true if validator is undefined', () => {
6100
expect(uut.validate('value', undefined)).toEqual([true, undefined]);
@@ -77,4 +171,70 @@ describe('TextField:Presenter', () => {
77171
expect(uut.shouldHidePlaceholder({floatingPlaceholder: true, hint: 'Hint text', floatOnFocus: false}, true)).toBe(true);
78172
});
79173
});
174+
175+
describe('Should float placeholder', () => {
176+
it('should not float when isFocused is false', () => {
177+
const props = {floatOnFocus: true};
178+
const isFocused = false;
179+
const hasValue = undefined;
180+
const value = undefined;
181+
expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false);
182+
});
183+
184+
it('should not float when isFocused is false', () => {
185+
const props = {floatOnFocus: false};
186+
const isFocused = true;
187+
const hasValue = undefined;
188+
const value = undefined;
189+
expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false);
190+
});
191+
192+
it('should float when floatOnFocus and isFocused is true', () => {
193+
const props = {floatOnFocus: true};
194+
const isFocused = true;
195+
const hasValue = undefined;
196+
const value = undefined;
197+
expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(true);
198+
});
199+
200+
it('should float when hasValue is true', () => {
201+
const props = {floatOnFocus: false};
202+
const isFocused = false;
203+
const hasValue = true;
204+
const value = undefined;
205+
expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(true);
206+
});
207+
208+
it('should not float when hasValue is false', () => {
209+
const props = {floatOnFocus: false};
210+
const isFocused = false;
211+
const hasValue = false;
212+
const value = undefined;
213+
expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false);
214+
});
215+
216+
it('should float when defaultValue and value is undefined', () => {
217+
const props = {floatOnFocus: false, defaultValue: 'default value'};
218+
const isFocused = false;
219+
const hasValue = false;
220+
const value = undefined;
221+
expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(true);
222+
});
223+
224+
it('should not float when defaultValue and value', () => {
225+
const props = {floatOnFocus: false, defaultValue: 'default value'};
226+
const isFocused = false;
227+
const hasValue = false;
228+
const value = 'value';
229+
expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false);
230+
});
231+
232+
it('should not float when no defaultValue and value', () => {
233+
const props = {floatOnFocus: false, defaultValue: undefined};
234+
const isFocused = false;
235+
const hasValue = false;
236+
const value = 'value';
237+
expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false);
238+
});
239+
});
80240
});

0 commit comments

Comments
 (0)