|
| 1 | +/** |
| 2 | + * Test to compare different date comparison methods |
| 3 | + * |
| 4 | + * Question: Is valueOf() the best method, or should we use isBefore/isAfter? |
| 5 | + */ |
| 6 | + |
| 7 | +import * as helper from '../src/helper'; |
| 8 | +import moment from 'moment'; |
| 9 | + |
| 10 | +describe('Date Comparison Methods', () => { |
| 11 | + beforeAll(() => { |
| 12 | + if (typeof (global as any).window === 'undefined') { |
| 13 | + (global as any).window = {}; |
| 14 | + } |
| 15 | + (global as any).window.moment = moment; |
| 16 | + }); |
| 17 | + |
| 18 | + describe('valueOf() vs isBefore/isAfter', () => { |
| 19 | + it('should produce same results with valueOf() and isBefore/isAfter', () => { |
| 20 | + const startDate = helper.strToDate('20240201', 'YYYYMMDD'); |
| 21 | + const endDate = helper.strToDate('20240203', 'YYYYMMDD'); |
| 22 | + const testDate = helper.strToDate('20240202', 'YYYYMMDD'); |
| 23 | + |
| 24 | + // Method 1: valueOf() (current implementation) |
| 25 | + const inRangeValueOf = |
| 26 | + testDate.valueOf() >= startDate.valueOf() && |
| 27 | + testDate.valueOf() <= endDate.valueOf(); |
| 28 | + |
| 29 | + // Method 2: isBefore/isAfter with 'day' granularity |
| 30 | + const inRangeMoment = |
| 31 | + !testDate.isBefore(startDate, 'day') && |
| 32 | + !testDate.isAfter(endDate, 'day'); |
| 33 | + |
| 34 | + // Method 3: isSameOrAfter/isSameOrBefore |
| 35 | + const inRangeSame = |
| 36 | + testDate.isSameOrAfter(startDate, 'day') && |
| 37 | + testDate.isSameOrBefore(endDate, 'day'); |
| 38 | + |
| 39 | + expect(inRangeValueOf).toBe(true); |
| 40 | + expect(inRangeMoment).toBe(true); |
| 41 | + expect(inRangeSame).toBe(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should handle boundary dates correctly with both methods', () => { |
| 45 | + const startDate = helper.strToDate('20240201', 'YYYYMMDD'); |
| 46 | + const endDate = helper.strToDate('20240203', 'YYYYMMDD'); |
| 47 | + |
| 48 | + // Test start date boundary |
| 49 | + const onStartDate = helper.strToDate('20240201', 'YYYYMMDD'); |
| 50 | + expect(onStartDate.valueOf() >= startDate.valueOf()).toBe(true); |
| 51 | + expect(!onStartDate.isBefore(startDate, 'day')).toBe(true); |
| 52 | + expect(onStartDate.isSameOrAfter(startDate, 'day')).toBe(true); |
| 53 | + |
| 54 | + // Test end date boundary |
| 55 | + const onEndDate = helper.strToDate('20240203', 'YYYYMMDD'); |
| 56 | + expect(onEndDate.valueOf() <= endDate.valueOf()).toBe(true); |
| 57 | + expect(!onEndDate.isAfter(endDate, 'day')).toBe(true); |
| 58 | + expect(onEndDate.isSameOrBefore(endDate, 'day')).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle dates normalized to start of day', () => { |
| 62 | + // strToDate normalizes to startOf('day'), so all dates should be at midnight |
| 63 | + const date1 = helper.strToDate('20240201', 'YYYYMMDD'); |
| 64 | + const date2 = helper.strToDate('20240201', 'YYYYMMDD'); |
| 65 | + |
| 66 | + // Both should be at midnight (00:00:00) |
| 67 | + expect(date1.hours()).toBe(0); |
| 68 | + expect(date1.minutes()).toBe(0); |
| 69 | + expect(date1.seconds()).toBe(0); |
| 70 | + expect(date1.valueOf()).toBe(date2.valueOf()); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('Performance and clarity', () => { |
| 75 | + it('should note that isBefore/isAfter is more explicit about day granularity', () => { |
| 76 | + // isBefore/isAfter with 'day' is more explicit about what we're comparing |
| 77 | + // valueOf() works but is less clear about granularity |
| 78 | + const date1 = helper.strToDate('20240201', 'YYYYMMDD'); |
| 79 | + const date2 = helper.strToDate('20240201', 'YYYYMMDD'); |
| 80 | + |
| 81 | + // Both methods work, but isBefore with 'day' is more explicit |
| 82 | + expect(date1.isBefore(date2, 'day')).toBe(false); |
| 83 | + expect(date1.valueOf() < date2.valueOf()).toBe(false); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments