@@ -15,21 +15,15 @@ const eventData = {
15
15
} ;
16
16
const onChangePageMock = jest . fn ( ) ;
17
17
const onScrollMock = jest . fn ( ) ;
18
- const props = {
19
- testID : 'carousel' ,
20
- initialPage : 0 ,
21
- pagingEnabled : true ,
22
- autoplay : false ,
23
- autoplayInterval : 4000 ,
24
- horizontal : true ,
25
- onChangePage : onChangePageMock ,
26
- onScroll : onScrollMock
27
- //animatedScrollOffset: // set to check Animated
28
- } ;
29
18
30
19
const TestCase = props => {
31
20
return (
32
- < Carousel { ...props } >
21
+ < Carousel
22
+ testID = { 'carousel' }
23
+ onChangePage = { onChangePageMock }
24
+ onScroll = { onScrollMock }
25
+ { ...props }
26
+ >
33
27
{ map ( [ ...Array ( numberOfPagesShown ) ] , ( _ , index ) => (
34
28
< Page key = { index } >
35
29
< Text testID = { `page-${ index } ` } > Page #{ index } </ Text >
@@ -49,29 +43,41 @@ const Page = ({children, ...others}) => {
49
43
50
44
describe ( 'Carousel render tests' , ( ) => {
51
45
52
- describe ( 'default setup' , ( ) => {
53
- it ( 'should be set to default' , ( ) => {
54
- const component = render ( < TestCase { ...props } /> ) ;
46
+ describe ( 'initialPage' , ( ) => {
47
+ it ( 'should be set to default initialPage' , ( ) => {
48
+ const component = render ( < TestCase /> ) ;
49
+ const scrollView = component . getByTestId ( 'carousel.scrollView' ) ;
50
+
51
+ expect ( scrollView . props . contentOffset . x ) . toBe ( 0 ) ;
52
+ } ) ;
55
53
56
- component . getByText ( 'Page #0' ) ; // Validates that the text is there
54
+ it ( 'should be set to initialPage = 2' , ( ) => {
55
+ const component = render ( < TestCase initialPage = { 2 } /> ) ;
56
+ const scrollView = component . getByTestId ( 'carousel.scrollView' ) ;
57
+
58
+ expect ( scrollView . props . contentOffset . x ) . toBe ( Constants . screenWidth * 2 ) ;
57
59
} ) ;
60
+ } ) ;
58
61
62
+ describe ( 'onScroll' , ( ) => {
59
63
it ( 'should trigger onScroll from the second scroll' , ( ) => {
60
- const component = render ( < TestCase { ... props } /> ) ;
64
+ const component = render ( < TestCase /> ) ;
61
65
const scrollView = component . getByTestId ( 'carousel.scrollView' ) ;
62
66
63
- fireEvent . scroll ( scrollView , eventData ) ; //NOTE: first scroll will no fire onScroll
67
+ fireEvent . scroll ( scrollView , eventData ) ; //NOTE: first scroll doesn't fire onScroll
64
68
expect ( onScrollMock ) . not . toHaveBeenCalled ( ) ;
65
69
66
70
fireEvent . scroll ( scrollView , eventData ) ;
67
71
expect ( onScrollMock ) . toHaveBeenCalled ( ) ;
68
72
} ) ;
73
+ } ) ;
69
74
75
+ describe ( 'onChangePage' , ( ) => {
70
76
it ( 'should trigger onChangePage with current page' , async ( ) => {
71
- const component = render ( < TestCase { ... props } /> ) ;
77
+ const component = render ( < TestCase /> ) ;
72
78
const scrollView = component . getByTestId ( 'carousel.scrollView' ) ;
73
79
74
- fireEvent . scroll ( scrollView , eventData ) ; //NOTE: first scroll will no fire onScroll
80
+ fireEvent . scroll ( scrollView , eventData ) ; //NOTE: first scroll doesn't fire onScroll
75
81
fireEvent . scroll ( scrollView , eventData ) ;
76
82
expect ( onChangePageMock ) . not . toHaveBeenCalled ( ) ;
77
83
0 commit comments