gristlabs_grist-core/test/common/RelativeDates.ts
Cyprien P 620e86a9f1 (core) New date filter with a calendar view
Summary:
Implements the new date filtering panel. Design results from long
discussion between: Alex, Anais, Cyprien and Dmitry.

Test environment: https://grist-new-date-range-filter.fly.dev/

Test Plan: Include various new tests.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D3720
2022-12-20 09:58:42 +01:00

63 lines
2.0 KiB
TypeScript

import {DEPS, getMatchingDoubleRelativeDate} from 'app/client/ui/RelativeDatesOptions';
import sinon from 'sinon';
import {assert} from 'chai';
import moment from 'moment-timezone';
import {diffUnit} from 'app/common/RelativeDates';
const CURRENT_TIME = moment.tz('2022-09-26T12:13:32.018Z', 'utc');
const now = () => moment(CURRENT_TIME);
describe('RelativeDates', function() {
const sandbox = sinon.createSandbox();
before(() => {
sinon.stub(DEPS, 'getCurrentTime').returns(now());
});
after(() => {
sandbox.restore();
});
describe('getMatchingDoubleRelativeDate', function() {
it('should work correctly', function() {
assert.deepEqual(
getMatchingDoubleRelativeDate(getDateValue('10/1/2022'), {unit: 'month'}),
[{unit: 'month', quantity: 1}]
);
assert.deepEqual(
getMatchingDoubleRelativeDate(getDateValue('9/19/2022'), {unit: 'week'}),
[{unit: 'week', quantity: -1}, {quantity: 1, unit: 'day'}]
);
assert.deepEqual(
getMatchingDoubleRelativeDate(getDateValue('9/21/2022'), {unit: 'week'}),
[{unit: 'week', quantity: -1}, {quantity: 3, unit: 'day'}]
);
assert.deepEqual(
getMatchingDoubleRelativeDate(getDateValue('9/30/2022'), {unit: 'month'}),
[{unit: 'month', quantity: 0}, {quantity: 29, unit: 'day'}]
);
assert.deepEqual(
getMatchingDoubleRelativeDate(getDateValue('10/1/2022'), {unit: 'month'}),
[{unit: 'month', quantity: 1}]
);
});
});
describe('diffUnit', function() {
it('should work correctly', function() {
assert.equal(diffUnit(moment('2022-09-30'), moment('2022-10-01'), 'month'), -1);
assert.equal(diffUnit(moment('2022-10-01'), moment('2022-09-30'), 'month'), 1);
assert.equal(diffUnit(moment('2022-09-30'), moment('2022-10-01'), 'week'), 0);
assert.equal(diffUnit(moment('2022-09-30'), moment('2022-10-02'), 'week'), -1);
});
});
});
function getDateValue(date: string): number {
return moment.tz(date, "MM-DD-YYYY", 'utc').valueOf()/1000;
}