mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
test(Email Trigger (IMAP) Node): Improve email imap testing (#13255)
Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import { type ImapSimple } from '@n8n/imap';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { returnJsonArray } from 'n8n-core';
|
||||
import { type IDataObject, type ITriggerFunctions } from 'n8n-workflow';
|
||||
|
||||
import { getNewEmails } from '../../v2/utils';
|
||||
|
||||
describe('Test IMap V2 utils', () => {
|
||||
afterEach(() => jest.resetAllMocks());
|
||||
|
||||
describe('getNewEmails', () => {
|
||||
const triggerFunctions = mock<ITriggerFunctions>({
|
||||
helpers: { returnJsonArray },
|
||||
});
|
||||
|
||||
const message = {
|
||||
attributes: {
|
||||
uuid: 1,
|
||||
struct: {},
|
||||
},
|
||||
parts: [
|
||||
{ which: '', body: 'Body content' },
|
||||
{ which: 'HEADER', body: 'h' },
|
||||
{ which: 'TEXT', body: 'txt' },
|
||||
],
|
||||
};
|
||||
|
||||
const staticData: IDataObject = {};
|
||||
const imapConnection = mock<ImapSimple>({
|
||||
search: jest.fn().mockReturnValue(Promise.resolve([message])),
|
||||
});
|
||||
const getText = jest.fn().mockReturnValue('text');
|
||||
const getAttachment = jest.fn().mockReturnValue(['attachment']);
|
||||
|
||||
it('should return new emails', async () => {
|
||||
const expectedResults = [
|
||||
{
|
||||
format: 'resolved',
|
||||
expected: {
|
||||
json: {
|
||||
attachments: undefined,
|
||||
headers: { '': 'Body content' },
|
||||
headerLines: undefined,
|
||||
html: false,
|
||||
},
|
||||
binary: undefined,
|
||||
},
|
||||
},
|
||||
{
|
||||
format: 'simple',
|
||||
expected: {
|
||||
json: {
|
||||
textHtml: 'text',
|
||||
textPlain: 'text',
|
||||
metadata: {
|
||||
'0': 'h',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
format: 'raw',
|
||||
expected: {
|
||||
json: { raw: 'txt' },
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
expectedResults.forEach(async (expectedResult) => {
|
||||
triggerFunctions.getNodeParameter
|
||||
.calledWith('format')
|
||||
.mockReturnValue(expectedResult.format);
|
||||
triggerFunctions.getNodeParameter
|
||||
.calledWith('dataPropertyAttachmentsPrefixName')
|
||||
.mockReturnValue('resolved');
|
||||
|
||||
const result = getNewEmails.call(
|
||||
triggerFunctions,
|
||||
imapConnection,
|
||||
[],
|
||||
staticData,
|
||||
'',
|
||||
getText,
|
||||
getAttachment,
|
||||
);
|
||||
|
||||
await expect(result).resolves.toEqual([expectedResult.expected]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user