This commit is contained in:
Michael Kret 2025-01-03 08:10:16 +02:00
parent c2185c8feb
commit 2128c02c4d
2 changed files with 147 additions and 0 deletions

View file

@ -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<IExecuteFunctions>;
beforeEach(() => {
googleCalendar = new GoogleCalendar();
mockExecuteFunctions = mock<IExecuteFunctions>({
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<INode>({ 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');
});
});
});

View file

@ -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<IExecuteFunctions>;
beforeEach(() => {
googleCalendar = new GoogleCalendar();
mockExecuteFunctions = mock<IExecuteFunctions>({
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<INode>({ 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');
});
});
});