2022-08-04 17:09:54 +08:00
import React from 'react' ;
import Slider from '..' ;
2022-08-17 15:47:22 +08:00
import type { SliderSingleProps } from '..' ;
2022-08-04 17:09:54 +08:00
import focusTest from '../../../tests/shared/focusTest' ;
import mountTest from '../../../tests/shared/mountTest' ;
import rtlTest from '../../../tests/shared/rtlTest' ;
2022-08-17 15:47:22 +08:00
import { render , fireEvent , act } from '../../../tests/utils' ;
2022-08-04 17:09:54 +08:00
import ConfigProvider from '../../config-provider' ;
import SliderTooltip from '../SliderTooltip' ;
2022-08-17 15:47:22 +08:00
import type { TooltipProps } from '../../tooltip' ;
function tooltipProps ( ) : TooltipProps {
return ( global as any ) . tooltipProps ;
}
jest . mock ( '../../tooltip' , ( ) = > {
const ReactReal = jest . requireActual ( 'react' ) ;
const Tooltip = jest . requireActual ( '../../tooltip' ) ;
const TooltipComponent = Tooltip . default ;
return ReactReal . forwardRef ( ( props : TooltipProps , ref : any ) = > {
( global as any ) . tooltipProps = props ;
return < TooltipComponent { ...props } ref = { ref } / > ;
} ) ;
} ) ;
2022-08-04 17:09:54 +08:00
describe ( 'Slider' , ( ) = > {
mountTest ( Slider ) ;
rtlTest ( Slider ) ;
focusTest ( Slider , { testLib : true } ) ;
2022-08-17 15:47:22 +08:00
beforeEach ( ( ) = > {
jest . useFakeTimers ( ) ;
} ) ;
afterEach ( ( ) = > {
jest . clearAllTimers ( ) ;
jest . useRealTimers ( ) ;
} ) ;
2022-08-04 17:09:54 +08:00
it ( 'should show tooltip when hovering slider handler' , ( ) = > {
const { container } = render ( < Slider defaultValue = { 30 } / > ) ;
fireEvent . mouseEnter ( container . querySelector ( '.ant-slider-handle' ) ! ) ;
expect ( document . querySelector ( '.ant-tooltip' ) ) . toMatchSnapshot ( ) ;
fireEvent . mouseLeave ( container . querySelector ( '.ant-slider-handle' ) ! ) ;
expect ( document . querySelector ( '.ant-tooltip' ) ) . toMatchSnapshot ( ) ;
} ) ;
2022-08-17 15:47:22 +08:00
describe ( 'should show correct placement tooltip when set tooltipPlacement' , ( ) = > {
function test ( name : string , props : Partial < SliderSingleProps > ) {
it ( name , ( ) = > {
const { container } = render ( < Slider vertical defaultValue = { 30 } { ...props } / > ) ;
2022-08-04 17:09:54 +08:00
2022-08-17 15:47:22 +08:00
fireEvent . mouseEnter ( container . querySelector ( '.ant-slider-handle' ) ! ) ;
expect ( tooltipProps ( ) . placement ) . toEqual ( 'left' ) ;
} ) ;
}
2022-08-04 17:09:54 +08:00
2022-08-17 15:47:22 +08:00
test ( 'new' , { tooltip : { placement : 'left' } } ) ;
test ( 'legacy' , { tooltipPlacement : 'left' } ) ;
2022-08-04 17:09:54 +08:00
} ) ;
2022-08-17 15:47:22 +08:00
describe ( 'when tooltip.open is true, tooltip should show always, or should never show' , ( ) = > {
function test (
name : string ,
showProps : Partial < SliderSingleProps > ,
hideProps : Partial < SliderSingleProps > ,
) {
it ( name , ( ) = > {
const { container , rerender } = render ( < Slider defaultValue = { 30 } { ...showProps } / > ) ;
expect (
container . querySelector ( '.ant-tooltip-content' ) ! . className . includes ( 'ant-tooltip-hidden' ) ,
) . toBeFalsy ( ) ;
fireEvent . mouseEnter ( container . querySelector ( '.ant-slider-handle' ) ! ) ;
expect (
container . querySelector ( '.ant-tooltip-content' ) ! . className . includes ( 'ant-tooltip-hidden' ) ,
) . toBeFalsy ( ) ;
fireEvent . click ( container . querySelector ( '.ant-slider-handle' ) ! ) ;
expect (
container . querySelector ( '.ant-tooltip-content' ) ! . className . includes ( 'ant-tooltip-hidden' ) ,
) . toBeFalsy ( ) ;
// Force hide
rerender ( < Slider defaultValue = { 30 } { ...hideProps } / > ) ;
act ( ( ) = > {
jest . runAllTimers ( ) ;
} ) ;
if ( container . querySelector ( '.ant-zoom-down-leave-active' ) ) {
fireEvent . animationEnd ( container . querySelector ( '.ant-zoom-down-leave-active' ) ! ) ;
}
expect ( container . querySelector ( '.ant-tooltip-hidden' ) ! ) . toBeTruthy ( ) ;
} ) ;
}
test ( 'new' , { tooltip : { open : true } } , { tooltip : { open : false } } ) ;
test ( 'legacy' , { tooltipVisible : true } , { tooltipVisible : false } ) ;
2022-08-04 17:09:54 +08:00
} ) ;
it ( 'when step is null, thumb can only be slided to the specific mark' , ( ) = > {
const intentionallyWrongValue = 40 ;
const marks = {
0 : '0' ,
48 : '48' ,
100 : '100' ,
} ;
const { container } = render (
2022-08-17 15:47:22 +08:00
< Slider
marks = { marks }
defaultValue = { intentionallyWrongValue }
step = { null }
tooltip = { { open : true } }
/ > ,
2022-08-04 17:09:54 +08:00
) ;
expect ( container . querySelector ( '.ant-slider-handle' ) ! . getAttribute ( 'aria-valuenow' ) ) . toBe ( '48' ) ;
} ) ;
it ( 'when step is not null, thumb can be slided to the multiples of step' , ( ) = > {
const marks = {
0 : '0' ,
48 : '48' ,
100 : '100' ,
} ;
const { container } = render (
2022-08-17 15:47:22 +08:00
< Slider marks = { marks } defaultValue = { 49 } step = { 1 } tooltip = { { open : true } } / > ,
2022-08-04 17:09:54 +08:00
) ;
expect ( container . querySelector ( '.ant-slider-handle' ) ! . getAttribute ( 'aria-valuenow' ) ) . toBe ( '49' ) ;
} ) ;
it ( 'when step is undefined, thumb can be slided to the multiples of step' , ( ) = > {
const marks = {
0 : '0' ,
48 : '48' ,
100 : '100' ,
} ;
const { container } = render (
2022-08-17 15:47:22 +08:00
< Slider marks = { marks } defaultValue = { 49 } step = { undefined } tooltip = { { open : true } } / > ,
2022-08-04 17:09:54 +08:00
) ;
expect ( container . querySelector ( '.ant-slider-handle' ) ! . getAttribute ( 'aria-valuenow' ) ) . toBe ( '49' ) ;
} ) ;
it ( 'should render in RTL direction' , ( ) = > {
const { container } = render (
< ConfigProvider direction = "rtl" >
2022-08-17 15:47:22 +08:00
< Slider defaultValue = { 30 } tooltip = { { open : true } } / >
2022-08-04 17:09:54 +08:00
< / ConfigProvider > ,
) ;
expect ( container ) . toMatchSnapshot ( ) ;
} ) ;
it ( 'should keepAlign by calling forcePopupAlign' , async ( ) = > {
let ref : any ;
render (
< SliderTooltip
title = "30"
2022-08-26 17:17:13 +08:00
open
2022-08-04 17:09:54 +08:00
ref = { node = > {
ref = node ;
} }
/ > ,
) ;
ref . forcePopupAlign = jest . fn ( ) ;
2022-08-17 15:47:22 +08:00
act ( ( ) = > {
jest . runAllTimers ( ) ;
} ) ;
2022-08-04 17:09:54 +08:00
expect ( ref . forcePopupAlign ) . toHaveBeenCalled ( ) ;
} ) ;
it ( 'tipFormatter should not crash with undefined value' , ( ) = > {
[ undefined , null ] . forEach ( value = > {
2022-08-17 15:47:22 +08:00
render ( < Slider value = { value as any } tooltip = { { open : true } } / > ) ;
2022-08-04 17:09:54 +08:00
} ) ;
} ) ;
it ( 'step should not crash with undefined value' , ( ) = > {
[ undefined , null ] . forEach ( value = > {
2022-08-17 15:47:22 +08:00
render ( < Slider step = { value } tooltip = { { open : true } } / > ) ;
2022-08-04 17:09:54 +08:00
} ) ;
} ) ;
2022-08-17 15:47:22 +08:00
it ( 'deprecated warning' , ( ) = > {
const errSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) = > { } ) ;
const { rerender } = render ( < Slider tooltipPrefixCls = "xxx" / > ) ;
expect ( errSpy ) . toHaveBeenCalledWith (
'Warning: [antd: Slider] `tooltipPrefixCls` is deprecated which will be removed in next major version, please use `tooltip.prefixCls` instead.' ,
) ;
rerender ( < Slider getTooltipPopupContainer = { ( ) = > document . body } / > ) ;
expect ( errSpy ) . toHaveBeenCalledWith (
'Warning: [antd: Slider] `getTooltipPopupContainer` is deprecated which will be removed in next major version, please use `tooltip.getPopupContainer` instead.' ,
) ;
rerender ( < Slider tipFormatter = { v = > v } / > ) ;
expect ( errSpy ) . toHaveBeenCalledWith (
'Warning: [antd: Slider] `tipFormatter` is deprecated which will be removed in next major version, please use `tooltip.formatter` instead.' ,
) ;
rerender ( < Slider tooltipVisible / > ) ;
expect ( errSpy ) . toHaveBeenCalledWith (
'Warning: [antd: Slider] `tooltipVisible` is deprecated which will be removed in next major version, please use `tooltip.open` instead.' ,
) ;
rerender ( < Slider tooltipPlacement = "left" / > ) ;
expect ( errSpy ) . toHaveBeenCalledWith (
'Warning: [antd: Slider] `tooltipPlacement` is deprecated which will be removed in next major version, please use `tooltip.placement` instead.' ,
) ;
errSpy . mockRestore ( ) ;
} ) ;
2022-08-04 17:09:54 +08:00
} ) ;