1- import * as React from 'react' ;
2- import { fireEvent , render , wait } from '@testing-library/react-native' ;
1+ import React from 'react' ;
2+ import { View } from 'react-native' ;
3+ import { fireEvent , render } from '@testing-library/react-native' ;
34import { SegmentedControl } from '../src/SegmentedControl' ;
45import { Segment } from '../src/Segment' ;
56
67jest . mock ( 'react-native-reanimated' , ( ) =>
78 require ( 'react-native-reanimated/mock' ) ,
89) ;
910
11+ jest . mock ( 'react-native-gesture-handler' , ( ) => {
12+ const actual = jest . requireActual ( 'react-native-gesture-handler' ) ;
13+ const { TouchableOpacity } = jest . requireActual ( 'react-native' ) ;
14+ return {
15+ ...actual ,
16+ TouchableOpacity,
17+ } ;
18+ } ) ;
19+
1020describe ( 'SegmentedControl' , ( ) => {
1121 it ( 'should render' , ( ) => {
1222 const { asJSON } = render (
@@ -18,6 +28,33 @@ describe('SegmentedControl', () => {
1828 expect ( asJSON ( ) ) . toMatchSnapshot ( ) ;
1929 } ) ;
2030
31+ it ( 'should render initially without slider, press on a segment and slider should appear' , async ( ) => {
32+ const { getByTestId, getByText } = render (
33+ < SegmentedControl >
34+ < Segment name = "first" content = "First" />
35+ < Segment name = "second" content = "Second" />
36+ </ SegmentedControl > ,
37+ ) ;
38+
39+ expect ( ( ) => getByTestId ( 'SegmentedControl_Slider' ) ) . toThrow ( ) ;
40+
41+ const secondSegment = getByText ( 'Second' ) ;
42+ fireEvent . press ( secondSegment ) ;
43+
44+ expect ( getByTestId ( 'SegmentedControl_Slider' ) ) . toBeDefined ( ) ;
45+ } ) ;
46+
47+ it ( 'should render initially with slider on `Second`' , async ( ) => {
48+ const { getByTestId } = render (
49+ < SegmentedControl initialSelectedName = "second" >
50+ < Segment name = "first" content = "First" />
51+ < Segment name = "second" content = "Second" />
52+ </ SegmentedControl > ,
53+ ) ;
54+
55+ expect ( getByTestId ( 'SegmentedControl_Slider' ) ) . toBeDefined ( ) ;
56+ } ) ;
57+
2158 it ( 'should call onChangeValue when pressed on `Test`' , async ( ) => {
2259 const changeValueSpy = jest . fn ( ) ;
2360 const { getByTestId } = render (
@@ -29,9 +66,36 @@ describe('SegmentedControl', () => {
2966 const button = getByTestId ( 'Segment_Button' ) ;
3067 fireEvent . press ( button ) ;
3168
32- await wait ( ) ;
33-
34- // TODO: Test not working.. button isnt being pressed from RNGH
3569 expect ( changeValueSpy ) . toBeCalledWith ( 'Test' ) ;
3670 } ) ;
71+
72+ it ( 'should throw if a child component is not a Segment' , ( ) => {
73+ const errorSpy = jest . spyOn ( console , 'error' ) ;
74+ errorSpy . mockImplementation ( ) ;
75+
76+ expect ( ( ) => {
77+ render (
78+ < SegmentedControl >
79+ < View />
80+ </ SegmentedControl > ,
81+ ) ;
82+ } ) . toThrow ( 'SegmentedControl only accepts Segment as children.' ) ;
83+
84+ errorSpy . mockRestore ( ) ;
85+ } ) ;
86+
87+ it ( 'should throw if a Segment has no name' , ( ) => {
88+ const errorSpy = jest . spyOn ( console , 'error' ) ;
89+ errorSpy . mockImplementation ( ) ;
90+
91+ expect ( ( ) => {
92+ render (
93+ < SegmentedControl >
94+ < Segment name = "" content = "No Name" />
95+ </ SegmentedControl > ,
96+ ) ;
97+ } ) . toThrow ( 'Segment requires `name` to be defined.' ) ;
98+
99+ errorSpy . mockRestore ( ) ;
100+ } ) ;
37101} ) ;
0 commit comments