|
| 1 | +import { fireEvent, render } from '@testing-library/react' |
| 2 | + |
| 3 | +import { |
| 4 | + Stepper, |
| 5 | + StepperContainer, |
| 6 | + StepperDecreaseButton, |
| 7 | + StepperIncreaseButton, |
| 8 | + StepperInput, |
| 9 | +} from '..' |
| 10 | + |
| 11 | +describe('Stepper', () => { |
| 12 | + it('should render', () => { |
| 13 | + const { container } = render( |
| 14 | + <Stepper> |
| 15 | + <StepperContainer> |
| 16 | + <StepperDecreaseButton /> |
| 17 | + <StepperInput editable={false} /> |
| 18 | + <StepperIncreaseButton /> |
| 19 | + </StepperContainer> |
| 20 | + </Stepper>, |
| 21 | + ) |
| 22 | + expect(container).toMatchSnapshot() |
| 23 | + }) |
| 24 | + |
| 25 | + it('should throw error if children are used outside of StepperProvider', () => { |
| 26 | + expect(() => { |
| 27 | + render(<StepperInput editable={false} />) |
| 28 | + }).toThrow('useStepper must be used within a StepperProvider') |
| 29 | + }) |
| 30 | + |
| 31 | + it('should call onValueChange when value is changed', () => { |
| 32 | + const onValueChange = vi.fn() |
| 33 | + const { container } = render( |
| 34 | + <Stepper onValueChange={onValueChange}> |
| 35 | + <StepperInput /> |
| 36 | + </Stepper>, |
| 37 | + ) |
| 38 | + const input = container.querySelector('[aria-label="Stepper value"]') |
| 39 | + fireEvent.change(input!, { target: { value: '10' } }) |
| 40 | + expect(onValueChange).toHaveBeenCalledWith(10) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should change inner value when onValueChange is not provided', () => { |
| 44 | + const { container } = render( |
| 45 | + <Stepper> |
| 46 | + <StepperInput /> |
| 47 | + </Stepper>, |
| 48 | + ) |
| 49 | + const input = container.querySelector('[aria-label="Stepper value"]') |
| 50 | + fireEvent.change(input!, { target: { value: '10' } }) |
| 51 | + expect(input).toHaveAttribute('data-value', '10') |
| 52 | + }) |
| 53 | + |
| 54 | + it('should have disabled decrease button when value is at min', () => { |
| 55 | + const { container } = render( |
| 56 | + <Stepper> |
| 57 | + <StepperDecreaseButton /> |
| 58 | + <StepperInput /> |
| 59 | + <StepperIncreaseButton /> |
| 60 | + </Stepper>, |
| 61 | + ) |
| 62 | + const decreaseButton = container.querySelector( |
| 63 | + '[aria-label="Decrease button"] svg', |
| 64 | + ) |
| 65 | + fireEvent.change(container.querySelector('[aria-label="Stepper value"]')!, { |
| 66 | + target: { value: '0' }, |
| 67 | + }) |
| 68 | + expect(decreaseButton).toHaveClass( |
| 69 | + 'color-0-var(--base10,light-dark(#0000001A,#FFFFFF1A))--255', |
| 70 | + ) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should have disabled increase button when value is at max', () => { |
| 74 | + const { container } = render( |
| 75 | + <Stepper> |
| 76 | + <StepperDecreaseButton /> |
| 77 | + <StepperInput /> |
| 78 | + <StepperIncreaseButton /> |
| 79 | + </Stepper>, |
| 80 | + ) |
| 81 | + const increaseButton = container.querySelector( |
| 82 | + '[aria-label="Increase button"] svg', |
| 83 | + ) |
| 84 | + fireEvent.change(container.querySelector('[aria-label="Stepper value"]')!, { |
| 85 | + target: { value: '100' }, |
| 86 | + }) |
| 87 | + expect(increaseButton).toHaveClass( |
| 88 | + 'color-0-var(--base10,light-dark(#0000001A,#FFFFFF1A))--255', |
| 89 | + ) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should export components', async () => { |
| 93 | + const index = await import('../index') |
| 94 | + expect({ ...index }).toEqual({ |
| 95 | + Stepper: expect.any(Function), |
| 96 | + StepperContainer: expect.any(Function), |
| 97 | + StepperDecreaseButton: expect.any(Function), |
| 98 | + StepperIncreaseButton: expect.any(Function), |
| 99 | + StepperInput: expect.any(Function), |
| 100 | + useStepper: expect.any(Function), |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should increase value when increase button is clicked', () => { |
| 105 | + const { container } = render( |
| 106 | + <Stepper> |
| 107 | + <StepperDecreaseButton /> |
| 108 | + <StepperInput /> |
| 109 | + <StepperIncreaseButton /> |
| 110 | + </Stepper>, |
| 111 | + ) |
| 112 | + const increaseButton = container.querySelector( |
| 113 | + '[aria-label="Increase button"]', |
| 114 | + ) |
| 115 | + fireEvent.click(increaseButton!) |
| 116 | + expect( |
| 117 | + container.querySelector('[aria-label="Stepper value"]'), |
| 118 | + ).toHaveAttribute('data-value', '1') |
| 119 | + }) |
| 120 | + |
| 121 | + it('should decrease value when decrease button is clicked', () => { |
| 122 | + const { container } = render( |
| 123 | + <Stepper defaultValue={1}> |
| 124 | + <StepperDecreaseButton /> |
| 125 | + <StepperInput /> |
| 126 | + <StepperIncreaseButton /> |
| 127 | + </Stepper>, |
| 128 | + ) |
| 129 | + const decreaseButton = container.querySelector( |
| 130 | + '[aria-label="Decrease button"]', |
| 131 | + ) |
| 132 | + fireEvent.click(decreaseButton!) |
| 133 | + expect( |
| 134 | + container.querySelector('[aria-label="Stepper value"]'), |
| 135 | + ).toHaveAttribute('data-value', '0') |
| 136 | + }) |
| 137 | +}) |
0 commit comments