Skip to content

Commit b336767

Browse files
test: robust testing
1 parent fed687a commit b336767

2 files changed

Lines changed: 80 additions & 7 deletions

File tree

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,78 @@
11
import React from 'react';
2+
import { TextStyle } from 'react-native';
3+
import LinearGradient from 'react-native-linear-gradient';
24
import { render } from '@testing-library/react-native';
3-
45
import { LinearGradientText } from '../components/LinearGradientText';
56

67
describe('LinearGradientText', () => {
7-
it('renders correctly', () => {
8-
const { toJSON } = render(<LinearGradientText text="Hello" colors={['#fff', '#fff']} />);
9-
expect(toJSON()).toMatchSnapshot();
8+
it('renders correctly with default props', () => {
9+
const { getByTestId } = render(<LinearGradientText text="Hello" colors={['#fff', '#000']} />);
10+
11+
const textElement = getByTestId('gradient-text');
12+
expect(textElement).toBeTruthy();
13+
expect(textElement.props.children).toBe('Hello');
14+
15+
const gradientElement = getByTestId('gradient-element');
16+
17+
// Integer values ​​corresponding to#fff and #000
18+
const expectedColors = [4294967295, 4278190080];
19+
expect(gradientElement.props.colors).toEqual(expectedColors);
20+
});
21+
22+
it('applies custom textStyle', () => {
23+
const customStyle: TextStyle = { fontSize: 20, fontWeight: 'bold' };
24+
const { getByTestId } = render(
25+
<LinearGradientText text="Styled Text" colors={['#ff0', '#f0f']} textStyle={customStyle} />
26+
);
27+
28+
const textElement = getByTestId('gradient-text');
29+
expect(textElement.props.style).toContainEqual(customStyle);
30+
});
31+
32+
it('applies custom textProps', () => {
33+
const { getByTestId } = render(
34+
<LinearGradientText
35+
text="Props Test"
36+
colors={['#ff0', '#0ff']}
37+
textProps={{ numberOfLines: 1 }}
38+
/>
39+
);
40+
41+
const textElement = getByTestId('gradient-text');
42+
expect(textElement.props.numberOfLines).toBe(1);
43+
});
44+
45+
it('renders with custom gradient start and end', () => {
46+
const { getByTestId } = render(
47+
<LinearGradientText
48+
text="Gradient Test"
49+
colors={['#123456', '#654321']}
50+
start={{ x: 0.2, y: 0.8 }}
51+
end={{ x: 0.8, y: 0.2 }}
52+
/>
53+
);
54+
55+
const textElement = getByTestId('gradient-text');
56+
expect(textElement).toBeTruthy();
57+
});
58+
59+
it('renders gradient with provided colors', () => {
60+
const { UNSAFE_getAllByType } = render(
61+
<LinearGradientText text="Color Test" colors={['#111111', '#222222', '#333333']} />
62+
);
63+
64+
const expectedColors = ['#111111', '#222222', '#333333'];
65+
66+
const gradients = UNSAFE_getAllByType(LinearGradient);
67+
expect(gradients[0].props.colors).toEqual(expectedColors);
68+
});
69+
70+
it('applies maskText styling correctly', () => {
71+
const { getByTestId } = render(
72+
<LinearGradientText text="Masked Text" colors={['#000', '#fff']} />
73+
);
74+
75+
const maskedText = getByTestId('mask-text');
76+
expect(maskedText.props.style).toContainEqual({ backgroundColor: 'transparent' });
1077
});
1178
});

src/components/LinearGradientText.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ export const LinearGradientText: FC<Props> = (props) => {
2424
} = props;
2525

2626
return (
27-
<MaskedView maskElement={<Text style={[styles.maskText, textStyle]}>{text}</Text>}>
28-
<LinearGradient colors={colors} start={start} end={end}>
29-
<Text style={[styles.text, textStyle]} {...textProps}>
27+
<MaskedView
28+
maskElement={
29+
<Text style={[styles.maskText, textStyle]} testID="mask-text">
30+
{text}
31+
</Text>
32+
}
33+
>
34+
<LinearGradient colors={colors} start={start} end={end} testID="gradient-element">
35+
<Text style={[styles.text, textStyle]} testID="gradient-text" {...textProps}>
3036
{text}
3137
</Text>
3238
</LinearGradient>

0 commit comments

Comments
 (0)