Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan committed Dec 6, 2023
1 parent bd95912 commit f782d44
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
9 changes: 3 additions & 6 deletions src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
},
blur: () => {
const { activeElement } = document;
if (containerRef.current.contains(activeElement)) {
if (containerRef.current?.contains(activeElement)) {
(activeElement as HTMLElement)?.blur();
}
},
Expand Down Expand Up @@ -513,18 +513,15 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
>
<div
className={cls(`${prefixCls}-rail`, classNames?.rail)}
style={{
...railStyle,
...styles?.rail,
}}
style={{ ...railStyle, ...styles?.rail }}
/>

<Tracks
prefixCls={prefixCls}
style={trackStyle}
values={sortedCacheValues}
startPoint={startPoint}
onStartMove={mergedDraggableTrack ? onStartMove : null}
onStartMove={mergedDraggableTrack ? onStartMove : undefined}
/>

<Steps
Expand Down
15 changes: 11 additions & 4 deletions tests/Slider.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import classNames from 'classnames';
import { render, fireEvent, createEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { createEvent, fireEvent, render } from '@testing-library/react';
import classNames from 'classnames';
import keyCode from 'rc-util/lib/KeyCode';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React from 'react';
import Slider from '../src/Slider';

describe('Slider', () => {
Expand Down Expand Up @@ -189,7 +189,9 @@ describe('Slider', () => {
it('decreases the value when key "left" is pressed', () => {
const onChange = jest.fn();
const onChangeComplete = jest.fn();
const { container } = render(<Slider defaultValue={50} onChange={onChange} onChangeComplete={onChangeComplete} />);
const { container } = render(
<Slider defaultValue={50} onChange={onChange} onChangeComplete={onChangeComplete} />,
);

fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[0], {
keyCode: keyCode.LEFT,
Expand Down Expand Up @@ -647,4 +649,9 @@ describe('Slider', () => {
fireEvent(document, mouseMove);
expect(onChange).toHaveBeenLastCalledWith(100);
});

it('should render Slider with included=false', () => {
const { asFragment } = render(<Slider included={false} />);
expect(asFragment().firstChild).toMatchSnapshot();
});
});
24 changes: 24 additions & 0 deletions tests/__snapshots__/Slider.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ exports[`Slider should render Slider with correct DOM structure 1`] = `
</div>
`;

exports[`Slider should render Slider with included=false 1`] = `
<div
class="rc-slider rc-slider-horizontal"
>
<div
class="rc-slider-rail"
/>
<div
class="rc-slider-step"
/>
<div
aria-disabled="false"
aria-orientation="horizontal"
aria-valuemax="100"
aria-valuemin="0"
aria-valuenow="0"
class="rc-slider-handle"
role="slider"
style="left: 0%; transform: translateX(-50%);"
tabindex="0"
/>
</div>
`;

exports[`Slider should render Slider without handle if value is null 1`] = `
<div
class="rc-slider rc-slider-horizontal"
Expand Down
10 changes: 5 additions & 5 deletions tests/common.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable max-len, no-undef */
import React from 'react';
import { render, fireEvent, createEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { createEvent, fireEvent, render } from '@testing-library/react';
import KeyCode from 'rc-util/lib/KeyCode';
import Slider, { Range, createSliderWithTooltip } from '../src';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React from 'react';
import Slider from '../src';

// const setWidth = (object, width) => {
// // https://github.com/tmpvar/jsdom/commit/0cdb2efcc69b6672dc2928644fc0172df5521176
Expand Down Expand Up @@ -366,7 +366,7 @@ describe('Common', () => {
'Warning: [rc-slider] `onAfterChange` is deprecated. Please use `onChangeComplete` instead.',
);
errSpy.mockRestore();
})
});

// Move to antd instead
// it('the tooltip should be attach to the container with the id tooltip', () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/marks.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable max-len, no-undef */
import React from 'react';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { fireEvent, render } from '@testing-library/react';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React from 'react';
import Slider from '../src';

describe('marks', () => {
Expand Down

0 comments on commit f782d44

Please sign in to comment.