Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Discord Node): New sendAndWait operation #12894

Merged
merged 15 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import type { MockProxy } from 'jest-mock-extended';
import { mock } from 'jest-mock-extended';
import { SEND_AND_WAIT_OPERATION, type IExecuteFunctions } from 'n8n-workflow';

import { versionDescription } from '../../../../v2/actions/versionDescription';
import { DiscordV2 } from '../../../../v2/DiscordV2.node';
import * as transport from '../../../../v2/transport/discord.api';

jest.mock('../../../../v2/transport/discord.api', () => {
const originalModule = jest.requireActual('../../../../v2/transport/discord.api');
return {
...originalModule,
discordApiRequest: jest.fn(async function (method: string) {
if (method === 'POST') {
return {};
}
}),
};
});

describe('Test DiscordV2, message => sendAndWait', () => {
let discord: DiscordV2;
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;

beforeEach(() => {
discord = new DiscordV2(versionDescription);
mockExecuteFunctions = mock<IExecuteFunctions>();
mockExecuteFunctions.helpers = {
constructExecutionMetaData: jest.fn(() => []),
returnJsonArray: jest.fn(() => []),
} as any;
});

afterEach(() => {
jest.clearAllMocks();
});

it('should send message and put execution to wait', async () => {
const items = [{ json: { data: 'test' } }];
mockExecuteFunctions.getNodeParameter.mockImplementation((key: string) => {
if (key === 'operation') return SEND_AND_WAIT_OPERATION;
if (key === 'resource') return 'message';
if (key === 'authentication') return 'botToken';
if (key === 'message') return 'my message';
if (key === 'subject') return '';
if (key === 'approvalOptions.values') return {};
if (key === 'responseType') return 'approval';
if (key === 'sendTo') return 'channel';
if (key === 'channelId') return 'channelID';
if (key === 'options.limitWaitTime.values') return {};
});

mockExecuteFunctions.putExecutionToWait.mockImplementation();
mockExecuteFunctions.getInputData.mockReturnValue(items);
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');

mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('http://localhost/waiting-webhook');
mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('nodeID');

const result = await discord.execute.call(mockExecuteFunctions);

expect(result).toEqual([items]);
expect(transport.discordApiRequest).toHaveBeenCalledTimes(1);
expect(mockExecuteFunctions.putExecutionToWait).toHaveBeenCalledTimes(1);

expect(transport.discordApiRequest).toHaveBeenCalledWith(
'POST',
'/channels/channelID/messages',
{
components: [
{
components: [
{
label: 'Approve',
style: 5,
type: 2,
url: 'http://localhost/waiting-webhook/nodeID?approved=true',
},
],
type: 1,
},
],
embeds: [
{
color: 5814783,
description:
'my message\n\n_This message was sent automatically with _[n8n](https://n8n.io/?utm_source=n8n-internal&utm_medium=powered_by&utm_campaign=n8n-nodes-base.telegram_instanceId)',
},
],
},
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const description: INodeProperties[] = getSendAndWaitProperties(
undefined,
{
noButtonStyle: true,
defaultApproveLabel: '✓ Approve',
defaultDisapproveLabel: '✗ Decline',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be using this wrong -- when I tried to configure this on Discord, I don't get a Decline option
Screenshot 2025-01-29 at 16 31 59

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dana-gill
by default message send with one button option, you need to chose corresponding Approval option
image

},
).filter((p) => p.name !== 'subject');

Expand Down