Skip to content

Commit

Permalink
Merge pull request #231 from marmelab/revert_axis
Browse files Browse the repository at this point in the history
[RFR] Allow to display chart from end to start dates
  • Loading branch information
Julien Demangeon committed Mar 20, 2018
2 parents 7317b83 + d7b6ca2 commit 0af34b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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', () => {
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);
});
});

0 comments on commit 0af34b9

Please sign in to comment.