2023-01-10 05:06:12 -08:00
|
|
|
/**
|
|
|
|
* @jest-environment jsdom
|
|
|
|
*/
|
|
|
|
|
2023-01-23 05:28:17 -08:00
|
|
|
import { DateTime } from 'luxon';
|
2023-10-27 05:17:52 -07:00
|
|
|
import { getGlobalState } from '@/GlobalState';
|
|
|
|
import { evaluate, getLocalISOString } from './Helpers';
|
2024-03-20 04:05:54 -07:00
|
|
|
import { dateExtensions } from '../../src/Extensions/DateExtensions';
|
2023-10-27 05:17:52 -07:00
|
|
|
|
|
|
|
const { defaultTimezone } = getGlobalState();
|
2023-01-10 05:06:12 -08:00
|
|
|
|
|
|
|
describe('Data Transformation Functions', () => {
|
|
|
|
describe('Date Data Transformation Functions', () => {
|
|
|
|
test('.isWeekend() should work correctly on a date', () => {
|
2023-01-23 05:28:17 -08:00
|
|
|
expect(evaluate('={{ DateTime.local(2023, 1, 20).isWeekend() }}')).toBe(false);
|
|
|
|
expect(evaluate('={{ DateTime.local(2023, 1, 21).isWeekend() }}')).toBe(true);
|
|
|
|
expect(evaluate('={{ DateTime.local(2023, 1, 22).isWeekend() }}')).toBe(true);
|
|
|
|
expect(evaluate('={{ DateTime.local(2023, 1, 23).isWeekend() }}')).toBe(false);
|
2023-01-10 05:06:12 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('.beginningOf("week") should work correctly on a date', () => {
|
2023-01-23 05:28:17 -08:00
|
|
|
expect(evaluate('={{ DateTime.local(2023, 1, 20).beginningOf("week") }}')).toEqual(
|
2023-10-27 05:17:52 -07:00
|
|
|
DateTime.local(2023, 1, 16, { zone: defaultTimezone }),
|
2023-06-15 01:07:47 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(evaluate('={{ new Date(2023, 0, 20).beginningOf("week") }}')).toEqual(
|
2023-10-27 05:17:52 -07:00
|
|
|
DateTime.local(2023, 1, 16, { zone: defaultTimezone }).toJSDate(),
|
2023-01-10 05:06:12 -08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-02-15 01:50:16 -08:00
|
|
|
test('.beginningOf("week") should work correctly on a string', () => {
|
|
|
|
const evaluatedDate = evaluate('={{ "2023-01-30".toDate().beginningOf("week") }}');
|
2023-10-27 05:17:52 -07:00
|
|
|
const expectedDate = DateTime.local(2023, 1, 23, { zone: defaultTimezone }).toJSDate();
|
2023-02-15 01:50:16 -08:00
|
|
|
|
|
|
|
if (evaluatedDate && evaluatedDate instanceof Date) {
|
|
|
|
expect(evaluatedDate.toDateString()).toEqual(expectedDate.toDateString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('.beginningOf("month") should work correctly on a string', () => {
|
|
|
|
const evaluatedDate = evaluate('={{ "2023-06-16".toDate().beginningOf("month") }}');
|
2023-10-27 05:17:52 -07:00
|
|
|
const expectedDate = DateTime.local(2023, 6, 1, { zone: defaultTimezone }).toJSDate();
|
2023-02-15 01:50:16 -08:00
|
|
|
|
|
|
|
if (evaluatedDate && evaluatedDate instanceof Date) {
|
|
|
|
expect(evaluatedDate.toDateString()).toEqual(expectedDate.toDateString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('.beginningOf("year") should work correctly on a string', () => {
|
|
|
|
const evaluatedDate = evaluate('={{ "2023-01-30".toDate().beginningOf("year") }}');
|
2023-10-27 05:17:52 -07:00
|
|
|
const expectedDate = DateTime.local(2023, 1, 1, { zone: defaultTimezone }).toJSDate();
|
2023-02-15 01:50:16 -08:00
|
|
|
|
|
|
|
if (evaluatedDate && evaluatedDate instanceof Date) {
|
|
|
|
expect(evaluatedDate.toDateString()).toEqual(expectedDate.toDateString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-10 05:06:12 -08:00
|
|
|
test('.endOfMonth() should work correctly on a date', () => {
|
2023-01-23 05:28:17 -08:00
|
|
|
expect(evaluate('={{ DateTime.local(2023, 1, 16).endOfMonth() }}')).toEqual(
|
2023-10-27 05:17:52 -07:00
|
|
|
DateTime.local(2023, 1, 31, 23, 59, 59, 999, { zone: defaultTimezone }),
|
2023-06-15 01:07:47 -07:00
|
|
|
);
|
|
|
|
expect(evaluate('={{ new Date(2023, 0, 16).endOfMonth() }}')).toEqual(
|
2023-10-27 05:17:52 -07:00
|
|
|
DateTime.local(2023, 1, 31, 23, 59, 59, 999, { zone: defaultTimezone }).toJSDate(),
|
2023-01-10 05:06:12 -08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('.extract("day") should work correctly on a date', () => {
|
2023-01-23 05:28:17 -08:00
|
|
|
expect(evaluate('={{ DateTime.local(2023, 1, 20).extract("day") }}')).toEqual(20);
|
2023-01-10 05:06:12 -08:00
|
|
|
});
|
|
|
|
|
2023-02-15 01:50:16 -08:00
|
|
|
test('.extract() should extract week for no args', () => {
|
|
|
|
expect(evaluate('={{ DateTime.local(2023, 1, 20).extract() }}')).toEqual(3);
|
|
|
|
});
|
|
|
|
|
2023-01-10 05:06:12 -08:00
|
|
|
test('.format("yyyy LLL dd") should work correctly on a date', () => {
|
2023-01-23 05:28:17 -08:00
|
|
|
expect(evaluate('={{ DateTime.local(2023, 1, 16).format("yyyy LLL dd") }}')).toEqual(
|
|
|
|
'2023 Jan 16',
|
2023-01-10 05:06:12 -08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('.toDate() should work on a string', () => {
|
2023-01-12 07:06:34 -08:00
|
|
|
const date = new Date(2022, 0, 3);
|
|
|
|
expect(evaluate(`={{ "${getLocalISOString(date)}".toDate() }}`)).toEqual(date);
|
2023-01-10 05:06:12 -08:00
|
|
|
});
|
2023-02-15 01:50:16 -08:00
|
|
|
|
|
|
|
test('.inBetween() should work on string and Date', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
expect(evaluate("={{ $now.isBetween('2023-06-23'.toDate(), '2023-06-23') }}")).toBeDefined();
|
2023-02-15 01:50:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('.inBetween() should work on string and DateTime', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
expect(evaluate("={{ $now.isBetween($now, '2023-06-23') }}")).toBeDefined();
|
2023-02-15 01:50:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('.inBetween() should not work for invalid strings', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
expect(evaluate("={{ $now.isBetween($now, 'invalid') }}")).toBeUndefined();
|
2023-02-15 01:50:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('.inBetween() should not work for numbers', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
expect(evaluate('={{ $now.isBetween($now, 1) }}')).toBeUndefined();
|
2023-02-15 01:50:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('.inBetween() should not work for a single argument', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
expect(() => evaluate('={{ $now.isBetween($now) }}')).toThrow();
|
2023-02-15 01:50:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('.inBetween() should not work for a more than two arguments', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
expect(() =>
|
|
|
|
evaluate("={{ $now.isBetween($now, '2023-06-23', '2023-09-21'.toDate()) }}"),
|
|
|
|
).toThrow();
|
2023-02-15 01:50:16 -08:00
|
|
|
});
|
2024-03-20 04:05:54 -07:00
|
|
|
|
|
|
|
describe('toDateTime', () => {
|
|
|
|
test('should return itself for DateTime', () => {
|
|
|
|
const result = evaluate(
|
|
|
|
"={{ DateTime.fromFormat('01-01-2024', 'dd-MM-yyyy').toDateTime() }}",
|
|
|
|
) as unknown as DateTime;
|
|
|
|
expect(result).toBeInstanceOf(DateTime);
|
|
|
|
expect(result.day).toEqual(1);
|
|
|
|
expect(result.month).toEqual(1);
|
|
|
|
expect(result.year).toEqual(2024);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return a DateTime for JS Date', () => {
|
|
|
|
const result = evaluate(
|
|
|
|
'={{ new Date(2024, 0, 1, 12).toDateTime() }}',
|
|
|
|
) as unknown as DateTime;
|
|
|
|
expect(result).toBeInstanceOf(DateTime);
|
|
|
|
expect(result.day).toEqual(1);
|
|
|
|
expect(result.month).toEqual(1);
|
|
|
|
expect(result.year).toEqual(2024);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('toInt/toFloat', () => {
|
|
|
|
test('should return milliseconds for DateTime', () => {
|
|
|
|
expect(evaluate("={{ DateTime.fromISO('2024-01-01T00:00:00.000Z').toInt() }}")).toEqual(
|
|
|
|
1704067200000,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return milliseconds for JS Date', () => {
|
|
|
|
expect(evaluate('={{ new Date("2024-01-01T00:00:00.000Z").toFloat() }}')).toEqual(
|
|
|
|
1704067200000,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not have a doc (hidden from autocomplete)', () => {
|
|
|
|
expect(dateExtensions.functions.toInt.doc).toBeUndefined();
|
|
|
|
expect(dateExtensions.functions.toFloat.doc).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('toBoolean', () => {
|
|
|
|
test('should return undefined', () => {
|
|
|
|
expect(evaluate('={{ new Date("2024-01-01T00:00:00.000Z").toBoolean() }}')).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not have a doc (hidden from autocomplete)', () => {
|
|
|
|
expect(dateExtensions.functions.toBoolean.doc).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
2023-01-10 05:06:12 -08:00
|
|
|
});
|
|
|
|
});
|