|
| 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 | +let instance = null; |
| 8 | +const endDate = new Date(); |
| 9 | +const startDate = subDays(endDate, 7); |
| 10 | + |
| 11 | +const commonProps = { |
| 12 | + ranges: [{ startDate, endDate, key: 'selection' }], |
| 13 | + onChange: () => {}, |
| 14 | + moveRangeOnFirstSelection: false, |
| 15 | +}; |
| 16 | + |
| 17 | +const compareRanges = (newRange, assertionRange) => { |
| 18 | + ['startDate', 'endDate'].forEach(key => { |
| 19 | + if (!newRange[key] || !assertionRange[key]) { |
| 20 | + return expect(newRange[key]).toEqual(assertionRange[key]); |
| 21 | + } |
| 22 | + return expect(isSameDay(newRange[key], assertionRange[key])).toEqual(true); |
| 23 | + }); |
| 24 | +}; |
| 25 | + |
| 26 | +beforeEach(() => { |
| 27 | + testRenderer = renderer.create(<DateRange {...commonProps} />); |
| 28 | + instance = testRenderer.getInstance(); |
| 29 | +}); |
2 | 30 |
|
3 | 31 | describe('DateRange', () => { |
4 | 32 | test('Should resolve', () => { |
5 | 33 | expect(DateRange).toEqual(expect.anything()); |
6 | 34 | }); |
| 35 | + |
| 36 | + test('calculate new selection by resetting end date', () => { |
| 37 | + const methodResult = instance.calcNewSelection(subDays(endDate, 10), true); |
| 38 | + compareRanges(methodResult.range, { |
| 39 | + startDate: subDays(endDate, 10), |
| 40 | + endDate: subDays(endDate, 10), |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + test('calculate new selection by resetting end date if start date is not before', () => { |
| 45 | + const methodResult = instance.calcNewSelection(addDays(endDate, 2), true); |
| 46 | + compareRanges(methodResult.range, { |
| 47 | + startDate: addDays(endDate, 2), |
| 48 | + endDate: addDays(endDate, 2), |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + test('calculate new selection based on moveRangeOnFirstSelection prop', () => { |
| 53 | + testRenderer.update(<DateRange {...commonProps} moveRangeOnFirstSelection />); |
| 54 | + const methodResult = instance.calcNewSelection(subDays(endDate, 10), true); |
| 55 | + compareRanges(methodResult.range, { |
| 56 | + startDate: subDays(endDate, 10), |
| 57 | + endDate: subDays(endDate, 3), |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + test('calculate new selection by retaining end date, based on retainEndDateOnFirstSelection prop', () => { |
| 62 | + testRenderer.update(<DateRange {...commonProps} retainEndDateOnFirstSelection />); |
| 63 | + const methodResult = instance.calcNewSelection(subDays(endDate, 10), true); |
| 64 | + compareRanges(methodResult.range, { |
| 65 | + startDate: subDays(endDate, 10), |
| 66 | + endDate, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + test('calculate new selection by retaining the unset end date, based on retainEndDateOnFirstSelection prop', () => { |
| 71 | + testRenderer.update( |
| 72 | + <DateRange |
| 73 | + {...commonProps} |
| 74 | + ranges={[{ ...commonProps.ranges[0], endDate: null }]} |
| 75 | + retainEndDateOnFirstSelection |
| 76 | + /> |
| 77 | + ); |
| 78 | + const methodResult = instance.calcNewSelection(subDays(endDate, 10), true); |
| 79 | + compareRanges(methodResult.range, { |
| 80 | + startDate: subDays(endDate, 10), |
| 81 | + endDate: null, |
| 82 | + }); |
| 83 | + }); |
7 | 84 | }); |
0 commit comments