|
| 1 | +import React from 'react' |
| 2 | +import { render } from '@testing-library/react' |
| 3 | +import { fireEvent } from '@testing-library/dom' |
| 4 | +import SliderRightPanel from './index' |
| 5 | +import '@testing-library/jest-dom' |
| 6 | + |
| 7 | +class ResizeObserverMock { |
| 8 | + observe() {} |
| 9 | + unobserve() {} |
| 10 | + disconnect() {} |
| 11 | +} |
| 12 | + |
| 13 | +global.ResizeObserver = ResizeObserverMock |
| 14 | + |
| 15 | +jest.mock('@janhq/joi', () => ({ |
| 16 | + ...jest.requireActual('@janhq/joi'), |
| 17 | + Slider: ({ children, onValueChange, ...props }: any) => ( |
| 18 | + <div data-testid="slider-root" {...props}> |
| 19 | + <input |
| 20 | + data-testid="slider-input" |
| 21 | + type="number" |
| 22 | + {...props} |
| 23 | + onChange={(e: any) => |
| 24 | + onValueChange && onValueChange([parseInt(e.target.value)]) |
| 25 | + } |
| 26 | + /> |
| 27 | + {children} |
| 28 | + </div> |
| 29 | + ), |
| 30 | +})) |
| 31 | + |
| 32 | +describe('SliderRightPanel', () => { |
| 33 | + const defaultProps = { |
| 34 | + title: 'Test Slider', |
| 35 | + disabled: false, |
| 36 | + min: 0, |
| 37 | + max: 100, |
| 38 | + step: 1, |
| 39 | + description: 'This is a test slider', |
| 40 | + value: 50, |
| 41 | + onValueChanged: jest.fn(), |
| 42 | + } |
| 43 | + |
| 44 | + it('renders correctly with given props', () => { |
| 45 | + const { getByText } = render(<SliderRightPanel {...defaultProps} />) |
| 46 | + expect(getByText('Test Slider')).toBeInTheDocument() |
| 47 | + }) |
| 48 | + |
| 49 | + it('calls onValueChanged with correct value when input is changed', () => { |
| 50 | + defaultProps.onValueChanged = jest.fn() |
| 51 | + const { getByRole } = render(<SliderRightPanel {...defaultProps} />) |
| 52 | + |
| 53 | + const input = getByRole('textbox') |
| 54 | + fireEvent.change(input, { target: { value: '75' } }) |
| 55 | + expect(defaultProps.onValueChanged).toHaveBeenCalledWith(75) |
| 56 | + }) |
| 57 | + |
| 58 | + it('calls onValueChanged with correct value when slider is changed', () => { |
| 59 | + defaultProps.onValueChanged = jest.fn() |
| 60 | + const { getByTestId } = render(<SliderRightPanel {...defaultProps} />) |
| 61 | + |
| 62 | + const input = getByTestId('slider-input') |
| 63 | + fireEvent.change(input, { target: { value: '75' } }) |
| 64 | + expect(defaultProps.onValueChanged).toHaveBeenCalledWith(75) |
| 65 | + }) |
| 66 | + |
| 67 | + it('calls onValueChanged with max value when input exceeds max', () => { |
| 68 | + defaultProps.onValueChanged = jest.fn() |
| 69 | + const { getByRole } = render(<SliderRightPanel {...defaultProps} />) |
| 70 | + const input = getByRole('textbox') |
| 71 | + fireEvent.change(input, { target: { value: '150' } }) |
| 72 | + fireEvent.focusOut(input) |
| 73 | + expect(defaultProps.onValueChanged).toHaveBeenCalledWith(100) |
| 74 | + }) |
| 75 | + |
| 76 | + it('calls onValueChanged with min value when input is below min', () => { |
| 77 | + defaultProps.onValueChanged = jest.fn() |
| 78 | + const { getByRole } = render(<SliderRightPanel {...defaultProps} />) |
| 79 | + const input = getByRole('textbox') |
| 80 | + fireEvent.change(input, { target: { value: '0' } }) |
| 81 | + fireEvent.focusOut(input) |
| 82 | + expect(defaultProps.onValueChanged).toHaveBeenCalledWith(0) |
| 83 | + }) |
| 84 | + |
| 85 | + it('does not call onValueChanged when input is invalid', () => { |
| 86 | + defaultProps.onValueChanged = jest.fn() |
| 87 | + const { getByRole } = render(<SliderRightPanel {...defaultProps} />) |
| 88 | + const input = getByRole('textbox') |
| 89 | + fireEvent.change(input, { target: { value: 'invalid' } }) |
| 90 | + expect(defaultProps.onValueChanged).not.toHaveBeenCalledWith(0) |
| 91 | + }) |
| 92 | +}) |
0 commit comments