mirror of
https://github.com/pieartsy/PluralFlux.git
synced 2026-04-16 17:45:28 +10:00
refactor: Removing then/catch from async/await calls (#22)
* refactored async/await for import helper to not also use then/catch * added enum * refactor webhookHelper and tests to not use then/catch * changed docstring * refactoring bot and tests to not use then/catch * refactoring commands.js and tests to not use then/catch * refactoring memberHelper.js and tests to not use then/catch * removing then/catch from messageHelper.test.js * fixed set up for commands tests * edited bot to have top level main function * one more test in commands.js, and removed console.error * fixed typo in webhookHelper * forgot to switch over some tests in bot.test and commands.test * removed console.log from import helper * put console.error in commands * converted utils.js to not use then/catch * tested utils checkImageFormatValidity * removed jest-fetch-mock since it turns out I was just manually mocking it anyway * refactored database to not use then/catch * added dash to commands.js and test to pass * added the remaining webhook tests * changed utils to check for 10MB size not 1MB * removed unnecessary try/catch from utils * Simplify getWebhook to use .find() instead of foreach logic * make memberCommand exit when error occurs with parseMemberCommand * changed commands.js to not have user interaction within the catch * updated console.error message in database.js * made importHelper mock throw error instead of "resolve" error * replaced "pk;" with "pf;" in test * Got rid of unnecessary check for empty message from user (Fluxer doesn't allow this to happen) Removed export of token * getAllMembersInfo checks for fields.length * added default case to memberCommandHandler to throw error if command is not recognized * reversed check for valid proxy (was returning valid if the proxy existed and invalid if it didn't) * pushes e.message instead of full error object to errors array in importHelper * adjusted tests to properly use mockRejectedValue for async rejections * changed getAllMembersInfo map to say `index` not `name` as it actually gets the index of a member and then the member object * adjusted importHelper to properly test throwing of aggregate error * revamped setting of expiration warning (moved to utils and changed logic, wrote tests) --------- Co-authored-by: Aster Fialla <asterfialla@gmail.com>
This commit is contained in:
@@ -14,10 +14,44 @@ const {webhookHelper} = require("../../src/helpers/webhookHelper.js");
|
||||
const {enums} = require("../../src/enums");
|
||||
|
||||
describe('webhookHelper', () => {
|
||||
const channelId = '123';
|
||||
const authorId = '456';
|
||||
const guildId = '789';
|
||||
const text = "hello";
|
||||
let client, member, attachments, message, webhook;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
jest.clearAllMocks();
|
||||
client = {
|
||||
channels: {
|
||||
get: jest.fn().mockReturnValue(channelId)
|
||||
}
|
||||
}
|
||||
member = {proxy: "--text", name: 'somePerson', displayname: "Some Person", propic: 'oya.png'};
|
||||
attachments = {
|
||||
size: 1,
|
||||
first: () => {return channelId;}
|
||||
};
|
||||
|
||||
message = {
|
||||
client,
|
||||
channelId: channelId,
|
||||
content: text,
|
||||
attachments: attachments,
|
||||
author: {
|
||||
id: authorId
|
||||
},
|
||||
guild: {
|
||||
guildId: guildId
|
||||
},
|
||||
reply: jest.fn().mockResolvedValue(),
|
||||
delete: jest.fn().mockResolvedValue()
|
||||
}
|
||||
|
||||
webhook = {
|
||||
send: async() => jest.fn().mockResolvedValue()
|
||||
}
|
||||
})
|
||||
|
||||
describe(`sendMessageAsMember`, () => {
|
||||
@@ -34,9 +68,7 @@ describe('webhookHelper', () => {
|
||||
author: {
|
||||
id: '123'
|
||||
},
|
||||
guild: {
|
||||
guildId: '123'
|
||||
},
|
||||
guildId: '123',
|
||||
reply: jest.fn()
|
||||
}
|
||||
const member = {proxy: "--text", name: 'somePerson', displayname: "Some Person"};
|
||||
@@ -50,25 +82,24 @@ describe('webhookHelper', () => {
|
||||
// Arrange
|
||||
messageHelper.parseProxyTags.mockResolvedValue({});
|
||||
// Act
|
||||
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
|
||||
expect(res).toBeUndefined();
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
const res = await webhookHelper.sendMessageAsMember(client, message)
|
||||
// Assert
|
||||
expect(res).toBeUndefined();
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
|
||||
test('calls parseProxyTags and returns if proxyMatch is undefined', async() => {
|
||||
// Arrange
|
||||
messageHelper.parseProxyTags.mockResolvedValue(undefined);
|
||||
// Act
|
||||
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
|
||||
// Assert
|
||||
expect(res).toBeUndefined();
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
const res = await webhookHelper.sendMessageAsMember(client, message)
|
||||
// Assert
|
||||
expect(res).toBeUndefined();
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
|
||||
test('calls parseProxyTags with attachmentUrl', async() => {
|
||||
@@ -79,27 +110,22 @@ describe('webhookHelper', () => {
|
||||
return {url: 'oya.png'}
|
||||
}
|
||||
}
|
||||
// message.attachments.set('attachment', {url: 'oya.png'})
|
||||
// message.attachments.set('first', () => {return {url: 'oya.png'}})
|
||||
messageHelper.parseProxyTags.mockResolvedValue(undefined);
|
||||
// Act
|
||||
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
|
||||
// Assert
|
||||
expect(res).toBeUndefined();
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, 'oya.png');
|
||||
})
|
||||
const res = await webhookHelper.sendMessageAsMember(client, message)
|
||||
// Assert
|
||||
expect(res).toBeUndefined();
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, 'oya.png');
|
||||
})
|
||||
|
||||
test('if message matches member proxy but is not sent from a guild, throw an error', async() => {
|
||||
// Arrange
|
||||
message.guildId = null;
|
||||
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
|
||||
// Act
|
||||
return webhookHelper.sendMessageAsMember(client, message).catch((res) => {
|
||||
// Assert
|
||||
expect(res).toEqual(new Error(enums.err.NOT_IN_SERVER));
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
// Act and Assert
|
||||
await expect(webhookHelper.sendMessageAsMember(client, message)).rejects.toThrow(enums.err.NOT_IN_SERVER);
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
|
||||
test('if message matches member proxy and sent in a guild and has an attachment, reply to message with ping', async() => {
|
||||
@@ -109,12 +135,11 @@ describe('webhookHelper', () => {
|
||||
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
|
||||
const expected = `${enums.misc.ATTACHMENT_SENT_BY} ${proxyMessage.member.displayname}`
|
||||
// Act
|
||||
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
|
||||
// Assert
|
||||
expect(message.reply).toHaveBeenCalledTimes(1);
|
||||
expect(message.reply).toHaveBeenCalledWith(expected);
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
await webhookHelper.sendMessageAsMember(client, message)
|
||||
// Assert
|
||||
expect(message.reply).toHaveBeenCalledTimes(1);
|
||||
expect(message.reply).toHaveBeenCalledWith(expected);
|
||||
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||
})
|
||||
|
||||
test('if message matches member proxy and sent in a guild channel and no attachment, calls replace message', async() => {
|
||||
@@ -124,63 +149,26 @@ describe('webhookHelper', () => {
|
||||
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);
|
||||
})
|
||||
await webhookHelper.sendMessageAsMember(client, message);
|
||||
// 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() => {
|
||||
test('if replace message throws error, throw same error and does not call message.reply', async () => {
|
||||
// Arrange
|
||||
message.guildId = '123';
|
||||
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
|
||||
jest.spyOn(webhookHelper, 'replaceMessage').mockImplementation(() => {throw new Error("error")});
|
||||
jest.spyOn(webhookHelper, 'replaceMessage').mockRejectedValue(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'));
|
||||
})
|
||||
await expect(webhookHelper.sendMessageAsMember(client, message)).rejects.toThrow("error");
|
||||
// Assert
|
||||
expect(message.reply).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
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
|
||||
@@ -192,13 +180,12 @@ describe('webhookHelper', () => {
|
||||
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();
|
||||
})
|
||||
await webhookHelper.replaceMessage(client, message, emptyText, member)
|
||||
expect(webhookHelper.getOrCreateWebhook).not.toHaveBeenCalled();
|
||||
expect(message.delete).not.toHaveBeenCalled();
|
||||
})
|
||||
|
||||
test('calls getOrCreateWebhook and message.delete with correct arguments if text >= 0', async() => {
|
||||
test('calls getOrCreateWebhook and message.delete with correct arguments if text > 0 & < 2000', async() => {
|
||||
// Arrange
|
||||
message.attachments = {
|
||||
size: 0,
|
||||
@@ -207,58 +194,108 @@ describe('webhookHelper', () => {
|
||||
};
|
||||
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();
|
||||
})
|
||||
await webhookHelper.replaceMessage(client, message, text, member);
|
||||
// Assert
|
||||
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledTimes(1);
|
||||
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledWith(client, channelId);
|
||||
expect(message.delete).toHaveBeenCalledTimes(1);
|
||||
expect(message.delete).toHaveBeenCalledWith();
|
||||
})
|
||||
|
||||
// TODO: flaky for some reason
|
||||
test('calls getOrCreateWebhook and message.delete with correct arguments if attachments exist', async() => {
|
||||
// TODO: Flaky for some reason. Skipping until attachments are implemented
|
||||
test.skip('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();
|
||||
})
|
||||
await webhookHelper.replaceMessage(client, message, emptyText, member);
|
||||
// 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() => {
|
||||
test('calls returnBufferFromText if text is more than 2000 characters', async() => {
|
||||
// Arrange
|
||||
const text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbb";
|
||||
message.content = text;
|
||||
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()}));
|
||||
const returnedBuffer = {text: 'bbbb', file: file};
|
||||
const expected = {content: returnedBuffer.text, username: member.displayname, avatar_url: member.propic, files: [{name: 'text.txt', data: returnedBuffer.file}]};
|
||||
|
||||
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
|
||||
webhook.send = jest.fn().mockImplementationOnce(async() => {throw new Error('error')});
|
||||
messageHelper.returnBufferFromText = jest.fn().mockResolvedValue(returnedBuffer);
|
||||
webhook.send = jest.fn();
|
||||
messageHelper.returnBufferFromText = jest.fn().mockReturnValue(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'));
|
||||
})
|
||||
await webhookHelper.replaceMessage(client, message, text, member);
|
||||
// Assert
|
||||
expect(messageHelper.returnBufferFromText).toHaveBeenCalledTimes(1);
|
||||
expect(messageHelper.returnBufferFromText).toHaveBeenCalledWith(text);
|
||||
expect(webhook.send).toHaveBeenCalledTimes(1);
|
||||
expect(webhook.send).toHaveBeenCalledWith(expected);
|
||||
})
|
||||
})
|
||||
|
||||
describe(`getOrCreateWebhook`, () => {
|
||||
let channel;
|
||||
|
||||
beforeEach(async () => {
|
||||
channel = {
|
||||
createWebhook: jest.fn().mockResolvedValue()
|
||||
}
|
||||
jest.spyOn(webhookHelper, 'getWebhook').mockResolvedValue(webhook);
|
||||
})
|
||||
|
||||
test('throws error if channel does not allow webhooks', async() => {
|
||||
channel.createWebhook = false;
|
||||
|
||||
await expect(webhookHelper.getOrCreateWebhook(client, channel)).rejects.toThrow(enums.err.NO_WEBHOOKS_ALLOWED);
|
||||
})
|
||||
|
||||
test('calls getWebhook if channel allows webhooks and returns webhook', async() => {
|
||||
const res = await webhookHelper.getOrCreateWebhook(client, channel);
|
||||
expect(webhookHelper.getWebhook).toHaveBeenCalledTimes(1);
|
||||
expect(webhookHelper.getWebhook).toHaveBeenCalledWith(client, channel);
|
||||
expect(res).toEqual(webhook);
|
||||
})
|
||||
|
||||
test("calls createWebhook if getWebhook doesn't return webhook", async() => {
|
||||
jest.spyOn(webhookHelper, 'getWebhook').mockResolvedValue();
|
||||
await webhookHelper.getOrCreateWebhook(client, channel);
|
||||
expect(channel.createWebhook).toHaveBeenCalledTimes(1);
|
||||
expect(channel.createWebhook).toHaveBeenCalledWith({name: 'PluralFlux Proxy Webhook'});
|
||||
})
|
||||
})
|
||||
|
||||
describe(`getWebhook`, () => {
|
||||
let webhook1, webhook2, channel;
|
||||
beforeEach(() => {
|
||||
webhook1 = {name: 'PluralFlux Proxy Webhook'};
|
||||
webhook2 = {name: 'other webhook'};
|
||||
channel = {
|
||||
fetchWebhooks: jest.fn().mockResolvedValue([webhook1, webhook2])
|
||||
}
|
||||
})
|
||||
|
||||
test('calls fetchWebhooks and returns correct webhook', async() => {
|
||||
// Act
|
||||
const res = await webhookHelper.getWebhook(client, channel);
|
||||
// Assert
|
||||
expect(res).toEqual(webhook1);
|
||||
expect(channel.fetchWebhooks).toHaveBeenCalledTimes(1);
|
||||
expect(channel.fetchWebhooks).toHaveBeenCalledWith();
|
||||
})
|
||||
|
||||
test('if fetchWebhooks returns no webhooks, return', async() => {
|
||||
// Arrange
|
||||
channel.fetchWebhooks = jest.fn().mockResolvedValue([]);
|
||||
// Act
|
||||
const res = await webhookHelper.getWebhook(client, channel);
|
||||
// Assert
|
||||
expect(res).toBeUndefined();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user