mostly finished command tests

This commit is contained in:
Aster Fialla
2026-02-23 15:54:45 -05:00
parent 6937d7b694
commit 9059c63747

View File

@@ -40,14 +40,6 @@ describe('commands', () => {
const authorId = '123'; const authorId = '123';
const discriminator = '123'; const discriminator = '123';
const username = 'somePerson' const username = 'somePerson'
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
})
describe('memberCommand', () => {
const attachmentUrl = 'oya.png'; const attachmentUrl = 'oya.png';
const attachmentExpiration = new Date('2026-01-01').toDateString(); const attachmentExpiration = new Date('2026-01-01').toDateString();
const message = { const message = {
@@ -63,10 +55,19 @@ describe('commands', () => {
url: attachmentUrl url: attachmentUrl
})) }))
}, },
reply: jest.fn().mockResolvedValue() reply: jest.fn().mockResolvedValue(),
} }
const args = ['new'] const args = ['new']
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
})
describe('memberCommand', () => {
test('calls parseMemberCommand with the correct arguments', () => { test('calls parseMemberCommand with the correct arguments', () => {
// Arrange // Arrange
memberHelper.parseMemberCommand = jest.fn().mockResolvedValue("parsed command"); memberHelper.parseMemberCommand = jest.fn().mockResolvedValue("parsed command");
@@ -100,11 +101,80 @@ describe('commands', () => {
}); });
}) })
test('if parseMemberCommand returns object, reply with embed and content', () => {
// Arrange
const reply = {
errors: ['error', 'error2'],
success: 'success',
embed: {}
}
memberHelper.parseMemberCommand = jest.fn().mockResolvedValue(reply);
// Act
return commands.memberCommand(message, args).catch(() => {
// Assert
expect(message.reply).toHaveBeenCalledTimes(1);
expect(message.reply).toHaveBeenCalledWith({content: `success\n\n${enums.err.ERRORS_OCCURRED}\n\nerror\nerror2}`, embeds: [reply.embed]})
});
})
}) })
describe('importCommand', () => {
test('if message includes --help and no attachmentURL, return help message', () => {
const args = ["--help"];
message.content = "pf;import --help";
message.attachments.size = 0;
return commands.importCommand(message, args).then(() => {
expect(message.reply).toHaveBeenCalledTimes(1);
expect(message.reply).toHaveBeenCalledWith(enums.help.IMPORT);
expect(importHelper.pluralKitImport).not.toHaveBeenCalled();
})
})
test('if no args and no attachmentURL, return help message', () => {
const args = [""];
message.content = 'pf;import'
message.attachments.size = 0;
return commands.importCommand(message, args).then(() => {
expect(message.reply).toHaveBeenCalledTimes(1);
expect(message.reply).toHaveBeenCalledWith(enums.help.IMPORT);
expect(importHelper.pluralKitImport).not.toHaveBeenCalled();
})
})
test('if attachment URL, call pluralKitImport with correct arguments', () => {
const args = [""];
message.content = 'pf;import'
importHelper.pluralKitImport = jest.fn().mockResolvedValue('success');
return commands.importCommand(message, args).then(() => {
expect(message.reply).toHaveBeenCalledTimes(1);
expect(message.reply).toHaveBeenCalledWith('success');
expect(importHelper.pluralKitImport).toHaveBeenCalledTimes(1);
expect(importHelper.pluralKitImport).toHaveBeenCalledWith(authorId, attachmentUrl);
})
})
test('if pluralKitImport returns aggregate errors, send errors.', () => {
const args = [""];
message.content = 'pf;import'
importHelper.pluralKitImport = jest.fn().mockImplementation(() => {throw new AggregateError(['error1', 'error2'], 'errors')});
return commands.importCommand(message, args).catch(() => {
expect(message.reply).toHaveBeenCalledTimes(1);
expect(message.reply).toHaveBeenCalledWith(`errors. \n\n${enums.err.ERRORS_OCCURRED}\n\nerror1\nerror2`);
})
})
test('if message.reply throws error, call returnBufferFromText and message.reply again.', () => {
// Arrange
const args = [""];
message.content = 'pf;import'
message.reply = jest.fn().mockImplementationOnce(() => {throw e})
messageHelper.returnBufferFromText = jest.fn().mockResolvedValue({file: 'test.txt', text: 'normal content'});
return commands.importCommand(message, args).catch(() => {
expect(message.reply).toHaveBeenCalledTimes(2);
expect(message.reply).toHaveBeenNthCalledWith(1, {content: 'normal content', files: [{name: 'test.txt', data: 'test.txt' }],});
})
})
})
afterEach(() => { afterEach(() => {
// restore the spy created with spyOn // restore the spy created with spyOn