|
1 | 1 | import React from 'react'; |
| 2 | +import { TextStyle } from 'react-native'; |
| 3 | +import LinearGradient from 'react-native-linear-gradient'; |
2 | 4 | import { render } from '@testing-library/react-native'; |
3 | | - |
4 | 5 | import { LinearGradientText } from '../components/LinearGradientText'; |
5 | 6 |
|
6 | 7 | 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' }); |
10 | 77 | }); |
11 | 78 | }); |
0 commit comments