1+ import { render , screen } from "@testing-library/react" ;
2+ import userEvent from "@testing-library/user-event" ;
3+ import { SlideNavigation , type SlideNavigationProps } from "../SlideNavigation" ;
4+
5+ describe ( "SlideNavigation" , ( ) => {
6+ const defaultProps : SlideNavigationProps = {
7+ currentIndex : 0 ,
8+ maxIndex : 2 ,
9+ onPrev : jest . fn ( ) ,
10+ onNext : jest . fn ( ) ,
11+ onGoToIndex : jest . fn ( ) ,
12+ } ;
13+
14+ beforeEach ( ( ) => {
15+ jest . clearAllMocks ( ) ;
16+ } ) ;
17+
18+ it ( "renders desktop navigation by default" , ( ) => {
19+ render ( < SlideNavigation { ...defaultProps } /> ) ;
20+
21+ expect ( screen . getByTestId ( "prev-slide-button" ) ) . toBeInTheDocument ( ) ;
22+ expect ( screen . getByTestId ( "next-slide-button" ) ) . toBeInTheDocument ( ) ;
23+ expect ( screen . getByTestId ( "slide-indicators" ) ) . toBeInTheDocument ( ) ;
24+ } ) ;
25+
26+ it ( "renders mobile navigation when variant is mobile" , ( ) => {
27+ render ( < SlideNavigation { ...defaultProps } variant = "mobile" /> ) ;
28+
29+ expect ( screen . getByTestId ( "mobile-prev-slide-button" ) ) . toBeInTheDocument ( ) ;
30+ expect ( screen . getByTestId ( "mobile-next-slide-button" ) ) . toBeInTheDocument ( ) ;
31+ expect ( screen . getByTestId ( "mobile-slide-indicators" ) ) . toBeInTheDocument ( ) ;
32+ } ) ;
33+
34+ it ( "calls onPrev when prev button is clicked" , async ( ) => {
35+ const user = userEvent . setup ( ) ;
36+ render ( < SlideNavigation { ...defaultProps } currentIndex = { 1 } /> ) ;
37+
38+ const prevButton = screen . getByTestId ( "prev-slide-button" ) ;
39+ await user . click ( prevButton ) ;
40+
41+ expect ( defaultProps . onPrev ) . toHaveBeenCalledTimes ( 1 ) ;
42+ } ) ;
43+
44+ it ( "calls onNext when next button is clicked" , async ( ) => {
45+ const user = userEvent . setup ( ) ;
46+ render ( < SlideNavigation { ...defaultProps } /> ) ;
47+
48+ const nextButton = screen . getByTestId ( "next-slide-button" ) ;
49+ await user . click ( nextButton ) ;
50+
51+ expect ( defaultProps . onNext ) . toHaveBeenCalledTimes ( 1 ) ;
52+ } ) ;
53+
54+ it ( "calls onGoToIndex when indicator is clicked" , async ( ) => {
55+ const user = userEvent . setup ( ) ;
56+ render ( < SlideNavigation { ...defaultProps } /> ) ;
57+
58+ const secondIndicator = screen . getByTestId ( "slide-indicator-1" ) ;
59+ await user . click ( secondIndicator ) ;
60+
61+ expect ( defaultProps . onGoToIndex ) . toHaveBeenCalledWith ( 1 ) ;
62+ } ) ;
63+
64+ it ( "disables prev button when currentIndex is 0" , ( ) => {
65+ render ( < SlideNavigation { ...defaultProps } currentIndex = { 0 } /> ) ;
66+
67+ const prevButton = screen . getByTestId ( "prev-slide-button" ) ;
68+ expect ( prevButton ) . toBeDisabled ( ) ;
69+ } ) ;
70+
71+ it ( "disables next button when currentIndex equals maxIndex" , ( ) => {
72+ render ( < SlideNavigation { ...defaultProps } currentIndex = { 2 } maxIndex = { 2 } /> ) ;
73+
74+ const nextButton = screen . getByTestId ( "next-slide-button" ) ;
75+ expect ( nextButton ) . toBeDisabled ( ) ;
76+ } ) ;
77+
78+ it ( "enables prev button when currentIndex is greater than 0" , ( ) => {
79+ render ( < SlideNavigation { ...defaultProps } currentIndex = { 1 } /> ) ;
80+
81+ const prevButton = screen . getByTestId ( "prev-slide-button" ) ;
82+ expect ( prevButton ) . not . toBeDisabled ( ) ;
83+ } ) ;
84+
85+ it ( "enables next button when currentIndex is less than maxIndex" , ( ) => {
86+ render ( < SlideNavigation { ...defaultProps } currentIndex = { 1 } maxIndex = { 2 } /> ) ;
87+
88+ const nextButton = screen . getByTestId ( "next-slide-button" ) ;
89+ expect ( nextButton ) . not . toBeDisabled ( ) ;
90+ } ) ;
91+
92+ it ( "renders correct number of indicators" , ( ) => {
93+ render ( < SlideNavigation { ...defaultProps } maxIndex = { 3 } /> ) ;
94+
95+ expect ( screen . getByTestId ( "slide-indicator-0" ) ) . toBeInTheDocument ( ) ;
96+ expect ( screen . getByTestId ( "slide-indicator-1" ) ) . toBeInTheDocument ( ) ;
97+ expect ( screen . getByTestId ( "slide-indicator-2" ) ) . toBeInTheDocument ( ) ;
98+ expect ( screen . getByTestId ( "slide-indicator-3" ) ) . toBeInTheDocument ( ) ;
99+ } ) ;
100+
101+ it ( "works with mobile variant" , async ( ) => {
102+ const user = userEvent . setup ( ) ;
103+ render ( < SlideNavigation { ...defaultProps } currentIndex = { 1 } variant = "mobile" /> ) ;
104+
105+ const mobilePrevButton = screen . getByTestId ( "mobile-prev-slide-button" ) ;
106+ const mobileNextButton = screen . getByTestId ( "mobile-next-slide-button" ) ;
107+
108+ await user . click ( mobilePrevButton ) ;
109+ expect ( defaultProps . onPrev ) . toHaveBeenCalledTimes ( 1 ) ;
110+
111+ await user . click ( mobileNextButton ) ;
112+ expect ( defaultProps . onNext ) . toHaveBeenCalledTimes ( 1 ) ;
113+ } ) ;
114+
115+ it ( "applies custom className" , ( ) => {
116+ render ( < SlideNavigation { ...defaultProps } className = "custom-class" /> ) ;
117+
118+ const container = screen . getByTestId ( "prev-slide-button" ) . parentElement ;
119+ expect ( container ) . toHaveClass ( "custom-class" ) ;
120+ } ) ;
121+ } ) ;
0 commit comments