forked from PluralFlux/PluralFlux
, updated tests for webhookHelper and removed error response when proxy is sent without content
This commit is contained in:
@@ -5,7 +5,8 @@ const {messageHelper} = require("../../src/helpers/messageHelper.js");
|
||||
jest.mock('../../src/helpers/messageHelper.js', () => {
|
||||
return {messageHelper: {
|
||||
parseProxyTags: jest.fn(),
|
||||
returnBuffer: jest.fn()
|
||||
returnBuffer: jest.fn(),
|
||||
returnBufferFromText: jest.fn(),
|
||||
}}
|
||||
})
|
||||
|
||||
@@ -14,16 +15,18 @@ const {enums} = require("../../src/enums");
|
||||
|
||||
describe('webhookHelper', () => {
|
||||
|
||||
const client = {};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
jest.clearAllMocks();
|
||||
})
|
||||
|
||||
describe(`sendMessageAsMember`, () => {
|
||||
const client = {};
|
||||
const content = "hi"
|
||||
const attachments = new Map();
|
||||
const attachments = {
|
||||
size: 0,
|
||||
first: () => {}
|
||||
}
|
||||
const message = {
|
||||
client,
|
||||
content: content,
|
||||
@@ -64,6 +67,7 @@ describe('webhookHelper', () => {
|
||||
expect(res).toBeUndefined();
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
@@ -94,6 +98,7 @@ describe('webhookHelper', () => {
|
||||
return webhookHelper.sendMessageAsMember(client, message).catch((res) => {
|
||||
// Assert
|
||||
expect(res).toEqual(new Error(enums.err.NOT_IN_SERVER));
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
@@ -112,11 +117,139 @@ describe('webhookHelper', () => {
|
||||
})
|
||||
})
|
||||
|
||||
test('if message matches member proxy and sent in a guild channel and no attachment, calls replace message', async() => {
|
||||
// Arrange
|
||||
message.guildId = '123';
|
||||
proxyMessage.hasAttachment = false;
|
||||
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
|
||||
jest.spyOn(webhookHelper, 'replaceMessage').mockResolvedValue();
|
||||
// Act
|
||||
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
|
||||
// Assert
|
||||
expect(message.reply).not.toHaveBeenCalled();
|
||||
expect(webhookHelper.replaceMessage).toHaveBeenCalledTimes(1);
|
||||
expect(webhookHelper.replaceMessage).toHaveBeenCalledWith(client, message, proxyMessage.message, proxyMessage.member);
|
||||
})
|
||||
})
|
||||
|
||||
test('if replace message throws error, throw same error', async() => {
|
||||
// Arrange
|
||||
message.guildId = '123';
|
||||
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
|
||||
jest.spyOn(webhookHelper, 'replaceMessage').mockImplementation(() => {throw new Error("error")});
|
||||
// Act
|
||||
return webhookHelper.sendMessageAsMember(client, message).catch((res) => {
|
||||
// Assert
|
||||
expect(message.reply).not.toHaveBeenCalled();
|
||||
expect(webhookHelper.replaceMessage).toHaveBeenCalledTimes(1);
|
||||
expect(webhookHelper.replaceMessage).toHaveBeenCalledWith(client, message, proxyMessage.message, proxyMessage.member);
|
||||
expect(res).toEqual(new Error('error'));
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe(`replaceMessage`, () => {
|
||||
const channelId = '123';
|
||||
const authorId = '456';
|
||||
const guildId = '789';
|
||||
const text = "hello";
|
||||
const client = {
|
||||
channels: {
|
||||
get: jest.fn().mockReturnValue(channelId)
|
||||
}
|
||||
}
|
||||
const member = {proxy: "--text", name: 'somePerson', displayname: "Some Person", propic: 'oya.png'};
|
||||
const attachments= {
|
||||
size: 1,
|
||||
first: () => {return channelId;}
|
||||
};
|
||||
const message = {
|
||||
client,
|
||||
channelId: channelId,
|
||||
content: text,
|
||||
attachments: attachments,
|
||||
author: {
|
||||
id: authorId
|
||||
},
|
||||
guild: {
|
||||
guildId: guildId
|
||||
},
|
||||
reply: jest.fn(),
|
||||
delete: jest.fn()
|
||||
}
|
||||
|
||||
const webhook = {
|
||||
send: async() => jest.fn().mockResolvedValue()
|
||||
}
|
||||
|
||||
test('does not call anything if text is 0 or message has no attachments', async() => {
|
||||
// Arrange
|
||||
const emptyText = ''
|
||||
const noAttachments = {
|
||||
size: 0,
|
||||
first: () => {}
|
||||
}
|
||||
message.attachments = noAttachments;
|
||||
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
|
||||
// Act
|
||||
return webhookHelper.replaceMessage(client, message, emptyText, member).then(() => {
|
||||
expect(webhookHelper.getOrCreateWebhook).not.toHaveBeenCalled();
|
||||
expect(message.delete).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
test('calls getOrCreateWebhook and message.delete with correct arguments if text >= 0', async() => {
|
||||
// Arrange
|
||||
message.attachments = {
|
||||
size: 0,
|
||||
first: () => {
|
||||
}
|
||||
};
|
||||
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
|
||||
// Act
|
||||
return webhookHelper.replaceMessage(client, message, text, member).then((res) => {
|
||||
// Assert
|
||||
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledTimes(1);
|
||||
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledWith(client, channelId);
|
||||
expect(message.delete).toHaveBeenCalledTimes(1);
|
||||
expect(message.delete).toHaveBeenCalledWith();
|
||||
})
|
||||
})
|
||||
|
||||
test('calls getOrCreateWebhook and message.delete with correct arguments if attachments exist', async() => {
|
||||
// Arrange
|
||||
const emptyText = ''
|
||||
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
|
||||
// Act
|
||||
return webhookHelper.replaceMessage(client, message, emptyText, member).then((res) => {
|
||||
// Assert
|
||||
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledTimes(1);
|
||||
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledWith(client, channelId);
|
||||
expect(message.delete).toHaveBeenCalledTimes(1);
|
||||
expect(message.delete).toHaveBeenCalledWith();
|
||||
})
|
||||
})
|
||||
|
||||
test('calls returnBufferFromText and console error if webhook.send returns error', async() => {
|
||||
// Arrange
|
||||
const file = Buffer.from(text, 'utf-8');
|
||||
const returnedBuffer = {text: text, file: file};
|
||||
const expected2ndSend = {content: returnedBuffer.text, username: member.displayname, avatar_url: member.propic, files: [{name: 'text.txt', data: returnedBuffer.file}]};
|
||||
jest.mock('console', () => ({error: jest.fn()}));
|
||||
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
|
||||
webhook.send = jest.fn().mockImplementationOnce(async() => {throw new Error('error')});
|
||||
messageHelper.returnBufferFromText = jest.fn().mockResolvedValue(returnedBuffer);
|
||||
// Act
|
||||
return webhookHelper.replaceMessage(client, message, text, member).catch((res) => {
|
||||
// Assert
|
||||
expect(messageHelper.returnBufferFromText).toHaveBeenCalledTimes(1);
|
||||
expect(messageHelper.returnBufferFromText).toHaveBeenCalledWith(text);
|
||||
expect(webhook.send).toHaveBeenCalledTimes(2);
|
||||
expect(webhook.send).toHaveBeenNthCalledWith(2, expected2ndSend);
|
||||
expect(console.error).toHaveBeenCalledTimes(1);
|
||||
expect(console.error).toHaveBeenCalledWith(new Error('error'));
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe(`getOrCreateWebhook`, () => {
|
||||
|
||||
Reference in New Issue
Block a user