From 683492155e9c647ef2ff8ad61c1f4ad4f614af32 Mon Sep 17 00:00:00 2001 From: Valya <68596159+valya@users.noreply.github.com> Date: Mon, 23 Jan 2023 13:28:17 +0000 Subject: [PATCH] fix: IsWeekend not checking if DateTime (#5221) (no-changelog) --- .../workflow/src/Extensions/DateExtensions.ts | 12 +++++-- .../DateExtensions.test.ts | 31 ++++++++----------- .../test/ExpressionExtensions/Helpers.ts | 4 ++- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/workflow/src/Extensions/DateExtensions.ts b/packages/workflow/src/Extensions/DateExtensions.ts index 6c6d0c2b78..71571febed 100644 --- a/packages/workflow/src/Extensions/DateExtensions.ts +++ b/packages/workflow/src/Extensions/DateExtensions.ts @@ -139,7 +139,10 @@ function isBetween(date: Date | DateTime, extraArgs: unknown[]): boolean { return secondDate > date && date > firstDate; } -function isDst(date: Date): boolean { +function isDst(date: Date | DateTime): boolean { + if (isDateTime(date)) { + return date.isInDST; + } return DateTime.fromJSDate(date).isInDST; } @@ -154,11 +157,14 @@ function isInLast(date: Date | DateTime, extraArgs: unknown[]): boolean { return dateInThePast <= thisDate && thisDate <= DateTime.now(); } -function isWeekend(date: Date): boolean { +function isWeekend(date: Date | DateTime): boolean { enum DAYS { saturday = 6, sunday = 7, } + if (isDateTime(date)) { + return [DAYS.saturday, DAYS.sunday].includes(date.weekday); + } return [DAYS.saturday, DAYS.sunday].includes(DateTime.fromJSDate(date).weekday); } @@ -200,7 +206,7 @@ function toLocaleString(date: Date | DateTime, extraArgs: unknown[]): string { return DateTime.fromJSDate(date).toLocaleString(dateFormat, { locale }); } -function toTimeFromNow(date: Date): string { +function toTimeFromNow(date: Date | DateTime): string { let diffObj: Duration; if (isDateTime(date)) { diffObj = date.diffNow(); diff --git a/packages/workflow/test/ExpressionExtensions/DateExtensions.test.ts b/packages/workflow/test/ExpressionExtensions/DateExtensions.test.ts index bd51a5cdff..0dcaea24b0 100644 --- a/packages/workflow/test/ExpressionExtensions/DateExtensions.test.ts +++ b/packages/workflow/test/ExpressionExtensions/DateExtensions.test.ts @@ -2,16 +2,16 @@ * @jest-environment jsdom */ -import { extend } from '@/Extensions'; -import { dateExtensions } from '@/Extensions/DateExtensions'; -import { evaluate, getLocalISOString } from './Helpers'; +import { DateTime } from 'luxon'; +import { evaluate, getLocalISOString, TEST_TIMEZONE } from './Helpers'; describe('Data Transformation Functions', () => { describe('Date Data Transformation Functions', () => { test('.isWeekend() should work correctly on a date', () => { - expect(evaluate('={{DateTime.now().isWeekend()}}')).toEqual( - extend(new Date(), 'isWeekend', []), - ); + 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); }); test('.toTimeFromNow() should work correctly on a date', () => { @@ -20,29 +20,24 @@ describe('Data Transformation Functions', () => { }); test('.beginningOf("week") should work correctly on a date', () => { - expect(evaluate('={{(new Date).beginningOf("week")}}')).toEqual( - dateExtensions.functions.beginningOf(new Date(), ['week']), + expect(evaluate('={{ DateTime.local(2023, 1, 20).beginningOf("week") }}')).toEqual( + DateTime.local(2023, 1, 16, { zone: TEST_TIMEZONE }).toJSDate(), ); }); test('.endOfMonth() should work correctly on a date', () => { - expect(evaluate('={{ DateTime.now().endOfMonth() }}')).toEqual( - dateExtensions.functions.endOfMonth(new Date()), + expect(evaluate('={{ DateTime.local(2023, 1, 16).endOfMonth() }}')).toEqual( + DateTime.local(2023, 1, 31, 23, 59, 59, 999, { zone: TEST_TIMEZONE }).toJSDate(), ); }); test('.extract("day") should work correctly on a date', () => { - expect(evaluate('={{ DateTime.now().extract("day") }}')).toEqual( - dateExtensions.functions.extract(new Date(), ['day']), - ); + expect(evaluate('={{ DateTime.local(2023, 1, 20).extract("day") }}')).toEqual(20); }); test('.format("yyyy LLL dd") should work correctly on a date', () => { - expect(evaluate('={{ DateTime.now().format("yyyy LLL dd") }}')).toEqual( - dateExtensions.functions.format(new Date(), ['yyyy LLL dd']), - ); - expect(evaluate('={{ DateTime.now().format("yyyy LLL dd") }}')).not.toEqual( - dateExtensions.functions.format(new Date(), ["HH 'hours and' mm 'minutes'"]), + expect(evaluate('={{ DateTime.local(2023, 1, 16).format("yyyy LLL dd") }}')).toEqual( + '2023 Jan 16', ); }); diff --git a/packages/workflow/test/ExpressionExtensions/Helpers.ts b/packages/workflow/test/ExpressionExtensions/Helpers.ts index 1a36df85a6..fe6c3df783 100644 --- a/packages/workflow/test/ExpressionExtensions/Helpers.ts +++ b/packages/workflow/test/ExpressionExtensions/Helpers.ts @@ -1,6 +1,8 @@ import { Expression, INodeExecutionData, Workflow } from '../../src'; import * as Helpers from '../Helpers'; +export const TEST_TIMEZONE = 'America/New_York'; + export const nodeTypes = Helpers.NodeTypes(); export const workflow = new Workflow({ nodes: [ @@ -28,7 +30,7 @@ export const evaluate = (value: string, values?: INodeExecutionData[]) => 'node', values ?? [], 'manual', - 'America/New_York', + TEST_TIMEZONE, {}, );