Skip to content

Commit 785d3a2

Browse files
Implement tests for the StepsIndicator component and related
1 parent 0569bc7 commit 785d3a2

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

package/__test__/components/StepNumber.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {StepNumber} from '../../src/components/StepNumber';
44

55
describe('StepNumber', () => {
66
it('Displays number of step according to given index', () => {
7-
const {getByText} = render(<StepNumber i={0} style={undefined} />);
7+
const {getByText} = render(
8+
<StepNumber i={0} index={0} style={undefined} />,
9+
);
810
expect(getByText('0')).toBeDefined();
911
});
1012
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React from 'react';
2+
import {render} from '@testing-library/react-native';
3+
import {StepsIndicator} from '../../src/components/StepsIndicator';
4+
import {constants} from '../../src/utils/constants';
5+
import {Platform} from 'react-native';
6+
import {styles} from '../../src/utils/styles';
7+
8+
const defaultOptions = [0, 1, 2, 3, 4, 5];
9+
const longerOptions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
10+
11+
describe('StepsIndicator', () => {
12+
it('Renders every step provided in options', () => {
13+
const {getByText} = render(
14+
<StepsIndicator
15+
options={defaultOptions}
16+
sliderWidth={20}
17+
currentValue={0}
18+
renderStepNumber
19+
/>,
20+
);
21+
for (const step of defaultOptions) {
22+
expect(getByText(step.toString())).toBeDefined();
23+
}
24+
});
25+
26+
it('Applies the big font size to the lower number of steps', () => {
27+
const {getByTestId} = render(
28+
<StepsIndicator
29+
options={defaultOptions}
30+
sliderWidth={20}
31+
currentValue={0}
32+
renderStepNumber
33+
/>,
34+
);
35+
expect(getByTestId('0th-step')).toHaveStyle({
36+
fontSize: constants.STEP_NUMBER_TEXT_FONT_BIG,
37+
});
38+
});
39+
40+
it('Applies the small font size to the number of steps higher than 9', () => {
41+
const {getByTestId} = render(
42+
<StepsIndicator
43+
options={longerOptions}
44+
sliderWidth={20}
45+
currentValue={0}
46+
renderStepNumber
47+
/>,
48+
);
49+
expect(getByTestId('0th-step')).toHaveStyle({
50+
fontSize: constants.STEP_NUMBER_TEXT_FONT_SMALL,
51+
});
52+
});
53+
54+
it('Applies platform-dependent styles for web', () => {
55+
Platform.OS = 'web';
56+
const {getByTestId} = render(
57+
<StepsIndicator
58+
options={longerOptions}
59+
sliderWidth={20}
60+
currentValue={0}
61+
renderStepNumber
62+
/>,
63+
);
64+
expect(getByTestId('StepsIndicator-Container')).toHaveStyle(
65+
styles.stepsIndicator,
66+
);
67+
});
68+
69+
it('Reverts given options when isLTR is set', () => {
70+
const {getByTestId} = render(
71+
<StepsIndicator
72+
options={defaultOptions}
73+
sliderWidth={20}
74+
currentValue={0}
75+
renderStepNumber
76+
isLTR
77+
/>,
78+
);
79+
expect(getByTestId('0th-step')).toHaveTextContent('5');
80+
expect(getByTestId('2th-step')).toHaveTextContent('3');
81+
});
82+
83+
it('Does not display any step numbers if prop is not set', () => {
84+
const {queryByTestId} = render(
85+
<StepsIndicator
86+
options={defaultOptions}
87+
sliderWidth={20}
88+
currentValue={0}
89+
/>,
90+
);
91+
expect(queryByTestId('0th-step')).toBeFalsy();
92+
});
93+
});

package/src/components/StepNumber.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import {styles} from '../utils/styles';
44

55
export const StepNumber = ({
66
i,
7+
index,
78
style,
89
}: {
910
i: number;
11+
index: number;
1012
style: StyleProp<TextStyle>;
1113
}) => {
1214
return (
1315
<View style={styles.stepNumber}>
14-
<Text style={style}>{i}</Text>
16+
<Text testID={`${index}th-step`} style={style}>
17+
{i}
18+
</Text>
1519
</View>
1620
);
1721
};

package/src/components/StepsIndicator.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export const StepsIndicator = ({
7474
{renderStepNumber ? (
7575
<StepNumber
7676
i={i}
77+
index={index}
7778
style={stepNumberFontStyle}
7879
key={`${index}th-step`}
7980
/>
@@ -96,6 +97,7 @@ export const StepsIndicator = ({
9697
return (
9798
<View
9899
pointerEvents="none"
100+
testID="StepsIndicator-Container"
99101
style={platformDependentStyles.stepIndicatorContainerStyle}>
100102
{values.map((i, index) => renderStepIndicator(i, index))}
101103
</View>

0 commit comments

Comments
 (0)