|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react'; |
| 3 | +import Tour from '../index'; |
| 4 | +import { TourStepProps } from '../types'; |
| 5 | + |
| 6 | +const steps: TourStepProps[] = [ |
| 7 | + { title: 'Step 1', description: 'Description 1' }, |
| 8 | + { title: 'Step 2', description: 'Description 2' }, |
| 9 | + { title: 'Step 3', description: 'Description 3' }, |
| 10 | +]; |
| 11 | + |
| 12 | +describe('<Tour />', () => { |
| 13 | + it('should not render when open is false', () => { |
| 14 | + const { baseElement } = render(<Tour open={false} steps={steps} />); |
| 15 | + expect(baseElement.querySelector('.ty-tour')).not.toBeInTheDocument(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should render when open is true', () => { |
| 19 | + const { getByText } = render(<Tour open steps={steps} />); |
| 20 | + expect(getByText('Step 1')).toBeInTheDocument(); |
| 21 | + expect(getByText('Description 1')).toBeInTheDocument(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should render step title and description', () => { |
| 25 | + const { getByText } = render(<Tour open steps={steps} />); |
| 26 | + expect(getByText('Step 1')).toBeInTheDocument(); |
| 27 | + expect(getByText('Description 1')).toBeInTheDocument(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should navigate to next step', () => { |
| 31 | + const onChange = jest.fn(); |
| 32 | + const { getByText } = render(<Tour open steps={steps} onChange={onChange} />); |
| 33 | + fireEvent.click(getByText('Next')); |
| 34 | + expect(onChange).toHaveBeenCalledWith(1); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should navigate to previous step', () => { |
| 38 | + const onChange = jest.fn(); |
| 39 | + const { getByText } = render(<Tour open steps={steps} current={1} onChange={onChange} />); |
| 40 | + fireEvent.click(getByText('Previous')); |
| 41 | + expect(onChange).toHaveBeenCalledWith(0); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should call onClose when close button is clicked', () => { |
| 45 | + const onClose = jest.fn(); |
| 46 | + const { baseElement } = render(<Tour open steps={steps} onClose={onClose} />); |
| 47 | + const closeBtn = baseElement.querySelector('.ty-tour__close-btn') as HTMLElement; |
| 48 | + fireEvent.click(closeBtn); |
| 49 | + expect(onClose).toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should call onFinish on last step next click', () => { |
| 53 | + const onFinish = jest.fn(); |
| 54 | + const onClose = jest.fn(); |
| 55 | + const { getByText } = render( |
| 56 | + <Tour open steps={steps} current={2} onFinish={onFinish} onClose={onClose} /> |
| 57 | + ); |
| 58 | + fireEvent.click(getByText('Finish')); |
| 59 | + expect(onFinish).toHaveBeenCalled(); |
| 60 | + expect(onClose).toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should render indicators', () => { |
| 64 | + const { baseElement } = render(<Tour open steps={steps} />); |
| 65 | + const indicators = baseElement.querySelectorAll('.ty-tour__indicator'); |
| 66 | + expect(indicators).toHaveLength(3); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should show active indicator for current step', () => { |
| 70 | + const { baseElement } = render(<Tour open steps={steps} current={1} />); |
| 71 | + const indicators = baseElement.querySelectorAll('.ty-tour__indicator'); |
| 72 | + expect(indicators[1]).toHaveClass('ty-tour__indicator_active'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should render mask by default', () => { |
| 76 | + const { baseElement } = render(<Tour open steps={steps} />); |
| 77 | + expect(baseElement.querySelector('.ty-tour__mask')).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should not render mask when mask is false', () => { |
| 81 | + const { baseElement } = render(<Tour open steps={steps} mask={false} />); |
| 82 | + expect(baseElement.querySelector('.ty-tour__mask')).not.toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should render primary type', () => { |
| 86 | + const { baseElement } = render(<Tour open steps={steps} type="primary" />); |
| 87 | + expect(baseElement.querySelector('.ty-tour__panel_primary')).toBeInTheDocument(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should handle keyboard Escape', () => { |
| 91 | + const onClose = jest.fn(); |
| 92 | + render(<Tour open steps={steps} onClose={onClose} />); |
| 93 | + fireEvent.keyDown(document, { key: 'Escape' }); |
| 94 | + expect(onClose).toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should center panel when no target', () => { |
| 98 | + const { baseElement } = render(<Tour open steps={[{ title: 'Centered' }]} />); |
| 99 | + expect( |
| 100 | + baseElement.querySelector('.ty-tour__panel-wrapper_centered') |
| 101 | + ).toBeInTheDocument(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should not render previous button on first step', () => { |
| 105 | + const { queryByText } = render(<Tour open steps={steps} />); |
| 106 | + expect(queryByText('Previous')).not.toBeInTheDocument(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should show Finish text on last step', () => { |
| 110 | + const { getByText } = render(<Tour open steps={steps} current={2} />); |
| 111 | + expect(getByText('Finish')).toBeInTheDocument(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should support custom indicatorsRender', () => { |
| 115 | + const { getByText } = render( |
| 116 | + <Tour |
| 117 | + open |
| 118 | + steps={steps} |
| 119 | + indicatorsRender={(current, total) => <span>{`${current + 1}/${total}`}</span>} |
| 120 | + /> |
| 121 | + ); |
| 122 | + expect(getByText('1/3')).toBeInTheDocument(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments