adding import tests

This commit is contained in:
Aster Fialla
2026-02-23 13:36:31 -05:00
parent 813e893afb
commit daf23b2f5d
4 changed files with 99 additions and 7650 deletions

7642
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -21,8 +21,11 @@
"@babel/core": "^7.29.0",
"@babel/plugin-transform-modules-commonjs": "^7.28.6",
"@babel/preset-env": "^7.29.0",
"@fetch-mock/jest": "^0.2.20",
"babel-jest": "^30.2.0",
"jest": "^30.2.0"
"fetch-mock": "^12.6.0",
"jest": "^30.2.0",
"jest-fetch-mock": "^3.0.3"
},
"scripts": {
"test": "jest"

View File

@@ -8,11 +8,11 @@ const ih = {};
*
* @async
* @param {string} authorId - The author of the message
* @param {string} attachmentUrl - The attached JSON url.
* @param {string | null} attachmentUrl - The attached JSON url.
* @returns {string} A successful addition of all members.
* @throws {Error} When the member exists, or creating a member doesn't work.
*/
ih.pluralKitImport = async function (authorId, attachmentUrl) {
ih.pluralKitImport = async function (authorId, attachmentUrl= null) {
if (!attachmentUrl) {
throw new Error(enums.err.NOT_JSON_FILE);
}
@@ -32,9 +32,9 @@ ih.pluralKitImport = async function (authorId, attachmentUrl) {
errors.push(e.message);
});
}
const aggregatedText = addedMembers.length > 0 ? `Successfully added members: ${addedMembers.join(', ')}` : enums.err.NO_MEMBERS_IMPORTED;
const aggregatedText = addedMembers.length > 0 ? `Successfully added members: ${addedMembers.join(', ')}` : `${enums.err.NO_MEMBERS_IMPORTED}`;
if (errors.length > 0) {
throw new AggregateError(errors, aggregatedText);
throw new AggregateError(errors, `${aggregatedText}\n\n${enums.err.IMPORT_ERROR}`);
}
return aggregatedText;
});

View File

@@ -1,12 +1,100 @@
const {enums} = require('../../src/enums.js');
const fetchMock = require('jest-fetch-mock');
jest.mock('../../src/helpers/memberHelper.js', () => {
return {
memberHelper: {
addFullMember: jest.fn()
}
}
})
fetchMock.enableMocks();
const {memberHelper} = require("../../src/helpers/memberHelper.js");
const {importHelper} = require('../../src/helpers/importHelper.js');
describe('importHelper', () => {
const authorId = '123';
const attachmentUrl = 'system.json';
const mockImportedMember = {
proxy_tags: [{
prefix: "SP{",
suffix: "}"
}],
display_name: "SomePerson",
avatar_url: 'oya.png',
name: 'somePerson'
}
const mockData = {
members: [mockImportedMember]
};
const mockAddReturnMember = {
proxy: "SP{text}",
displayname: "SomePerson",
propic: 'oya.png',
name: 'somePerson'
}
const mockAddReturn = {
member: mockAddReturnMember,
errors: []
}
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
global.fetch = jest.fn();
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve(mockData)
})
})
describe('pluralKitImport', () => {
test('if no attachment URL, throws error', () => {
return importHelper.pluralKitImport(authorId).catch((e) => {
expect(e).toEqual(new Error(enums.err.NOT_JSON_FILE));
})
})
test('if attachment URL, calls fetch and addFullMember and returns value', () => {
memberHelper.addFullMember.mockResolvedValue(mockAddReturn);
return importHelper.pluralKitImport(authorId, attachmentUrl).then((res) => {
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(attachmentUrl);
expect(memberHelper.addFullMember).toHaveBeenCalledWith(authorId, mockImportedMember.name, mockImportedMember.display_name, 'SP{text}', mockImportedMember.avatar_url);
expect(res).toEqual(`Successfully added members: ${mockAddReturnMember.name}`)
})
})
test('if addFullMember returns nothing, return correct enum', () => {
memberHelper.addFullMember.mockResolvedValue();
return importHelper.pluralKitImport(authorId, attachmentUrl).catch((res) => {
expect(res).toEqual(new AggregateError([], `${enums.err.NO_MEMBERS_IMPORTED}\n\n${enums.err.IMPORT_ERROR}`));
})
})
test('if addFullMember returns nothing and throws error, catch and return error', () => {
memberHelper.addFullMember.mockResolvedValue(new Error('error'));
return importHelper.pluralKitImport(authorId, attachmentUrl).catch((res) => {
expect(res).toEqual(new AggregateError([new Error('error')], `${enums.err.NO_MEMBERS_IMPORTED}\n\n${enums.err.IMPORT_ERROR}`))
})
})
test('if addFullMember returns member but also contains error, return member and error', () => {
// Arrange
const memberObj = {errors: ['error'], member: mockAddReturnMember};
memberHelper.addFullMember.mockResolvedValue(memberObj);
// Act
return importHelper.pluralKitImport(authorId, attachmentUrl).catch((res) => {
// Assert
expect(res).toEqual(new AggregateError(['error'], `Successfully added members: ${mockAddReturnMember.name}\n\n${enums.err.IMPORT_ERROR}`))
})
})
})
afterEach(() => {
// restore the spy created with spyOn
jest.restoreAllMocks();
jest.clearAllMocks();
});
})