Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: marks calculation when boolean or number is passed #303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/ReactSlider/ReactSlider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,10 @@ class ReactSlider extends React.Component {
const range = this.props.max - this.props.min + 1;

if (typeof marks === 'boolean') {
marks = Array.from({ length: range }).map((_, key) => key);
marks = Array.from({ length: range }).map((_, key) => key + this.props.min);
} else if (typeof marks === 'number') {
marks = Array.from({ length: range })
.map((_, key) => key)
.map((_, key) => key + this.props.min)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

.filter(key => key % marks === 0);
}

Expand Down
126 changes: 126 additions & 0 deletions src/components/ReactSlider/__tests__/ReactSlider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,130 @@ describe('<ReactSlider>', () => {
expect(mockRenderThumb).toHaveBeenCalledTimes(2);
expect(mockRenderThumb.mock.calls[1][1].value).toBe(mockSecondValue);
});

describe('marks prop', () => {
describe('boolean value', () => {
it('should not render marks if "false" is passed', () => {
const mockRenderMark = jest.fn();
renderer.create(
<ReactSlider
value={0}
min={0}
max={100}
marks={false}
renderMark={mockRenderMark}
/>
);

expect(mockRenderMark).not.toHaveBeenCalled();
});

it('should render marks if "true" is passed', () => {
const mockedMin = 0;
const mockedMax = 10;
const mockRenderMark = jest.fn();
renderer.create(
<ReactSlider
value={0}
min={mockedMin}
max={mockedMax}
marks
renderMark={mockRenderMark}
/>
);

expect(mockRenderMark).toHaveBeenCalledTimes(mockedMax + 1);
mockRenderMark.mock.calls.forEach(([call], index) => {
expect(call.key).toBe(mockedMin + index);
});
});

it('should render marks if "true" is passed and "min" is not 0', () => {
const mockedMin = 10;
const mockedMax = 20;
const mockRenderMark = jest.fn();
renderer.create(
<ReactSlider
value={0}
min={mockedMin}
max={mockedMax}
marks
renderMark={mockRenderMark}
/>
);

expect(mockRenderMark).toHaveBeenCalledTimes(mockedMax - mockedMin + 1);
mockRenderMark.mock.calls.forEach(([call], index) => {
expect(call.key).toBe(mockedMin + index);
});
});
});

describe('number value', () => {
it('should render correct marks', () => {
const mockedMin = 0;
const mockedMax = 10;
const mockedMarks = 2;
const mockRenderMark = jest.fn();
renderer.create(
<ReactSlider
value={0}
min={mockedMin}
max={mockedMax}
marks={mockedMarks}
renderMark={mockRenderMark}
/>
);

expect(mockRenderMark).toHaveBeenCalledTimes(6);
mockRenderMark.mock.calls.forEach(([call], index) => {
expect(call.key).toBe(mockedMin + mockedMarks * index);
});
});

it('should render correct marks if "min" is not 0', () => {
const mockedMin = 10;
const mockedMax = 20;
const mockedMarks = 2;
const mockRenderMark = jest.fn();
renderer.create(
<ReactSlider
value={0}
min={mockedMin}
max={mockedMax}
marks={mockedMarks}
renderMark={mockRenderMark}
/>
);

expect(mockRenderMark).toHaveBeenCalledTimes(6);
mockRenderMark.mock.calls.forEach(([call], index) => {
expect(call.key).toBe(mockedMin + mockedMarks * index);
});
});
});

describe('array of numbers is passed', () => {
it('should render correct marks', () => {
const mockedMin = 0;
const mockedMax = 10;
const mockedMarks = [1, 2, 3, 4, 5];
const mockRenderMark = jest.fn();
renderer.create(
<ReactSlider
value={0}
min={mockedMin}
max={mockedMax}
marks={mockedMarks}
renderMark={mockRenderMark}
/>
);

expect(mockRenderMark).toHaveBeenCalledTimes(mockedMarks.length);
mockRenderMark.mock.calls.forEach(([call], index) => {
expect(call.key).toBe(mockedMarks[index]);
});
});
});
});
});