|
| 1 | +import React from 'react'; |
| 2 | +import { subDays, addDays, isSameDay } from 'date-fns'; |
1 | 3 | import DateRange from '../DateRange'; |
| 4 | +import renderer from 'react-test-renderer'; |
| 5 | + |
| 6 | +let testRenderer = null; |
| 7 | +const endDate = new Date(); |
| 8 | +const startDate = subDays(endDate, 7); |
| 9 | + |
| 10 | +const commonProps = { |
| 11 | + ranges: [{ startDate, endDate, key: 'selection' }], |
| 12 | + onChange: () => {}, |
| 13 | + moveRangeOnFirstSelection: false, |
| 14 | +}; |
| 15 | + |
| 16 | +const compareRanges = (newRange, assertionRange) => { |
| 17 | + expect(isSameDay(newRange.startDate, assertionRange.startDate)).toEqual(true); |
| 18 | + expect(isSameDay(newRange.endDate, assertionRange.endDate)).toEqual(true); |
| 19 | +}; |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + testRenderer = renderer.create(<DateRange {...commonProps} />); |
| 23 | +}); |
2 | 24 |
|
3 | 25 | describe('DateRange', () => { |
4 | 26 | test('Should resolve', () => { |
5 | 27 | expect(DateRange).toEqual(expect.anything()); |
6 | 28 | }); |
| 29 | + |
| 30 | + test('calculate new selection without moving end date', () => { |
| 31 | + const instance = testRenderer.getInstance(); |
| 32 | + const methodResult = instance.calcNewSelection(subDays(endDate, 10), true); |
| 33 | + compareRanges(methodResult.range, { |
| 34 | + startDate: subDays(endDate, 10), |
| 35 | + endDate, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + test('calculate new selection by resetting end date if start date is not before', () => { |
| 40 | + const instance = testRenderer.getInstance(); |
| 41 | + const methodResult = instance.calcNewSelection(addDays(endDate, 2), true); |
| 42 | + compareRanges(methodResult.range, { |
| 43 | + startDate: addDays(endDate, 2), |
| 44 | + endDate: addDays(endDate, 2), |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + test('calculate new selection based on moveRangeOnFirstSelection prop', () => { |
| 49 | + testRenderer.update(<DateRange {...commonProps} moveRangeOnFirstSelection />); |
| 50 | + const instance = testRenderer.getInstance(); |
| 51 | + const methodResult = instance.calcNewSelection(subDays(endDate, 10), true); |
| 52 | + compareRanges(methodResult.range, { |
| 53 | + startDate: subDays(endDate, 10), |
| 54 | + endDate: subDays(endDate, 3), |
| 55 | + }); |
| 56 | + }); |
7 | 57 | }); |
0 commit comments