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

[RFR] Allow to display chart from end to start dates #231

Merged
merged 1 commit into from
Mar 20, 2018
Merged
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
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import { addMetaballsDefs } from './metaballs';

import './style.css';

const withinRange = (date, dateBounds) =>
export const withinRange = (date, dateBounds) => {
const startingDate = Math.min(...dateBounds);
const endingDate = Math.max(...dateBounds);

// @TODO: remove the `new Date()` constructor in the next major version: we need to force it at configuration level.
new Date(date) >= dateBounds[0] && new Date(date) <= dateBounds[1];
return new Date(date) >= startingDate && new Date(date) <= endingDate;
};

export default ({ d3 = window.d3, ...customConfiguration }) => {
const chart = selection => {
Expand Down
28 changes: 27 additions & 1 deletion src/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EventDrops from './';
import EventDrops, { withinRange } from './';
import zoom from './zoom';

jest.mock('./zoom');
Expand Down Expand Up @@ -157,3 +157,29 @@ describe('EventDrops', () => {
jest.restoreAllMocks();
});
});

describe('withinRange', () => {
it('should return true if date is in given date range', () => {
const dateRange = [new Date('2018-04-01'), new Date('2018-05-01')];
const test = (date, expectedResult) => {
expect(withinRange(date, dateRange)).toBe(expectedResult);
};

test('2018-04-19', true);
test('2018-04-01', true);
test('2018-05-01', true);
test('2018-05-19', false);
});

it('should return true if date is in given reverse date range (start date higher than end date', () => {

Choose a reason for hiding this comment

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

(start date older than end date)

const dateRange = [new Date('2018-05-01'), new Date('2018-04-01')];
const test = (date, expectedResult) => {
expect(withinRange(date, dateRange)).toBe(expectedResult);
};

test('2018-04-19', true);
test('2018-04-01', true);
test('2018-05-01', true);
test('2018-05-19', false);
});
});