|
| 1 | +import React from 'react'; |
| 2 | +import {fireEvent, render} from '@testing-library/react-native'; |
| 3 | +import {Text} from 'react-native'; |
| 4 | + |
| 5 | +import Slider, {type MarkerProps} from '../src/Slider'; |
| 6 | + |
| 7 | +describe('Slider', () => { |
| 8 | + it('maps public value props to native prop names', () => { |
| 9 | + const {getByTestId} = render( |
| 10 | + <Slider |
| 11 | + testID="slider" |
| 12 | + minimumValue={2} |
| 13 | + maximumValue={10} |
| 14 | + lowerLimit={3} |
| 15 | + upperLimit={9} |
| 16 | + value={4} |
| 17 | + step={2} |
| 18 | + disabled |
| 19 | + inverted |
| 20 | + tapToSeek |
| 21 | + thumbTintColor="red" |
| 22 | + />, |
| 23 | + ); |
| 24 | + |
| 25 | + expect(getByTestId('slider')).toHaveProp('minValue', 2); |
| 26 | + expect(getByTestId('slider')).toHaveProp('maxValue', 10); |
| 27 | + expect(getByTestId('slider')).toHaveProp('lowerLimit', 3); |
| 28 | + expect(getByTestId('slider')).toHaveProp('upperLimit', 9); |
| 29 | + expect(getByTestId('slider')).toHaveProp('value', 4); |
| 30 | + expect(getByTestId('slider')).toHaveProp('step', 2); |
| 31 | + expect(getByTestId('slider')).toHaveProp('disabled', true); |
| 32 | + expect(getByTestId('slider')).toHaveProp('inverted', true); |
| 33 | + expect(getByTestId('slider')).toHaveProp('tapToSeek', true); |
| 34 | + expect(getByTestId('slider')).toHaveProp('thumbTintColor', 'red'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('keeps minValue and maxValue as compatibility aliases', () => { |
| 38 | + const {getByTestId} = render( |
| 39 | + <Slider testID="slider" minValue={5} maxValue={15} />, |
| 40 | + ); |
| 41 | + |
| 42 | + expect(getByTestId('slider')).toHaveProp('minValue', 5); |
| 43 | + expect(getByTestId('slider')).toHaveProp('maxValue', 15); |
| 44 | + }); |
| 45 | + |
| 46 | + it('forwards native value events with numeric values', () => { |
| 47 | + const onValueChange = jest.fn(); |
| 48 | + const onSlidingStart = jest.fn(); |
| 49 | + const onSlidingComplete = jest.fn(); |
| 50 | + const {getByTestId} = render( |
| 51 | + <Slider |
| 52 | + testID="slider" |
| 53 | + onValueChange={onValueChange} |
| 54 | + onSlidingStart={onSlidingStart} |
| 55 | + onSlidingComplete={onSlidingComplete} |
| 56 | + />, |
| 57 | + ); |
| 58 | + |
| 59 | + fireEvent(getByTestId('slider'), 'onValueChange', { |
| 60 | + nativeEvent: {value: 0.25}, |
| 61 | + }); |
| 62 | + fireEvent(getByTestId('slider'), 'onSlidingStart', { |
| 63 | + nativeEvent: {value: 0.5}, |
| 64 | + }); |
| 65 | + fireEvent(getByTestId('slider'), 'onSlidingComplete', { |
| 66 | + nativeEvent: {value: 0.75}, |
| 67 | + }); |
| 68 | + |
| 69 | + expect(onValueChange).toHaveBeenCalledWith(0.25); |
| 70 | + expect(onSlidingStart).toHaveBeenCalledWith(0.5); |
| 71 | + expect(onSlidingComplete).toHaveBeenCalledWith(0.75); |
| 72 | + }); |
| 73 | + |
| 74 | + it('passes accessibility actions through unchanged', () => { |
| 75 | + const onAccessibilityAction = jest.fn(); |
| 76 | + const event = {nativeEvent: {actionName: 'increment'}}; |
| 77 | + const {getByTestId} = render( |
| 78 | + <Slider |
| 79 | + testID="slider" |
| 80 | + onAccessibilityAction={onAccessibilityAction} |
| 81 | + />, |
| 82 | + ); |
| 83 | + |
| 84 | + fireEvent(getByTestId('slider'), 'onAccessibilityAction', event); |
| 85 | + |
| 86 | + expect(onAccessibilityAction).toHaveBeenCalledWith(event); |
| 87 | + }); |
| 88 | + |
| 89 | + it('warns when lowerLimit is greater than upperLimit', () => { |
| 90 | + const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 91 | + |
| 92 | + render(<Slider lowerLimit={10} upperLimit={2} />); |
| 93 | + |
| 94 | + expect(warn).toHaveBeenCalledWith( |
| 95 | + 'Invalid configuration: lower limit is supposed to be smaller than upper limit', |
| 96 | + ); |
| 97 | + |
| 98 | + warn.mockRestore(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('renders step numbers and custom markers', () => { |
| 102 | + const StepMarker = jest.fn(({index, stepMarked}: MarkerProps) => ( |
| 103 | + <Text testID={`marker-${index}`}>{stepMarked ? 'selected' : 'idle'}</Text> |
| 104 | + )); |
| 105 | + |
| 106 | + const {getByTestId} = render( |
| 107 | + <Slider |
| 108 | + testID="slider" |
| 109 | + minimumValue={0} |
| 110 | + maximumValue={2} |
| 111 | + value={1} |
| 112 | + step={1} |
| 113 | + renderStepNumber |
| 114 | + StepMarker={StepMarker} |
| 115 | + />, |
| 116 | + ); |
| 117 | + |
| 118 | + expect(getByTestId('StepsIndicator-Container')).toBeTruthy(); |
| 119 | + expect(getByTestId('0th-step')).toHaveTextContent('0'); |
| 120 | + expect(getByTestId('1th-step')).toHaveTextContent('1'); |
| 121 | + expect(getByTestId('2th-step')).toHaveTextContent('2'); |
| 122 | + expect(getByTestId('marker-1')).toHaveTextContent('selected'); |
| 123 | + expect(getByTestId('slider')).toHaveProp('thumbTintColor', 'transparent'); |
| 124 | + expect(StepMarker).toHaveBeenCalledWith( |
| 125 | + expect.objectContaining({ |
| 126 | + currentValue: 1, |
| 127 | + index: 1, |
| 128 | + max: 2, |
| 129 | + min: 0, |
| 130 | + stepMarked: true, |
| 131 | + }), |
| 132 | + undefined, |
| 133 | + ); |
| 134 | + }); |
| 135 | +}); |
0 commit comments