Skip to content

Commit

Permalink
only round lower bound, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smith committed Jan 26, 2021
1 parent 11dec6e commit 6b448d2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as helpers from './helpers';
describe('url_params_context helpers', () => {
describe('getDateRange', () => {
describe('with non-rounded dates', () => {
it('rounds the values', () => {
it('rounds the lower value', () => {
expect(
helpers.getDateRange({
state: {},
Expand All @@ -20,7 +20,7 @@ describe('url_params_context helpers', () => {
})
).toEqual({
start: '1970-01-01T00:00:00.000Z',
end: '1971-02-01T00:00:00.000Z',
end: '1971-01-10T10:11:12.123Z',
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ export function getDateRange({
return { start: state.start, end: state.end };
}

// Calculate ticks for the time ranges to produce nicely rounded values
// Calculate ticks for the time ranges to produce nicely rounded values. We
// only use lower value since the upper end is not likely to matter because the
// cache is constancly cleared when there are writes to the shard.
const ticks = scaleUtc().domain([start, end]).nice().ticks();

// Return the first and last tick values.
return {
start: ticks[0].toISOString(),
end: ticks[ticks.length - 1].toISOString(),
end: end.toISOString(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ describe('UrlParamsContext', () => {

const wrapper = mountParams(location);
const params = getDataFromOutput(wrapper);
expect(params.start).toEqual('2010-03-15T00:00:00.000Z');
expect(params.end).toEqual('2010-04-11T00:00:00.000Z');

expect([params.start, params.end]).toEqual([
'2010-03-15T00:00:00.000Z',
'2010-04-10T12:00:00.000Z',
]);
});

it('should update param values if location has changed', () => {
Expand All @@ -65,8 +68,11 @@ describe('UrlParamsContext', () => {
// force an update
wrapper.setProps({ abc: 123 });
const params = getDataFromOutput(wrapper);
expect(params.start).toEqual('2009-03-15T00:00:00.000Z');
expect(params.end).toEqual('2009-04-11T00:00:00.000Z');

expect([params.start, params.end]).toEqual([
'2009-03-15T00:00:00.000Z',
'2009-04-10T12:00:00.000Z',
]);
});

it('should parse relative time ranges on mount', () => {
Expand All @@ -85,7 +91,7 @@ describe('UrlParamsContext', () => {

expect([params.start, params.end]).toEqual([
'1969-12-31T00:00:00.000Z',
'1970-01-01T00:00:00.000Z',
'1969-12-31T23:59:59.999Z',
]);

nowSpy.mockRestore();
Expand Down Expand Up @@ -136,8 +142,11 @@ describe('UrlParamsContext', () => {
expect(calls.length).toBe(2);

const params = getDataFromOutput(wrapper);
expect(params.start).toEqual('2005-09-19T00:00:00.000Z');
expect(params.end).toEqual('2005-10-23T00:00:00.000Z');

expect([params.start, params.end]).toEqual([
'2005-09-19T00:00:00.000Z',
'2005-10-21T12:00:00.000Z',
]);
});

it('should refresh the time range with new values if time range is relative', async () => {
Expand Down Expand Up @@ -183,7 +192,10 @@ describe('UrlParamsContext', () => {
await waitFor(() => {});

const params = getDataFromOutput(wrapper);
expect(params.start).toEqual('2000-06-14T00:00:00.000Z');
expect(params.end).toEqual('2000-06-15T00:00:00.000Z');

expect([params.start, params.end]).toEqual([
'2000-06-14T00:00:00.000Z',
'2000-06-14T23:59:59.999Z',
]);
});
});

0 comments on commit 6b448d2

Please sign in to comment.