diff --git a/packages/nodes-base/nodes/Google/Calendar/test/node/calendar.availability.test.ts b/packages/nodes-base/nodes/Google/Calendar/test/node/calendar.availability.test.ts new file mode 100644 index 0000000000..8e75ef2c7a --- /dev/null +++ b/packages/nodes-base/nodes/Google/Calendar/test/node/calendar.availability.test.ts @@ -0,0 +1,74 @@ +import type { MockProxy } from 'jest-mock-extended'; +import { mock } from 'jest-mock-extended'; +import type { INode, IExecuteFunctions } from 'n8n-workflow'; + +import * as genericFunctions from '../../GenericFunctions'; +import { GoogleCalendar } from '../../GoogleCalendar.node'; + +let googleApiRequestArgs: any[] = []; +const CALENDAR_ID = 'myCalendar'; + +jest.mock('../../GenericFunctions', () => ({ + getTimezones: jest.fn(), + googleApiRequest: jest.fn(async (...args: any[]) => { + googleApiRequestArgs = args; + return { calendars: { [CALENDAR_ID]: { busy: [] } } }; + }), + googleApiRequestAllItems: jest.fn(), + addTimezoneToDate: jest.fn(), + addNextOccurrence: jest.fn(), + encodeURIComponentOnce: jest.fn(), +})); + +describe('RespondToWebhook Node', () => { + let googleCalendar: GoogleCalendar; + let mockExecuteFunctions: MockProxy; + + beforeEach(() => { + googleCalendar = new GoogleCalendar(); + mockExecuteFunctions = mock({ + getInputData: jest.fn(), + getNode: jest.fn(), + getNodeParameter: jest.fn(), + getTimezone: jest.fn(), + helpers: { + constructExecutionMetaData: jest.fn().mockReturnValue([]), + }, + }); + }); + + afterEach(() => { + googleApiRequestArgs = []; + jest.clearAllMocks(); + }); + + describe('Google Calendar > Calendar > Availability', () => { + it('should not have invalid timeMin and timeMax date', async () => { + //pre loop setup + mockExecuteFunctions.getInputData.mockReturnValue([{ json: {} }]); + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('calendar'); + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('availability'); + mockExecuteFunctions.getTimezone.mockReturnValueOnce('Europe/Berlin'); + mockExecuteFunctions.getNode.mockReturnValue(mock({ typeVersion: 1.1 })); + //operation + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(CALENDAR_ID); // calendar + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(''); // timeMin, not set + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(''); // timeMax, not set + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({}); // options + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(''); // options.timezone, default to timezone + + await googleCalendar.execute.call(mockExecuteFunctions); + + expect(genericFunctions.googleApiRequest).toHaveBeenCalledTimes(1); + + const body = googleApiRequestArgs[2] as { + timeMin: string; + timeMax: string; + }; + + expect(body).toBeDefined(); + expect(body.timeMin).not.toEqual('Invalid Date'); + expect(body.timeMax).not.toEqual('Invalid Date'); + }); + }); +}); diff --git a/packages/nodes-base/nodes/Google/Calendar/test/node/event.create.test.ts b/packages/nodes-base/nodes/Google/Calendar/test/node/event.create.test.ts new file mode 100644 index 0000000000..4641e75182 --- /dev/null +++ b/packages/nodes-base/nodes/Google/Calendar/test/node/event.create.test.ts @@ -0,0 +1,73 @@ +import type { MockProxy } from 'jest-mock-extended'; +import { mock } from 'jest-mock-extended'; +import type { INode, IExecuteFunctions } from 'n8n-workflow'; + +import * as genericFunctions from '../../GenericFunctions'; +import { GoogleCalendar } from '../../GoogleCalendar.node'; + +let googleApiRequestArgs: any[] = []; + +jest.mock('../../GenericFunctions', () => ({ + getTimezones: jest.fn(), + googleApiRequest: jest.fn(async (...args: any[]) => { + googleApiRequestArgs = args; + return {}; + }), + googleApiRequestAllItems: jest.fn(), + addTimezoneToDate: jest.fn(), + addNextOccurrence: jest.fn(), + encodeURIComponentOnce: jest.fn(), +})); + +describe('RespondToWebhook Node', () => { + let googleCalendar: GoogleCalendar; + let mockExecuteFunctions: MockProxy; + + beforeEach(() => { + googleCalendar = new GoogleCalendar(); + mockExecuteFunctions = mock({ + getInputData: jest.fn(), + getNode: jest.fn(), + getNodeParameter: jest.fn(), + getTimezone: jest.fn(), + helpers: { + constructExecutionMetaData: jest.fn().mockReturnValue([]), + }, + }); + }); + + afterEach(() => { + googleApiRequestArgs = []; + jest.clearAllMocks(); + }); + + describe('Google Calendar > Event > Create', () => { + it('should not have invalid start and end date', async () => { + //pre loop setup + mockExecuteFunctions.getInputData.mockReturnValue([{ json: {} }]); + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('event'); + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('create'); + mockExecuteFunctions.getTimezone.mockReturnValueOnce('Europe/Berlin'); + mockExecuteFunctions.getNode.mockReturnValue(mock({ typeVersion: 1.1 })); + //operation + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('myCalendar'); + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(''); // start, not set + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(''); // end, not set + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(true); // useDefaultReminders + mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({}); // additionalFields + + await googleCalendar.execute.call(mockExecuteFunctions); + + expect(genericFunctions.googleApiRequest).toHaveBeenCalledTimes(1); + + const body = googleApiRequestArgs[2] as { + start: { dateTime: string }; + end: { dateTime: string }; + }; + + expect(body).toBeDefined(); + expect(body.start.dateTime).not.toEqual('Invalid Date'); + expect(body.end.dateTime).not.toEqual('Invalid Date'); + }); + }); +});