import { v4 as uuidv4 } from 'uuid' import { getDateSourceValue } from './index' import { getDateTimeFromISO } from './datetime' import type { Binding, BindingSource } from './bindings' const now = getDateTimeFromISO('2024-02-14T12:00:00', { locale: 'en-US', timezone: 'America/New_York', }) const makeBinding = (options: BindingSource['options'], id = uuidv4()): Binding => { return { id, source: { type: 'dateTime', dataType: 'date', options, }, bindingType: 'binding', type: 'binding', label: 'test', format: { type: 'dateTime', }, } } describe('bindings', () => { describe('getDateSourceValue', () => { it.each([ [true, '2024-02-14T17:00:00.000Z'], [false, '2024-02-14T12:00:00.000-05:00'], ])('should return a relative date string, no options', (isAction, expected) => expect( getDateSourceValue(makeBinding({}), isAction, { now, locale: 'en-US', timezone: 'America/New_York', })).toBe(expected) ) it.each([ ['Current Time', {}, '2024-02-14T12:00:00.000-05:00'], ['Start of Today', { startOfDay: true }, '2024-02-14T00:00:00.000-05:00'], ['1 year ago', { startOfDay: true, offset: -365 }, '2023-02-14T00:00:00.000-05:00'], ['6 months ago', { startOfDay: true, offset: -180 }, '2023-08-14T00:00:00.000-04:00'], ['90 days ago', { startOfDay: true, offset: -90 }, '2023-11-16T00:00:00.000-05:00'], ['60 days ago', { startOfDay: true, offset: -60 }, '2023-12-16T00:00:00.000-05:00'], ['30 days ago', { startOfDay: true, offset: -30 }, '2024-01-15T00:00:00.000-05:00'], ['2 weeks ago', { startOfDay: true, offset: -14 }, '2024-01-31T00:00:00.000-05:00'], ['1 week ago', { startOfDay: true, offset: -7 }, '2024-02-07T00:00:00.000-05:00'], ['6 days ago', { startOfDay: true, offset: -6 }, '2024-02-08T00:00:00.000-05:00'], ['5 days ago', { startOfDay: true, offset: -5 }, '2024-02-09T00:00:00.000-05:00'], ['4 days ago', { startOfDay: true, offset: -4 }, '2024-02-10T00:00:00.000-05:00'], ['3 days ago', { startOfDay: true, offset: -3 }, '2024-02-11T00:00:00.000-05:00'], ['2 days ago', { startOfDay: true, offset: -2 }, '2024-02-12T00:00:00.000-05:00'], ['1 day ago', { startOfDay: true, offset: -1 }, '2024-02-13T00:00:00.000-05:00'], ['12 hours ago', { offset: -0.5 }, '2024-02-14T00:00:00.000-05:00'], ['6 hours ago', { offset: -0.25 }, '2024-02-14T06:00:00.000-05:00'], ['3 hours ago', { offset: -0.125 }, '2024-02-14T09:00:00.000-05:00'], ['2 hours ago', { offset: -0.083333333333333 }, '2024-02-14T10:00:00.000-05:00'], ['1 hour ago', { offset: -0.041666666666666 }, '2024-02-14T11:00:00.000-05:00'], ['30 minutes ago', { offset: -0.020833333333333 }, '2024-02-14T11:30:00.000-05:00'], ['15 minutes ago', { offset: -0.010416666666666 }, '2024-02-14T11:45:00.000-05:00'], ['5 minutes ago', { offset: -0.003472222222222 }, '2024-02-14T11:55:00.000-05:00'], ['1 minute ago', { offset: -0.000694444444444 }, '2024-02-14T11:59:00.000-05:00'], ['1 minute from now', { offset: 0.000694444444444 }, '2024-02-14T12:01:00.000-05:00'], ['5 minutes from now', { offset: 0.003472222222222 }, '2024-02-14T12:05:00.000-05:00'], ['15 minutes from now', { offset: 0.010416666666666 }, '2024-02-14T12:15:00.000-05:00'], ['30 minutes from now', { offset: 0.020833333333333 }, '2024-02-14T12:30:00.000-05:00'], ['1 hour from now', { offset: 0.041666666666666 }, '2024-02-14T13:00:00.000-05:00'], ['2 hours from now', { offset: 0.083333333333333 }, '2024-02-14T14:00:00.000-05:00'], ['3 hours from now', { offset: 0.125 }, '2024-02-14T15:00:00.000-05:00'], ['6 hours from now', { offset: 0.25 }, '2024-02-14T18:00:00.000-05:00'], ['12 hours from now', { offset: 0.5 }, '2024-02-15T00:00:00.000-05:00'], ['Tomorrow', { startOfDay: true, offset: 1 }, '2024-02-15T00:00:00.000-05:00'], ['2 days from now', { startOfDay: true, offset: 2 }, '2024-02-16T00:00:00.000-05:00'], ['3 days from now', { startOfDay: true, offset: 3 }, '2024-02-17T00:00:00.000-05:00'], ['4 days from now', { startOfDay: true, offset: 4 }, '2024-02-18T00:00:00.000-05:00'], ['5 days from now', { startOfDay: true, offset: 5 }, '2024-02-19T00:00:00.000-05:00'], ['6 days from now', { startOfDay: true, offset: 6 }, '2024-02-20T00:00:00.000-05:00'], ['1 week from now', { startOfDay: true, offset: 7 }, '2024-02-21T00:00:00.000-05:00'], ['2 weeks from now', { startOfDay: true, offset: 14 }, '2024-02-28T00:00:00.000-05:00'], ['30 days from now', { startOfDay: true, offset: 30 }, '2024-03-15T00:00:00.000-04:00'], ['60 days from now', { startOfDay: true, offset: 60 }, '2024-04-14T00:00:00.000-04:00'], ['90 days from now', { startOfDay: true, offset: 90 }, '2024-05-14T00:00:00.000-04:00'], ['6 months from now', { startOfDay: true, offset: 180 }, '2024-08-14T00:00:00.000-04:00'], ['1 year from now', { startOfDay: true, offset: 365 }, '2025-02-14T00:00:00.000-05:00'], ])('Should return a relative date string: %s, isAction=false', (_name, options, expected) => expect( getDateSourceValue(makeBinding(options), false, { now, locale: 'en-US', timezone: 'America/New_York', })).toBe(expected) ) it.each([ ['Current Time', {}, '2024-02-14T17:00:00.000Z'], ['Start of Today', { startOfDay: true }, '2024-02-14T05:00:00.000Z'], ['1 year ago', { startOfDay: true, offset: -365 }, '2023-02-14T05:00:00.000Z'], ['6 months ago', { startOfDay: true, offset: -180 }, '2023-08-14T04:00:00.000Z'], ['90 days ago', { startOfDay: true, offset: -90 }, '2023-11-16T05:00:00.000Z'], ['60 days ago', { startOfDay: true, offset: -60 }, '2023-12-16T05:00:00.000Z'], ['30 days ago', { startOfDay: true, offset: -30 }, '2024-01-15T05:00:00.000Z'], ['2 weeks ago', { startOfDay: true, offset: -14 }, '2024-01-31T05:00:00.000Z'], ['1 week ago', { startOfDay: true, offset: -7 }, '2024-02-07T05:00:00.000Z'], ['6 days ago', { startOfDay: true, offset: -6 }, '2024-02-08T05:00:00.000Z'], ['5 days ago', { startOfDay: true, offset: -5 }, '2024-02-09T05:00:00.000Z'], ['4 days ago', { startOfDay: true, offset: -4 }, '2024-02-10T05:00:00.000Z'], ['3 days ago', { startOfDay: true, offset: -3 }, '2024-02-11T05:00:00.000Z'], ['2 days ago', { startOfDay: true, offset: -2 }, '2024-02-12T05:00:00.000Z'], ['1 day ago', { startOfDay: true, offset: -1 }, '2024-02-13T05:00:00.000Z'], ['12 hours ago', { offset: -0.5 }, '2024-02-14T05:00:00.000Z'], ['6 hours ago', { offset: -0.25 }, '2024-02-14T11:00:00.000Z'], ['3 hours ago', { offset: -0.125 }, '2024-02-14T14:00:00.000Z'], ['2 hours ago', { offset: -0.083333333333333 }, '2024-02-14T15:00:00.000Z'], ['1 hour ago', { offset: -0.041666666666666 }, '2024-02-14T16:00:00.000Z'], ['30 minutes ago', { offset: -0.020833333333333 }, '2024-02-14T16:30:00.000Z'], ['15 minutes ago', { offset: -0.010416666666666 }, '2024-02-14T16:45:00.000Z'], ['5 minutes ago', { offset: -0.003472222222222 }, '2024-02-14T16:55:00.000Z'], ['1 minute ago', { offset: -0.000694444444444 }, '2024-02-14T16:59:00.000Z'], ['1 minute from now', { offset: 0.000694444444444 }, '2024-02-14T17:01:00.000Z'], ['5 minutes from now', { offset: 0.003472222222222 }, '2024-02-14T17:05:00.000Z'], ['15 minutes from now', { offset: 0.010416666666666 }, '2024-02-14T17:15:00.000Z'], ['30 minutes from now', { offset: 0.020833333333333 }, '2024-02-14T17:30:00.000Z'], ['1 hour from now', { offset: 0.041666666666666 }, '2024-02-14T18:00:00.000Z'], ['2 hours from now', { offset: 0.083333333333333 }, '2024-02-14T19:00:00.000Z'], ['3 hours from now', { offset: 0.125 }, '2024-02-14T20:00:00.000Z'], ['6 hours from now', { offset: 0.25 }, '2024-02-14T23:00:00.000Z'], ['12 hours from now', { offset: 0.5 }, '2024-02-15T05:00:00.000Z'], ['Tomorrow', { startOfDay: true, offset: 1 }, '2024-02-15T05:00:00.000Z'], ['2 days from now', { startOfDay: true, offset: 2 }, '2024-02-16T05:00:00.000Z'], ['3 days from now', { startOfDay: true, offset: 3 }, '2024-02-17T05:00:00.000Z'], ['4 days from now', { startOfDay: true, offset: 4 }, '2024-02-18T05:00:00.000Z'], ['5 days from now', { startOfDay: true, offset: 5 }, '2024-02-19T05:00:00.000Z'], ['6 days from now', { startOfDay: true, offset: 6 }, '2024-02-20T05:00:00.000Z'], ['1 week from now', { startOfDay: true, offset: 7 }, '2024-02-21T05:00:00.000Z'], ['2 weeks from now', { startOfDay: true, offset: 14 }, '2024-02-28T05:00:00.000Z'], ['30 days from now', { startOfDay: true, offset: 30 }, '2024-03-15T04:00:00.000Z'], ['60 days from now', { startOfDay: true, offset: 60 }, '2024-04-14T04:00:00.000Z'], ['90 days from now', { startOfDay: true, offset: 90 }, '2024-05-14T04:00:00.000Z'], ['6 months from now', { startOfDay: true, offset: 180 }, '2024-08-14T04:00:00.000Z'], ['1 year from now', { startOfDay: true, offset: 365 }, '2025-02-14T05:00:00.000Z'], ])('Should return a relative date string: %s, isAction=true', (_name, options, expected) => expect( getDateSourceValue(makeBinding(options), true, { now, locale: 'en-US', timezone: 'America/New_York', })).toBe(expected) ) }) })