forked from PluralFlux/PluralFlux
feat: add tests and other such features (#3)
* converted import syntax to ES modules
removed unused methods
* got test sort of working (jest set up is not crashing but also not mocking correctly)
* adjusted beforeeach/beforeall so more pass
* more correct test setup
* converted import syntax to commonJS
removed unused methods
* got test sort of working (jest set up is not crashing but also not mocking correctly)
* adjusted beforeeach/beforeall so more pass
* more correct test setup
* more correct dockerfile and compose.yaml
* Revert "converted import syntax to commonJS"
This reverts commit 5ab0d62b
* updated jest to sort of work with es6
* separating out enum return from method return
* mostly working except for the weirdest error
* nevermind it wasn't actually working, gonna move on for now
* added babel to convert es modules to cjs
* finally figured out issue with tests (referencing the method directly in the test.each calls the real method not the mock in beforeEach())
* setup fixed more
* added error handling parseMemberCommand test
* renamed db to database
more tests and fixing logic for memberhelper
* upgraded fluxer.js
* moved import to helpers folder
* moved import to helpers folder
* more tests for member helper
* think i fixed weird error with webhook sending error when a user has no members
* simplified sendMessageAsAttachment
* added return to addFullMember so that addNewMember can reference it properly in strings
* test setup for messagehelper and webhookhelper
* readded line i shouldn't have removed in sendMessageAsMember
* fixed test and logic
* added test for memberHelper
* updated sendMessageAsAttachment to returnBufferFromText and updated commands/webhookHelper accordingly
* added tests for parseProxyTags and updated logic
* added "return" so tests dont terminate on failure and deleted env.jest
* finished tests for messageHelper!
* more cases for messageHelper just in case
* updating docstring for messageHelper parseProxyTags
* more tests for webhookhelper
* deleted extra file added during merge
* removed confusing brackets from enum docs
* finally mocking correctly
* adding more cases to messageHelper tests
* updating enums
* removed error response when proxy is sent without content
* , updated tests for webhookHelper and removed error response when proxy is sent without content
* added debounce to count guilds properly
* added todo note
* added tests for updateDisplayName
* edited help message trigger for updatePropic
* update message helper test to include space case
* update bot to suppress errors from API
* fixed bug for import not sending help text, added help text if you type a unrecognized command
* updated to be enum
* updated member helper and tests
* edit enums, tweak import content command
* removed unnecessary await and console.log
* made it easier to make a member
* added nicer error listing to importHelper
* updated documentation
* Merge branch 'main' of https://github.com/pieartsy/PluralFlux into add-tests
---------
Co-authored-by: Aster Fialla <asterfialla@gmail.com>
This commit is contained in:
510
tests/helpers/memberHelper.test.js
Normal file
510
tests/helpers/memberHelper.test.js
Normal file
@@ -0,0 +1,510 @@
|
||||
const {EmbedBuilder} = require("@fluxerjs/core");
|
||||
const {database} = require('../../src/database.js');
|
||||
const {enums} = require('../../src/enums.js');
|
||||
const {EmptyResultError, Op} = require('sequelize');
|
||||
const {memberHelper} = require("../../src/helpers/memberHelper.js");
|
||||
|
||||
jest.mock('@fluxerjs/core', () => jest.fn());
|
||||
jest.mock('../../src/database.js', () => {
|
||||
return {
|
||||
database: {
|
||||
members: {
|
||||
create: jest.fn().mockResolvedValue(),
|
||||
update: jest.fn().mockResolvedValue(),
|
||||
destroy: jest.fn().mockResolvedValue(),
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
jest.mock('sequelize', () => jest.fn());
|
||||
|
||||
describe('MemberHelper', () => {
|
||||
const authorId = "0001";
|
||||
const authorFull = "author#0001";
|
||||
const attachmentUrl = "../oya.png";
|
||||
const attachmentExpiration = new Date('2026-01-01T00.00.00.0000Z')
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
jest.clearAllMocks();
|
||||
})
|
||||
|
||||
describe('parseMemberCommand', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
jest.spyOn(memberHelper, 'getMemberInfo').mockResolvedValue("member info");
|
||||
jest.spyOn(memberHelper, 'addNewMember').mockResolvedValue("new member");
|
||||
jest.spyOn(memberHelper, 'removeMember').mockResolvedValue("remove member");
|
||||
jest.spyOn(memberHelper, 'getAllMembersInfo').mockResolvedValue("all member info");
|
||||
jest.spyOn(memberHelper, 'updateName').mockResolvedValue("update name");
|
||||
jest.spyOn(memberHelper, 'updateDisplayName').mockResolvedValue("update display name");
|
||||
jest.spyOn(memberHelper, 'updateProxy').mockResolvedValue("update proxy");
|
||||
jest.spyOn(memberHelper, 'updatePropic').mockResolvedValue("update propic");
|
||||
jest.spyOn(memberHelper, 'getProxyByMember').mockResolvedValue("get proxy");
|
||||
});
|
||||
|
||||
test.each([
|
||||
[['remove'], 'remove member', 'removeMember', ['remove']],
|
||||
[['list'], 'all member info', 'getAllMembersInfo', authorFull],
|
||||
[['somePerson', 'name'], 'update name', 'updateName', ['somePerson', 'name']],
|
||||
[['somePerson', 'displayname'], 'update display name', 'updateDisplayName', ['somePerson', 'displayname']],
|
||||
[['somePerson', 'proxy'], 'get proxy', 'getProxyByMember', 'somePerson'],
|
||||
[['somePerson', 'proxy', 'test'], 'update proxy', 'updateProxy', ['somePerson', 'proxy', 'test']],
|
||||
[['somePerson'], 'member info', 'getMemberInfo', 'somePerson'],
|
||||
])('%s calls %s and returns correct values', async (args, expectedResult, method, passedIn) => {
|
||||
// Act
|
||||
return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(expectedResult);
|
||||
expect(memberHelper[method]).toHaveBeenCalledTimes(1);
|
||||
expect(memberHelper[method]).toHaveBeenCalledWith(authorId, passedIn)
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
test.each([
|
||||
[['new'], attachmentUrl],
|
||||
[['new'], null,]
|
||||
])('%s returns correct values and calls addNewMember', (args, attachmentUrl) => {
|
||||
// Act
|
||||
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl).then((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual("new member");
|
||||
expect(memberHelper.addNewMember).toHaveBeenCalledTimes(1);
|
||||
expect(memberHelper.addNewMember).toHaveBeenCalledWith(authorId, args, attachmentUrl);
|
||||
});
|
||||
})
|
||||
|
||||
test('["somePerson", "propic"] returns correct values and updatePropic', () => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'propic'];
|
||||
// Act
|
||||
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl, attachmentExpiration).then((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual("update propic");
|
||||
expect(memberHelper['updatePropic']).toHaveBeenCalledTimes(1);
|
||||
expect(memberHelper['updatePropic']).toHaveBeenCalledWith(authorId, args, attachmentUrl, attachmentExpiration)
|
||||
});
|
||||
})
|
||||
|
||||
test.each([
|
||||
[['--help'], enums.help.MEMBER],
|
||||
[['name'], enums.help.NAME],
|
||||
[['displayname'], enums.help.DISPLAY_NAME],
|
||||
[['proxy'], enums.help.PROXY],
|
||||
[['propic'], enums.help.PROPIC],
|
||||
[['list', '--help'], enums.help.LIST],
|
||||
[[''], enums.help.MEMBER],
|
||||
])('%s returns correct enums', async (args, expectedResult) => {
|
||||
// Arrange
|
||||
const authorId = '1';
|
||||
const authorFull = 'somePerson#0001';
|
||||
// Act
|
||||
return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => {
|
||||
|
||||
expect(result).toEqual(expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe('errors', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
jest.clearAllMocks();
|
||||
jest.spyOn(memberHelper, 'getMemberInfo').mockImplementation(() => { throw new Error('member info error')});
|
||||
jest.spyOn(memberHelper, 'addNewMember').mockImplementation(() => { throw new Error('new member error')});
|
||||
jest.spyOn(memberHelper, 'removeMember').mockImplementation(() => { throw new Error('remove member error')});
|
||||
jest.spyOn(memberHelper, 'getAllMembersInfo').mockImplementation(() => { throw new Error('all member info error')});
|
||||
jest.spyOn(memberHelper, 'updateName').mockImplementation(() => { throw new Error('update name error')});
|
||||
jest.spyOn(memberHelper, 'updateDisplayName').mockImplementation(() => { throw new Error('update display name error')});
|
||||
jest.spyOn(memberHelper, 'updateProxy').mockImplementation(() => { throw new Error('update proxy error')});
|
||||
jest.spyOn(memberHelper, 'updatePropic').mockImplementation(() => { throw new Error('update propic error')});
|
||||
jest.spyOn(memberHelper, 'getProxyByMember').mockImplementation(() => { throw new Error('get proxy error')});
|
||||
})
|
||||
test.each([
|
||||
[['remove'], 'remove member error', 'removeMember', ['remove']],
|
||||
[['list'], 'all member info error', 'getAllMembersInfo', authorFull],
|
||||
[['somePerson', 'name'], 'update name error', 'updateName', ['somePerson', 'name']],
|
||||
[['somePerson', 'displayname'], 'update display name error', 'updateDisplayName', ['somePerson', 'displayname']],
|
||||
[['somePerson', 'proxy'], 'get proxy error', 'getProxyByMember', 'somePerson'],
|
||||
[['somePerson', 'proxy', 'test'], 'update proxy error', 'updateProxy', ['somePerson', 'proxy', 'test']],
|
||||
[['somePerson'], 'member info error', 'getMemberInfo', 'somePerson'],
|
||||
])('%s calls methods and throws correct values', async (args, expectedError, method, passedIn) => {
|
||||
// Act
|
||||
return memberHelper.parseMemberCommand(authorId, authorFull, args).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new Error(expectedError));
|
||||
expect(memberHelper[method]).toHaveBeenCalledTimes(1);
|
||||
expect(memberHelper[method]).toHaveBeenCalledWith(authorId, passedIn)
|
||||
});
|
||||
});
|
||||
|
||||
test.each([
|
||||
[['new'], attachmentUrl],
|
||||
[['new'], null,]
|
||||
])('%s throws correct error when addNewMember returns error', (args, attachmentUrl) => {
|
||||
// Act
|
||||
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new Error("new member error"));
|
||||
expect(memberHelper.addNewMember).toHaveBeenCalledTimes(1);
|
||||
expect(memberHelper.addNewMember).toHaveBeenCalledWith(authorId, args, attachmentUrl);
|
||||
});
|
||||
})
|
||||
|
||||
test('["somePerson", "propic"] throws correct error when updatePropic returns error', () => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'propic'];
|
||||
// Act
|
||||
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl, attachmentExpiration).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new Error("update propic error"));
|
||||
expect(memberHelper['updatePropic']).toHaveBeenCalledTimes(1);
|
||||
expect(memberHelper['updatePropic']).toHaveBeenCalledWith(authorId, args, attachmentUrl, attachmentExpiration)
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('addNewMember', () => {
|
||||
|
||||
test('returns help if --help passed in', async() => {
|
||||
// Arrange
|
||||
const args = ['new', '--help'];
|
||||
const expected = enums.help.NEW;
|
||||
//Act
|
||||
return memberHelper.addNewMember(authorId, args).then((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(expected);
|
||||
})
|
||||
})
|
||||
|
||||
test('calls getMemberInfo when successful and returns result', async () => {
|
||||
// Arrange
|
||||
const args = ['new', 'some person'];
|
||||
const memberObject = { name: args[1] }
|
||||
jest.spyOn(memberHelper, 'addFullMember').mockResolvedValue(memberObject);
|
||||
jest.spyOn(memberHelper, 'getMemberInfo').mockResolvedValue(memberObject);
|
||||
//Act
|
||||
return memberHelper.addNewMember(authorId, args).then((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(memberObject);
|
||||
expect(memberHelper.getMemberInfo).toHaveBeenCalledTimes(1);
|
||||
expect(memberHelper.getMemberInfo).toHaveBeenCalledWith(authorId, args[1]);
|
||||
})
|
||||
})
|
||||
|
||||
test('throws expected error when getMemberInfo throws error', async () => {
|
||||
// Arrange
|
||||
const args = ['new', 'some person'];
|
||||
const memberObject = { name: args[1] }
|
||||
jest.spyOn(memberHelper, 'addFullMember').mockResolvedValue(memberObject);
|
||||
jest.spyOn(memberHelper, 'getMemberInfo').mockImplementation(() => { throw new Error('getMemberInfo error') });
|
||||
//Act
|
||||
return memberHelper.addNewMember(authorId, args).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new Error('getMemberInfo error'));
|
||||
})
|
||||
})
|
||||
|
||||
test('throws expected error when addFullMember throws error', async () => {
|
||||
// Arrange
|
||||
const args = ['new', 'somePerson'];
|
||||
const expected = 'add full member error';
|
||||
jest.spyOn(memberHelper, 'addFullMember').mockImplementation(() => { throw new Error(expected)});
|
||||
|
||||
//Act
|
||||
return memberHelper.addNewMember(authorId, args).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new Error(expected));
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('updateName', () => {
|
||||
|
||||
test('sends help message when --help parameter passed in', async () => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'name', '--help'];
|
||||
|
||||
// Act
|
||||
return memberHelper.updateName(authorId, args).then((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(enums.help.NAME);
|
||||
})
|
||||
})
|
||||
|
||||
test('Sends string when no name', async () => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'name'];
|
||||
const expected = `The name for ${args[0]} is ${args[0]}, but you probably knew that!`;
|
||||
|
||||
// Act
|
||||
return memberHelper.updateName(authorId, args).then((result) => {
|
||||
expect(result).toEqual(expected);
|
||||
})
|
||||
})
|
||||
|
||||
test('throws error when name is empty', async () => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'name', " "];
|
||||
|
||||
// Act
|
||||
return memberHelper.updateName(authorId, args).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new RangeError("Name " + enums.err.NO_VALUE));
|
||||
})
|
||||
})
|
||||
|
||||
test('throws error when updateMemberField returns error', async () => {
|
||||
// Arrange
|
||||
const expected = 'update error';
|
||||
const args = ['somePerson', "name", "someNewPerson"];
|
||||
jest.spyOn(memberHelper, 'updateMemberField').mockImplementation(() => {
|
||||
throw new Error(expected)
|
||||
});
|
||||
// Act
|
||||
return memberHelper.updateName(authorId, args).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new Error(expected));
|
||||
})
|
||||
});
|
||||
|
||||
test('sends string when updateMemberField returns successfully', async () => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'name', 'someNewPerson'];
|
||||
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue("Updated");
|
||||
|
||||
// Act
|
||||
return memberHelper.updateName(authorId, args).then((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual("Updated");
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('updateDisplayName', () => {
|
||||
|
||||
test('sends help message when --help parameter passed in', async () => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'displayname', '--help'];
|
||||
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
|
||||
// Act
|
||||
return memberHelper.updateDisplayName(authorId, args).then((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(enums.help.DISPLAY_NAME);
|
||||
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
test('Sends string of current displayname when it exists and no displayname passed in', async () => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'displayname'];
|
||||
const displayname = "Some Person";
|
||||
const member = {
|
||||
displayname: displayname,
|
||||
}
|
||||
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(member);
|
||||
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
|
||||
// Act
|
||||
return memberHelper.updateDisplayName(authorId, args).then((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(`Display name for ${args[0]} is: "${member.displayname}".`);
|
||||
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
test('Sends error when no displayname passed in', async () => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'displayname'];
|
||||
const member = {}
|
||||
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(member);
|
||||
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
|
||||
// Act
|
||||
return memberHelper.updateDisplayName(authorId, args).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new Error(`Display name ${enums.err.NO_VALUE}`));
|
||||
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
test('Sends error when display name is too long', async () => {
|
||||
// Arrange
|
||||
const displayname = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
const args = ['somePerson', 'displayname', displayname];
|
||||
const member = {};
|
||||
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(member);
|
||||
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
|
||||
// Act
|
||||
return memberHelper.updateDisplayName(authorId, args).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new RangeError(enums.err.DISPLAY_NAME_TOO_LONG));
|
||||
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
test('Sends error when display name is blank', async () => {
|
||||
// Arrange
|
||||
const displayname = " ";
|
||||
const args = ['somePerson', 'displayname', displayname];
|
||||
const member = {};
|
||||
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(member);
|
||||
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
|
||||
// Act
|
||||
return memberHelper.updateDisplayName(authorId, args).catch((result) => {
|
||||
// Assert
|
||||
expect(result).toEqual(new Error(`Display name ${enums.err.NO_VALUE}`));
|
||||
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
test('call updateMemberField with correct arguments when displayname passed in correctly', async() => {
|
||||
// Arrange
|
||||
const args = ['somePerson', 'displayname', "Some Person"];
|
||||
const member = {};
|
||||
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue(member);
|
||||
// Act
|
||||
return memberHelper.updateDisplayName(authorId, args).then((result) => {
|
||||
// Assert
|
||||
expect(memberHelper.updateMemberField).toHaveBeenCalledWith(authorId, args);
|
||||
expect(memberHelper.updateMemberField).toHaveBeenCalledTimes(1);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('addFullMember', () => {
|
||||
const memberName = "somePerson";
|
||||
const displayName = "Some Person";
|
||||
const proxy = "--text";
|
||||
const propic = "oya.png";
|
||||
beforeEach(() => {
|
||||
database.members.create = jest.fn().mockResolvedValue();
|
||||
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue();
|
||||
})
|
||||
|
||||
test('calls getMemberByName', async() => {
|
||||
// Act
|
||||
return await memberHelper.addFullMember(authorId, memberName).then(() => {
|
||||
// Assert
|
||||
expect(memberHelper.getMemberByName).toHaveBeenCalledWith(authorId, memberName);
|
||||
expect(memberHelper.getMemberByName).toHaveBeenCalledTimes(1);
|
||||
})
|
||||
})
|
||||
|
||||
test('if getMemberByName returns member, throw error', async() => {
|
||||
memberHelper.getMemberByName.mockResolvedValue({name: memberName});
|
||||
// Act
|
||||
return await memberHelper.addFullMember(authorId, memberName).catch((e) => {
|
||||
// Assert
|
||||
expect(e).toEqual(new Error(`Can't add ${memberName}. ${enums.err.MEMBER_EXISTS}`))
|
||||
expect(database.members.create).not.toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
test('if displayname is over 32 characters, call database.member.create with null value', async() => {
|
||||
// Arrange
|
||||
const displayName = "Some person with a very very very long name that can't be processed";
|
||||
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: null, propic: null}
|
||||
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
|
||||
const expectedReturn = {member: expectedMemberArgs, errors: [`Tried to set displayname to \"${displayName}\". ${enums.err.DISPLAY_NAME_TOO_LONG}. ${enums.err.SET_TO_NULL}`]}
|
||||
|
||||
// Act
|
||||
return await memberHelper.addFullMember(authorId, memberName, displayName, null, null).then((res) => {
|
||||
// Assert
|
||||
expect(res).toEqual(expectedReturn);
|
||||
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
|
||||
expect(database.members.create).toHaveBeenCalledTimes(1);
|
||||
})
|
||||
})
|
||||
|
||||
test('if proxy, call checkIfProxyExists', async() => {
|
||||
// Arrange
|
||||
jest.spyOn(memberHelper, 'checkIfProxyExists').mockResolvedValue();
|
||||
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: proxy, propic: null}
|
||||
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
|
||||
const expectedReturn = {member: expectedMemberArgs, errors: []}
|
||||
|
||||
// Act
|
||||
return await memberHelper.addFullMember(authorId, memberName, null, proxy).then((res) => {
|
||||
// Assert
|
||||
expect(res).toEqual(expectedReturn);
|
||||
expect(memberHelper.checkIfProxyExists).toHaveBeenCalledWith(authorId, proxy);
|
||||
expect(memberHelper.checkIfProxyExists).toHaveBeenCalledTimes(1);
|
||||
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
|
||||
expect(database.members.create).toHaveBeenCalledTimes(1);
|
||||
})
|
||||
})
|
||||
|
||||
test('if checkProxyExists throws error, call database.member.create with null value', async() => {
|
||||
// Arrange
|
||||
jest.spyOn(memberHelper, 'checkIfProxyExists').mockImplementation(() => {throw new Error('error')});
|
||||
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: null, propic: null}
|
||||
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
|
||||
const expectedReturn = {member: expectedMemberArgs, errors: [`Tried to set proxy to \"${proxy}\". error. ${enums.err.SET_TO_NULL}`]}
|
||||
|
||||
// Act
|
||||
return await memberHelper.addFullMember(authorId, memberName, null, proxy, null).then((res) => {
|
||||
// Assert
|
||||
expect(res).toEqual(expectedReturn);
|
||||
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
|
||||
expect(database.members.create).toHaveBeenCalledTimes(1);
|
||||
})
|
||||
})
|
||||
|
||||
test('if propic, call checkImageFormatValidity', async() => {
|
||||
// Arrange
|
||||
jest.spyOn(memberHelper, 'checkImageFormatValidity').mockResolvedValue();
|
||||
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: null, propic: propic}
|
||||
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
|
||||
const expectedReturn = {member: expectedMemberArgs, errors: []}
|
||||
// Act
|
||||
return await memberHelper.addFullMember(authorId, memberName, null, null, propic).then((res) => {
|
||||
// Assert
|
||||
expect(res).toEqual(expectedReturn);
|
||||
expect(memberHelper.checkImageFormatValidity).toHaveBeenCalledWith(propic);
|
||||
expect(memberHelper.checkImageFormatValidity).toHaveBeenCalledTimes(1);
|
||||
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
|
||||
expect(database.members.create).toHaveBeenCalledTimes(1);
|
||||
})
|
||||
})
|
||||
|
||||
test('if checkImageFormatValidity throws error, call database.member.create with null value', async() => {
|
||||
// Arrange
|
||||
jest.spyOn(memberHelper, 'checkImageFormatValidity').mockImplementation(() => {throw new Error('error')});
|
||||
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: null, propic: null}
|
||||
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
|
||||
const expectedReturn = {member: expectedMemberArgs, errors: [`Tried to set profile picture to \"${propic}\". error. ${enums.err.SET_TO_NULL}`]}
|
||||
// Act
|
||||
return await memberHelper.addFullMember(authorId, memberName, null, null, propic).then((res) => {
|
||||
// Assert
|
||||
expect(res).toEqual(expectedReturn);
|
||||
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
|
||||
expect(database.members.create).toHaveBeenCalledTimes(1);
|
||||
})
|
||||
})
|
||||
|
||||
test('if all values are valid, call database.members.create', async() => {
|
||||
// Arrange
|
||||
jest.spyOn(memberHelper, 'checkIfProxyExists').mockResolvedValue();
|
||||
jest.spyOn(memberHelper, 'checkImageFormatValidity').mockResolvedValue();
|
||||
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: displayName, proxy: proxy, propic: propic}
|
||||
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
|
||||
const expectedReturn = {member: expectedMemberArgs, errors: []}
|
||||
// Act
|
||||
// Act
|
||||
return await memberHelper.addFullMember(authorId, memberName, displayName, proxy, propic).then((res) => {
|
||||
// Assert
|
||||
expect(res).toEqual(expectedReturn);
|
||||
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
|
||||
expect(database.members.create).toHaveBeenCalledTimes(1);
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
// restore the spy created with spyOn
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
})
|
||||
|
||||
128
tests/helpers/messageHelper.test.js
Normal file
128
tests/helpers/messageHelper.test.js
Normal file
@@ -0,0 +1,128 @@
|
||||
const env = require('dotenv');
|
||||
env.config();
|
||||
|
||||
const {memberHelper} = require("../../src/helpers/memberHelper.js");
|
||||
const {Message} = require("@fluxerjs/core");
|
||||
const {fs} = require('fs');
|
||||
const {enums} = require('../../src/enums');
|
||||
const {tmp, setGracefulCleanup} = require('tmp');
|
||||
|
||||
jest.mock('../../src/helpers/memberHelper.js', () => {
|
||||
return {memberHelper: {
|
||||
getMembersByAuthor: jest.fn()
|
||||
}}
|
||||
})
|
||||
|
||||
jest.mock('tmp');
|
||||
jest.mock('fs');
|
||||
jest.mock('@fluxerjs/core');
|
||||
|
||||
const {messageHelper} = require("../../src/helpers/messageHelper.js");
|
||||
|
||||
describe('messageHelper', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
jest.clearAllMocks();
|
||||
})
|
||||
|
||||
describe('parseCommandArgs', () => {
|
||||
test.each([
|
||||
['pk;member', ['']],
|
||||
['pk;member add somePerson "Some Person"', ['add', 'somePerson', 'Some Person']],
|
||||
['pk;member add \"Some Person\"', ['add', 'Some Person']],
|
||||
['pk;member add somePerson \'Some Person\'', ['add', 'somePerson', 'Some Person']],
|
||||
['pk;member add somePerson \"\'Some\' Person\"', ['add', 'somePerson', 'Some Person']],
|
||||
])('%s returns correct arguments', (content, expected) => {
|
||||
// Arrange
|
||||
const command = "member";
|
||||
const result = messageHelper.parseCommandArgs(content, command);
|
||||
expect(result).toEqual(expected);
|
||||
})
|
||||
})
|
||||
|
||||
describe(`parseProxyTags`, () => {
|
||||
const membersFor1 = [
|
||||
{name: "somePerson", proxy: "--text"},
|
||||
{name: "someSecondPerson", proxy: undefined},
|
||||
{name: "someOtherPerson", proxy: "?text}"},
|
||||
{name: "someLastPerson", proxy: "{text}"},
|
||||
{name: "someEmojiPerson", proxy: "⭐text"},
|
||||
{name: "someSpacePerson", proxy: "! text"},
|
||||
]
|
||||
|
||||
const membersFor2 = []
|
||||
|
||||
const membersFor3 = [
|
||||
{name: "someOtherThirdPerson", proxy: undefined}
|
||||
]
|
||||
|
||||
const attachmentUrl = "../oya.png"
|
||||
|
||||
beforeEach(() => {
|
||||
memberHelper.getMembersByAuthor = jest.fn().mockImplementation((specificAuthorId) => {
|
||||
if (specificAuthorId === "1") return membersFor1;
|
||||
if (specificAuthorId === "2") return membersFor2;
|
||||
if (specificAuthorId === "3") return membersFor3;
|
||||
})
|
||||
});
|
||||
|
||||
test.each([
|
||||
['1', 'hello', null, {}],
|
||||
['1', '--hello', null, {member: membersFor1[0], message: 'hello', hasAttachment: false}],
|
||||
['1', 'hello', attachmentUrl, {}],
|
||||
['1', '--hello', attachmentUrl, {member: membersFor1[0], message: 'hello', hasAttachment: true}],
|
||||
['1', '--', attachmentUrl, {member: membersFor1[0], message: '', hasAttachment: true}],
|
||||
['1', '?hello}', null, {member: membersFor1[2], message: 'hello', hasAttachment: false}],
|
||||
['1', '{hello}', null, {member: membersFor1[3], message: 'hello', hasAttachment: false}],
|
||||
['1', '⭐hello', null, {member: membersFor1[4], message: 'hello', hasAttachment: false}],
|
||||
['1', '! hello', null, {member: membersFor1[5], message: 'hello', hasAttachment: false}],
|
||||
['2', 'hello', null, undefined],
|
||||
['2', '--hello', null, undefined],
|
||||
['2', 'hello', attachmentUrl, undefined],
|
||||
['2', '--hello', attachmentUrl,undefined],
|
||||
['3', 'hello', null, {}],
|
||||
['3', '--hello', null, {}],
|
||||
['3', 'hello', attachmentUrl, {}],
|
||||
['3', '--hello', attachmentUrl,{}],
|
||||
])('ID %s with string %s returns correct proxy', async(specificAuthorId, content, attachmentUrl, expected) => {
|
||||
// Act
|
||||
return messageHelper.parseProxyTags(specificAuthorId, content, attachmentUrl).then((res) => {
|
||||
// Assert
|
||||
expect(res).toEqual(expected);
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
describe('returnBufferFromText', () => {
|
||||
const charas2000 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
|
||||
test('returns truncated text and buffer file when text is more than 2000 characters', () => {
|
||||
// Arrange
|
||||
|
||||
const charasOver2000 = "bbbbb"
|
||||
const expectedBuffer = Buffer.from(charasOver2000, 'utf-8');
|
||||
const expected = {text: charas2000, file: expectedBuffer};
|
||||
|
||||
// Act
|
||||
const result = messageHelper.returnBufferFromText(`${charas2000}${charasOver2000}`);
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual(expected);
|
||||
})
|
||||
|
||||
test('returns text when text is 2000 characters or less', () => {
|
||||
// Arrange
|
||||
const expected = {text: charas2000, file: undefined};
|
||||
// Act
|
||||
const result = messageHelper.returnBufferFromText(`${charas2000}`);
|
||||
// Assert
|
||||
expect(result).toEqual(expected);
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
// restore the spy created with spyOn
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
})
|
||||
269
tests/helpers/webhookHelper.test.js
Normal file
269
tests/helpers/webhookHelper.test.js
Normal file
@@ -0,0 +1,269 @@
|
||||
jest.mock('../../src/helpers/messageHelper.js')
|
||||
|
||||
const {messageHelper} = require("../../src/helpers/messageHelper.js");
|
||||
|
||||
jest.mock('../../src/helpers/messageHelper.js', () => {
|
||||
return {messageHelper: {
|
||||
parseProxyTags: jest.fn(),
|
||||
returnBuffer: jest.fn(),
|
||||
returnBufferFromText: jest.fn(),
|
||||
}}
|
||||
})
|
||||
|
||||
const {webhookHelper} = require("../../src/helpers/webhookHelper.js");
|
||||
const {enums} = require("../../src/enums");
|
||||
|
||||
describe('webhookHelper', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
jest.clearAllMocks();
|
||||
})
|
||||
|
||||
describe(`sendMessageAsMember`, () => {
|
||||
const client = {};
|
||||
const content = "hi"
|
||||
const attachments = {
|
||||
size: 0,
|
||||
first: () => {}
|
||||
}
|
||||
const message = {
|
||||
client,
|
||||
content: content,
|
||||
attachments: attachments,
|
||||
author: {
|
||||
id: '123'
|
||||
},
|
||||
guild: {
|
||||
guildId: '123'
|
||||
},
|
||||
reply: jest.fn()
|
||||
}
|
||||
const member = {proxy: "--text", name: 'somePerson', displayname: "Some Person"};
|
||||
const proxyMessage = {message: content, member: member}
|
||||
beforeEach(() => {
|
||||
jest.spyOn(webhookHelper, 'replaceMessage');
|
||||
|
||||
})
|
||||
|
||||
test('calls parseProxyTags and returns if proxyMatch is empty object', async() => {
|
||||
// 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();
|
||||
})
|
||||
})
|
||||
|
||||
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();
|
||||
})
|
||||
})
|
||||
|
||||
test('calls parseProxyTags with attachmentUrl', async() => {
|
||||
// Arrange
|
||||
message.attachments = {
|
||||
size: 1,
|
||||
first: () => {
|
||||
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');
|
||||
})
|
||||
})
|
||||
|
||||
test('if message matches member proxy but is not sent from a guild, throw an error', async() => {
|
||||
// Arrange
|
||||
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();
|
||||
})
|
||||
})
|
||||
|
||||
test('if message matches member proxy and sent in a guild and has an attachment, reply to message with ping', async() => {
|
||||
// Arrange
|
||||
message.guildId = '123'
|
||||
proxyMessage.hasAttachment = true;
|
||||
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();
|
||||
})
|
||||
})
|
||||
|
||||
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();
|
||||
})
|
||||
})
|
||||
|
||||
// TODO: flaky for some reason
|
||||
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`, () => {
|
||||
|
||||
})
|
||||
|
||||
describe(`getWebhook`, () => {
|
||||
|
||||
})
|
||||
|
||||
|
||||
afterEach(() => {
|
||||
// restore the spy created with spyOn
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
})
|
||||
Reference in New Issue
Block a user