1
1
import React , { Fragment } from 'react' ;
2
2
import { render } from 'react-dom' ;
3
- import { expect } from 'chai' ;
3
+ import chai , { expect } from 'chai' ;
4
+ import sinon from 'sinon' ;
5
+ import sinonChai from 'sinon-chai' ;
4
6
import { Simulate } from 'react-dom/test-utils' ;
5
7
import Carousel from '../../src/index' ;
6
8
7
9
function renderToJsdom ( component ) {
8
10
return render ( component , window . document . querySelector ( '#root' ) ) ;
9
11
}
10
12
13
+ chai . use ( sinonChai ) ;
11
14
let imagesFetched ;
12
15
13
16
global . Image = class MyImage {
@@ -57,6 +60,7 @@ describe('Carousel', () => {
57
60
expect ( dots . length ) . to . equal ( 3 ) ;
58
61
expect ( dots [ 0 ] . className ) . to . contain ( 'selected' ) ;
59
62
const nextButton = document . querySelector ( '.carousel-right-arrow' ) ;
63
+ expect ( nextButton . className ) . to . contain ( 'carousel-arrow-default' ) ;
60
64
Simulate . click ( nextButton ) ;
61
65
expect ( dots [ 0 ] . className ) . to . not . contain ( 'selected' ) ;
62
66
expect ( dots [ 1 ] . className ) . to . contain ( 'selected' ) ;
@@ -65,8 +69,14 @@ describe('Carousel', () => {
65
69
} ) ;
66
70
67
71
it ( 'should navigate to the previous slide when the button is clicked' , done => {
72
+ const onSlideTransitionedStub = sinon . stub ( ) ;
73
+
68
74
renderToJsdom (
69
- < Carousel initialSlide = { 1 } slideWidth = '300px' viewportWidth = '300px' infinite = { false } >
75
+ < Carousel initialSlide = { 1 }
76
+ slideWidth = '300px'
77
+ viewportWidth = '300px'
78
+ infinite = { false }
79
+ onSlideTransitioned = { onSlideTransitionedStub } >
70
80
< div id = 'slide1' />
71
81
< div id = 'slide2' />
72
82
< div id = 'slide3' />
@@ -78,16 +88,28 @@ describe('Carousel', () => {
78
88
expect ( dots . length ) . to . equal ( 3 ) ;
79
89
expect ( dots [ 1 ] . className ) . to . contain ( 'selected' ) ;
80
90
const prevButton = document . querySelector ( '.carousel-left-arrow' ) ;
91
+ expect ( prevButton . className ) . to . contain ( 'carousel-arrow-default' ) ;
81
92
Simulate . click ( prevButton ) ;
82
93
expect ( dots [ 1 ] . className ) . to . not . contain ( 'selected' ) ;
83
94
expect ( dots [ 0 ] . className ) . to . contain ( 'selected' ) ;
95
+ expect ( onSlideTransitionedStub ) . to . have . been . calledWith ( {
96
+ autoPlay : false ,
97
+ index : 0 ,
98
+ direction : 'left'
99
+ } ) ;
84
100
done ( ) ;
85
101
} ) ;
86
102
} ) ;
87
103
88
104
it ( 'should wrap around from the last to first slide if infinite is true and next is clicked' , done => {
105
+ const onSlideTransitionedStub = sinon . stub ( ) ;
106
+
89
107
renderToJsdom (
90
- < Carousel initialSlide = { 2 } slideWidth = '300px' viewportWidth = '300px' infinite = { true } >
108
+ < Carousel initialSlide = { 2 }
109
+ slideWidth = '300px'
110
+ viewportWidth = '300px'
111
+ infinite = { true }
112
+ onSlideTransitioned = { onSlideTransitionedStub } >
91
113
< div id = 'slide1' />
92
114
< div id = 'slide2' />
93
115
< div id = 'slide3' />
@@ -102,6 +124,11 @@ describe('Carousel', () => {
102
124
Simulate . click ( nextButton ) ;
103
125
expect ( dots [ 2 ] . className ) . to . not . contain ( 'selected' ) ;
104
126
expect ( dots [ 0 ] . className ) . to . contain ( 'selected' ) ;
127
+ expect ( onSlideTransitionedStub ) . to . have . been . calledWith ( {
128
+ autoPlay : false ,
129
+ index : 0 ,
130
+ direction : 'right'
131
+ } ) ;
105
132
done ( ) ;
106
133
} ) ;
107
134
} ) ;
@@ -128,8 +155,13 @@ describe('Carousel', () => {
128
155
} ) ;
129
156
130
157
it ( 'should jump directly to a slide when the dot is clicked' , done => {
158
+ const onSlideTransitionedStub = sinon . stub ( ) ;
159
+
131
160
renderToJsdom (
132
- < Carousel slideWidth = '300px' viewportWidth = '300px' infinite = { false } >
161
+ < Carousel slideWidth = '300px'
162
+ viewportWidth = '300px'
163
+ infinite = { false }
164
+ onSlideTransitioned = { onSlideTransitionedStub } >
133
165
< div id = 'slide1' />
134
166
< div id = 'slide2' />
135
167
< div id = 'slide3' />
@@ -143,6 +175,7 @@ describe('Carousel', () => {
143
175
Simulate . click ( dots [ 2 ] ) ;
144
176
expect ( dots [ 0 ] . className ) . to . not . contain ( 'selected' ) ;
145
177
expect ( dots [ 2 ] . className ) . to . contain ( 'selected' ) ;
178
+ expect ( onSlideTransitionedStub ) . to . have . been . calledOnce ;
146
179
done ( ) ;
147
180
} ) ;
148
181
} ) ;
@@ -451,4 +484,91 @@ describe('Carousel', () => {
451
484
expect ( document . getElementById ( 'slide10' ) ) . to . exist ;
452
485
} ) ;
453
486
} ) ;
487
+
488
+ it ( 'should render custom arrow' , done => {
489
+ const arrows = {
490
+ className : 'test-custom-arrow' ,
491
+ left : < span id = 'custom-left' > Left</ span > ,
492
+ right : < span id = 'custom-right' > Right</ span >
493
+ } ;
494
+
495
+ renderToJsdom (
496
+ < Carousel slideWidth = '300px'
497
+ viewportWidth = '300px'
498
+ infinite = { false }
499
+ arrows = { arrows } >
500
+ < div id = 'slide1' />
501
+ < div id = 'slide2' />
502
+ < div id = 'slide3' />
503
+ </ Carousel >
504
+ ) ;
505
+
506
+ setImmediate ( ( ) => {
507
+ const prevButton = document . querySelector ( '.carousel-left-arrow' ) ;
508
+ const nextButton = document . querySelector ( '.carousel-right-arrow' ) ;
509
+ expect ( prevButton . className ) . to . contain ( 'test-custom-arrow' ) ;
510
+ expect ( nextButton . className ) . to . contain ( 'test-custom-arrow' ) ;
511
+ expect ( document . getElementById ( 'custom-left' ) ) . to . exist ;
512
+ expect ( document . getElementById ( 'custom-right' ) ) . to . exist ;
513
+ done ( ) ;
514
+ } ) ;
515
+ } ) ;
516
+
517
+ it ( 'should render custom arrow without className' , done => {
518
+ const arrows = {
519
+ left : < span id = 'custom-left' > Left</ span > ,
520
+ right : < span id = 'custom-right' > Right</ span >
521
+ } ;
522
+
523
+ renderToJsdom (
524
+ < Carousel slideWidth = '300px'
525
+ viewportWidth = '300px'
526
+ infinite = { false }
527
+ arrows = { arrows } >
528
+ < div id = 'slide1' />
529
+ < div id = 'slide2' />
530
+ < div id = 'slide3' />
531
+ </ Carousel >
532
+ ) ;
533
+
534
+ setImmediate ( ( ) => {
535
+ const prevButton = document . querySelector ( '.carousel-left-arrow' ) ;
536
+ const nextButton = document . querySelector ( '.carousel-right-arrow' ) ;
537
+ expect ( prevButton . className ) . to . not . contain ( 'carousel-arrow-default' ) ;
538
+ expect ( nextButton . className ) . to . not . contain ( 'carousel-arrow-default' ) ;
539
+ expect ( document . getElementById ( 'custom-left' ) ) . to . exist ;
540
+ expect ( document . getElementById ( 'custom-right' ) ) . to . exist ;
541
+ done ( ) ;
542
+ } ) ;
543
+ } ) ;
544
+
545
+ it ( 'should call onSlideTransitioned with autoPlay true' , done => {
546
+ const onSlideTransitionedStub = sinon . stub ( ) ;
547
+ const carousel = renderToJsdom (
548
+ < Carousel slideWidth = '300px'
549
+ viewportWidth = '300px'
550
+ infinite = { false }
551
+ autoplay = { true }
552
+ pauseOnHover = { true }
553
+ onSlideTransitioned = { onSlideTransitionedStub } >
554
+ < div id = 'slide1' />
555
+ < div id = 'slide2' />
556
+ < div id = 'slide3' />
557
+ </ Carousel >
558
+ ) ;
559
+
560
+ setImmediate ( ( ) => {
561
+ const track = document . querySelector ( '.carousel-viewport' ) ;
562
+ const setHoverState = ( bool ) => {
563
+ expect ( bool ) . to . be . true ;
564
+ done ( ) ;
565
+ } ;
566
+ carousel . setHoverState = setHoverState ;
567
+ carousel . handleMovement ( track ) ;
568
+ expect ( onSlideTransitionedStub ) . to . have . been . calledWith ( {
569
+ autoPlay : true ,
570
+ direction : 'right'
571
+ } ) ;
572
+ } ) ;
573
+ } ) ;
454
574
} ) ;
0 commit comments