|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { render, screen } from '@testing-library/react'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | + |
| 6 | +import { WizardFooter } from '../WizardFooter'; |
| 7 | + |
| 8 | +describe('WizardFooter', () => { |
| 9 | + const defaultProps = { |
| 10 | + activeStep: { name: 'Step name', id: 'some-id' }, |
| 11 | + onNext: jest.fn(), |
| 12 | + onBack: jest.fn(), |
| 13 | + onClose: jest.fn() |
| 14 | + }; |
| 15 | + |
| 16 | + it('has button names of "Next", "Back", and "Cancel" by default', () => { |
| 17 | + render(<WizardFooter {...defaultProps} />); |
| 18 | + |
| 19 | + expect(screen.getByRole('button', { name: 'Next' })).toBeVisible(); |
| 20 | + expect(screen.getByRole('button', { name: 'Back' })).toBeVisible(); |
| 21 | + expect(screen.getByRole('button', { name: 'Cancel' })).toBeVisible(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('calls onNext when the next button is clicked', () => { |
| 25 | + const onNext = jest.fn(); |
| 26 | + |
| 27 | + render(<WizardFooter {...defaultProps} onNext={onNext} />); |
| 28 | + |
| 29 | + userEvent.click(screen.getByRole('button', { name: 'Next' })); |
| 30 | + expect(onNext).toHaveBeenCalled(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('calls onBack when the back button is clicked', () => { |
| 34 | + const onBack = jest.fn(); |
| 35 | + |
| 36 | + render(<WizardFooter {...defaultProps} onBack={onBack} />); |
| 37 | + |
| 38 | + userEvent.click(screen.getByRole('button', { name: 'Back' })); |
| 39 | + expect(onBack).toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('calls onClose when the close button is clicked', () => { |
| 43 | + const onClose = jest.fn(); |
| 44 | + |
| 45 | + render(<WizardFooter {...defaultProps} onClose={onClose} />); |
| 46 | + |
| 47 | + userEvent.click(screen.getByRole('button', { name: 'Cancel' })); |
| 48 | + expect(onClose).toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('can have custom button names', () => { |
| 52 | + render( |
| 53 | + <WizardFooter |
| 54 | + {...defaultProps} |
| 55 | + nextButtonText={<>Go!</>} |
| 56 | + backButtonText="Turn around!" |
| 57 | + cancelButtonText={<span>Get out!</span>} |
| 58 | + /> |
| 59 | + ); |
| 60 | + |
| 61 | + expect(screen.getByRole('button', { name: 'Go!' })).toBeVisible(); |
| 62 | + expect(screen.getByRole('button', { name: 'Turn around!' })).toBeVisible(); |
| 63 | + expect(screen.getByRole('button', { name: 'Get out!' })).toBeVisible(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('has disabled back button when disableBackButton is enabled', () => { |
| 67 | + render(<WizardFooter {...defaultProps} disableBackButton />); |
| 68 | + expect(screen.getByRole('button', { name: 'Back' })).toHaveAttribute('disabled'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('has no back button when activeStep has hideBackButton enabled', () => { |
| 72 | + render(<WizardFooter {...defaultProps} activeStep={{ ...defaultProps.activeStep, hideBackButton: true }} />); |
| 73 | + expect(screen.queryByRole('button', { name: 'Back' })).toBeNull(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('has no cancel button when activeStep has hideCancelButton enabled', () => { |
| 77 | + render(<WizardFooter {...defaultProps} activeStep={{ ...defaultProps.activeStep, hideCancelButton: true }} />); |
| 78 | + expect(screen.queryByRole('button', { name: 'Cancel' })).toBeNull(); |
| 79 | + }); |
| 80 | + |
| 81 | + it(`uses activeStep's nextButtonText when specified instead of nextButtonText of WizardFooter`, () => { |
| 82 | + render( |
| 83 | + <WizardFooter |
| 84 | + {...defaultProps} |
| 85 | + nextButtonText="Footer next" |
| 86 | + activeStep={{ ...defaultProps.activeStep, nextButtonText: 'Active step next' }} |
| 87 | + /> |
| 88 | + ); |
| 89 | + expect(screen.queryByRole('button', { name: 'Footer next' })).toBeNull(); |
| 90 | + expect(screen.getByRole('button', { name: 'Active step next' })).toBeVisible(); |
| 91 | + }); |
| 92 | +}); |
0 commit comments