@@ -36,15 +36,16 @@ export const FeaturedItems = ({
3636 const hasFeaturedItems = collectionFeaturedItems . length > 0
3737
3838 useEffect ( ( ) => {
39- if ( ! sliderRef . current ) return
40-
41- // checkSliderPositionToDisableButtons()
42-
43- setIsOverflowing ( sliderRef . current . scrollWidth > sliderRef . current . clientWidth )
39+ if ( sliderRef . current ) {
40+ setIsOverflowing ( sliderRef . current . scrollWidth > sliderRef . current . clientWidth )
41+ }
4442
4543 const handleResize = ( ) => {
46- if ( ! sliderRef . current ) return
47- setIsOverflowing ( sliderRef . current . scrollWidth > sliderRef . current ?. clientWidth )
44+ if ( sliderRef . current ) {
45+ /* istanbul ignore next */ setIsOverflowing (
46+ sliderRef . current . scrollWidth > sliderRef . current ?. clientWidth
47+ )
48+ }
4849 }
4950
5051 window . addEventListener ( 'resize' , handleResize )
@@ -58,6 +59,7 @@ export const FeaturedItems = ({
5859 return (
5960 < SkeletonTheme >
6061 < div
62+ data-testid = "featured-items-skeleton"
6163 style = { {
6264 display : 'flex' ,
6365 justifyContent : 'center' ,
@@ -86,48 +88,48 @@ export const FeaturedItems = ({
8688 const oneItemOnly = collectionFeaturedItems . length === 1
8789
8890 const handleNext = ( ) => {
89- if ( ! sliderRef . current ) return
91+ if ( sliderRef . current ) {
92+ const { width } = sliderRef . current . getBoundingClientRect ( )
9093
91- const { width } = sliderRef . current . getBoundingClientRect ( )
92-
93- sliderRef . current . scrollBy ( {
94- left : width ,
95- behavior : 'smooth'
96- } )
94+ sliderRef . current . scrollBy ( {
95+ left : width ,
96+ behavior : 'smooth'
97+ } )
98+ }
9799 }
98100
99101 const handlePrev = ( ) => {
100- if ( ! sliderRef . current ) return
101-
102- const { width } = sliderRef . current . getBoundingClientRect ( )
102+ if ( sliderRef . current ) {
103+ const { width } = sliderRef . current . getBoundingClientRect ( )
103104
104- sliderRef . current . scrollBy ( {
105- left : - width ,
106- behavior : 'smooth'
107- } )
105+ sliderRef . current . scrollBy ( {
106+ left : - width ,
107+ behavior : 'smooth'
108+ } )
109+ }
108110 }
109111
110112 const checkSliderPositionToDisableButtons = ( ) => {
111- if ( ! sliderRef . current ) return
112-
113- const { scrollLeft , scrollWidth , clientWidth } = sliderRef . current
114-
115- if ( scrollLeft < 50 ) {
116- setBackBtnDisabled ( true )
117- } else {
118- setBackBtnDisabled ( false )
119- }
120-
121- // 50 is the threshold to know when the scroll is at the end
122- if ( scrollLeft + clientWidth > scrollWidth - 50 ) {
123- setNextBtnDisabled ( true )
124- } else {
125- setNextBtnDisabled ( false )
113+ if ( sliderRef . current ) {
114+ const { scrollLeft , scrollWidth , clientWidth } = sliderRef . current
115+
116+ if ( scrollLeft < 50 ) {
117+ setBackBtnDisabled ( true )
118+ } else {
119+ setBackBtnDisabled ( false )
120+ }
121+
122+ // 50 is the threshold to know when the scroll is at the end
123+ if ( scrollLeft + clientWidth > scrollWidth - 50 ) {
124+ setNextBtnDisabled ( true )
125+ } else {
126+ setNextBtnDisabled ( false )
127+ }
126128 }
127129 }
128130
129131 return (
130- < div className = { `${ styles . featured_items } ${ className || '' } ` } >
132+ < div className = { `${ styles . featured_items } ${ className || '' } ` } data-testid = "featured-items" >
131133 < h4 > { t ( 'featuredItems.title' ) } </ h4 >
132134
133135 < div className = { styles [ 'slider-container' ] } data-testid = "featured-items-slider" >
@@ -138,6 +140,7 @@ export const FeaturedItems = ({
138140 variant = "secondary"
139141 aria-label = { t ( 'featuredItems.slider.prevLabel' ) }
140142 onClick = { handlePrev }
143+ data-testid = "featured-items-slider-prev-btn"
141144 disabled = { backBtnDisabled } >
142145 < ChevronLeft size = { 30 } color = { theme . color . primary } />
143146 </ Button >
@@ -172,6 +175,7 @@ export const FeaturedItems = ({
172175 variant = "secondary"
173176 aria-label = { t ( 'featuredItems.slider.nextLabel' ) }
174177 onClick = { handleNext }
178+ data-testid = "featured-items-slider-next-btn"
175179 disabled = { nextBtnDisabled } >
176180 < ChevronRight size = { 30 } color = { theme . color . primary } />
177181 </ Button >
0 commit comments