merge dev branch (more tests) (#47)
* more tests and bugfixes on spells * linting
This commit is contained in:
@ -4,9 +4,11 @@ require('babel-plugin-rewire');
|
||||
const command = require('@Commands/Roll');
|
||||
//const exec = command.__get__('exec');
|
||||
describe('roll command', () => {
|
||||
const message = { reply: str => str };
|
||||
const reply = jest.fn(str => str);
|
||||
const message = { author: { tag: 'test' }, reply: reply };
|
||||
it('should not return anything without correct arguments', () => {
|
||||
const args = ['1'];
|
||||
const args = ['1w6'];
|
||||
expect(command.exec(message, args)).resolves.toBeUndefined();
|
||||
expect(reply).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
76
__tests__/commands/Spells.js
Normal file
76
__tests__/commands/Spells.js
Normal file
@ -0,0 +1,76 @@
|
||||
require('module-alias/register');
|
||||
require('babel-plugin-rewire');
|
||||
|
||||
const Spells = require('@Commands/Spells');
|
||||
const createSpellList = Spells.__get__('createSpellList');
|
||||
const TestValue = {
|
||||
Name: 'Test',
|
||||
Level: 10,
|
||||
Attributes: [
|
||||
{ Name: 'Klugheit', Level: 10 },
|
||||
{ Name: 'Charisma', Level: 11 },
|
||||
{ Name: 'Mut', Level: 12 },
|
||||
],
|
||||
};
|
||||
|
||||
describe('createSpellList', () => {
|
||||
it('should return an array with expected object(s)', () => {
|
||||
const Test = { spells: [{ id: 'test', level: 10 }] };
|
||||
const expected = {
|
||||
Name: 'Test',
|
||||
Level: 10,
|
||||
Attributes: [
|
||||
{ Name: 'Klugheit', Level: 10 },
|
||||
{ Name: 'Charisma', Level: 11 },
|
||||
{ Name: 'Mut', Level: 12 },
|
||||
],
|
||||
};
|
||||
Spells.__Rewire__('getSpell', () => expected);
|
||||
|
||||
expect(createSpellList(Test)).toEqual(expect.arrayContaining([expected]));
|
||||
Spells.__ResetDependency__('getSpell');
|
||||
});
|
||||
it('should abort if character has no chants', () => {
|
||||
expect(createSpellList({ attributes: [] })).toBeNull();
|
||||
expect(createSpellList()).toBeNull();
|
||||
expect(createSpellList({})).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('ReplySpell', () => {
|
||||
const ReplySpell = Spells.__get__('ReplySpell');
|
||||
it('should return null if no param given.', () => {
|
||||
expect(ReplySpell()).toBeNull();
|
||||
});
|
||||
|
||||
it('should return a string', () => {
|
||||
// toBeInstanceOf(String) is not working :(
|
||||
expect(
|
||||
ReplySpell({
|
||||
Name: 'Test',
|
||||
Level: 9,
|
||||
Attributes: [
|
||||
{ Name: 'Klugheit', Level: 10 },
|
||||
{ Name: 'Charisma', Level: 11 },
|
||||
{ Name: 'Mut', Level: 12 },
|
||||
],
|
||||
})
|
||||
).toMatch(/.*Test.*(9)?.*/);
|
||||
});
|
||||
|
||||
Spells.__ResetDependency__('ReplySpell');
|
||||
});
|
||||
|
||||
describe('ReplySpellList', () => {
|
||||
const ReplySpellList = Spells.__get__('ReplySpellList');
|
||||
it('should return null if params are empty', () => {
|
||||
expect(ReplySpellList('')).toBe('Du kennst keine Zaubersprüche.');
|
||||
expect(ReplySpellList([])).toBe('Du kennst keine Zaubersprüche.');
|
||||
expect(ReplySpellList()).toBe('Du kennst keine Zaubersprüche.');
|
||||
});
|
||||
|
||||
it('should return Name and Level', () => {
|
||||
const input = [TestValue];
|
||||
expect(ReplySpellList(input)).toMatch('Test (10)');
|
||||
});
|
||||
});
|
109
__tests__/functions/getSpells.js
Normal file
109
__tests__/functions/getSpells.js
Normal file
@ -0,0 +1,109 @@
|
||||
require('module-alias/register');
|
||||
require('babel-plugin-rewire');
|
||||
|
||||
const command = require('@dsabot/getSpell');
|
||||
|
||||
describe('getSpell integration test', () => {
|
||||
it('should not find an entry for a spell', () => {
|
||||
const getSpell = command.__get__('getSpell');
|
||||
expect(getSpell({ Character: { name: '' }, spell_name: 'test' })).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null if char has no spells.', () => {
|
||||
const Character = {
|
||||
attributes: [
|
||||
{ id: 'mut', level: 10 },
|
||||
{ id: 'klugheit', level: 11 },
|
||||
{ id: 'charisma', level: 12 },
|
||||
],
|
||||
};
|
||||
command.__set__('Spells', [
|
||||
{ id: 'test', name: 'Test', attributes: ['MU', 'KL', 'CH'], modified_by: ['SK'] },
|
||||
]);
|
||||
|
||||
const getSpell = command.__get__('getSpell');
|
||||
|
||||
expect(getSpell({ Character: Character, spell_name: 'test' })).toBeNull();
|
||||
});
|
||||
|
||||
it('should return a correct spell result.', () => {
|
||||
const Character = {
|
||||
attributes: [
|
||||
{ id: 'mut', level: 10 },
|
||||
{ id: 'klugheit', level: 11 },
|
||||
{ id: 'charisma', level: 12 },
|
||||
],
|
||||
spells: [{ id: 'test', level: 7 }, { id: 'no-level' }],
|
||||
};
|
||||
const Spells = [
|
||||
{
|
||||
id: 'test',
|
||||
name: 'Testspell',
|
||||
attributes: ['MU', 'KL', 'CH'],
|
||||
modified_by: ['SK'],
|
||||
},
|
||||
{ id: 'no-level', name: 'No Level', attributes: ['MU', 'KL', 'CH'], modified_by: [] },
|
||||
];
|
||||
|
||||
command.__set__('Spells', Spells);
|
||||
|
||||
const getSpell = command.__get__('getSpell');
|
||||
|
||||
expect(getSpell({ Character: Character, spell_name: 'test' })).toEqual(
|
||||
expect.objectContaining({
|
||||
Name: 'Testspell',
|
||||
Level: 7,
|
||||
Attributes: [
|
||||
{ Level: 10, Name: 'MU' },
|
||||
{ Level: 11, Name: 'KL' },
|
||||
{ Level: 12, Name: 'CH' },
|
||||
],
|
||||
ModifiedBy: ['SK'],
|
||||
})
|
||||
);
|
||||
|
||||
expect(getSpell({ Character: Character, spell_name: 'Testspell' })).toEqual(
|
||||
expect.objectContaining({
|
||||
Name: 'Testspell',
|
||||
Level: 7,
|
||||
Attributes: [
|
||||
{ Level: 10, Name: 'MU' },
|
||||
{ Level: 11, Name: 'KL' },
|
||||
{ Level: 12, Name: 'CH' },
|
||||
],
|
||||
ModifiedBy: ['SK'],
|
||||
})
|
||||
);
|
||||
|
||||
expect(getSpell({ Character: Character, spell_name: 'no-level' })).toEqual(
|
||||
expect.objectContaining({
|
||||
Name: 'No Level',
|
||||
Level: 0,
|
||||
Attributes: [
|
||||
{ Level: 10, Name: 'MU' },
|
||||
{ Level: 11, Name: 'KL' },
|
||||
{ Level: 12, Name: 'CH' },
|
||||
],
|
||||
ModifiedBy: [],
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should not find the spell.', () => {
|
||||
const Character = {
|
||||
attributes: [
|
||||
{ id: 'mut', level: 10 },
|
||||
{ id: 'klugheit', level: 11 },
|
||||
{ id: 'charisma', level: 12 },
|
||||
],
|
||||
spells: [{ id: 'test', level: 7 }],
|
||||
};
|
||||
const Spells = [{ id: 'test', name: 'Testspell', attributes: ['MU', 'KL', 'CH'] }];
|
||||
|
||||
command.__set__('Spells', Spells);
|
||||
|
||||
const getSpell = command.__get__('getSpell');
|
||||
|
||||
expect(getSpell({ Character: Character, spell_name: 'well-hidden' })).toBeNull();
|
||||
});
|
||||
});
|
@ -88,16 +88,16 @@ module.exports = {
|
||||
}
|
||||
const Characters = []; //?+
|
||||
Promise.all(
|
||||
args.map(arg => {
|
||||
return findUser(arg).then(user => {
|
||||
args
|
||||
.map(arg => findUser(arg))
|
||||
.then(user => {
|
||||
if (!isEmpty(user)) {
|
||||
Characters.push({
|
||||
Name: user.character.name,
|
||||
Attributes: getStats(user),
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
})
|
||||
).then(() => returnResult(message, Characters));
|
||||
return null;
|
||||
},
|
||||
|
@ -20,8 +20,8 @@ module.exports = {
|
||||
const result = roll(numberOfDice, diceValues, message.author.tag);
|
||||
const total = Bonus ? Bonus + result.sum : result.sum;
|
||||
message.reply(
|
||||
`${findMessage('ROLL')} ${result.dice.join(', ')} ` +
|
||||
`(Gesamt: ${result.sum}${Bonus ? `+${Bonus}=${total}` : ``})`
|
||||
`${findMessage('ROLL')} \` ${result.dice.join(' `, ` ')} \`` +
|
||||
` (Gesamt: ${result.sum}${Bonus ? `+${Bonus}=${total}` : ``})`
|
||||
);
|
||||
}
|
||||
},
|
||||
|
@ -2,14 +2,15 @@ const Discord = require('discord.js');
|
||||
const { findMessage } = require('@dsabot/findMessage');
|
||||
const { getSpell } = require('@dsabot/getSpell');
|
||||
const { db } = require('../globals');
|
||||
const { isEmpty } = require('@dsabot/isEmpty');
|
||||
|
||||
const ReplySpellList = (SpellList = []) => {
|
||||
if (!SpellList) return findMessage('NO_SPELLS');
|
||||
if (isEmpty(SpellList)) return findMessage('NO_SPELLS');
|
||||
return `${SpellList.map(s => `${s.Name} (${s.Level})`).join('\n')}`;
|
||||
};
|
||||
|
||||
const ReplySpell = (Spell = {}) => {
|
||||
if (!Spell) return null;
|
||||
if (isEmpty(Spell)) return null;
|
||||
return `Deine Werte für ${Spell.Name} (${Spell.Level}) sind:
|
||||
|
||||
${Spell.Attributes.map(attribute => `${attribute.Name}: ${attribute.Level}`).join(' ')}
|
||||
|
@ -1,5 +1,3 @@
|
||||
const Capitalize = (Word = 'none') => {
|
||||
return Word[0].toUpperCase() + Word.substring(1);
|
||||
};
|
||||
const Capitalize = (Word = 'none') => `${Word[0].toUpperCase() + Word.substring(1)}`;
|
||||
|
||||
module.exports = { Capitalize };
|
||||
|
@ -1,5 +1,3 @@
|
||||
const CountOccurences = (arr, value) => {
|
||||
return arr.filter(v => v === value).length;
|
||||
};
|
||||
const CountOccurences = (arr, value) => arr.filter(v => v === value).length;
|
||||
|
||||
module.exports = { CountOccurences };
|
||||
|
@ -7,22 +7,20 @@ const CreateResultTable = ({
|
||||
Throws: Throws,
|
||||
PointsUsed: PointsUsed,
|
||||
Bonus: Bonus = 0,
|
||||
}) => {
|
||||
return `
|
||||
}) => `
|
||||
\`\`\`
|
||||
${''.padEnd(15)} ${Attributes.map(attr => `${attr.Name}`.padStart(6)).join('\t|\t')}\t|
|
||||
${'Dein Wert'.padEnd(15)} ${Attributes.map(attr =>
|
||||
`${attr.Level}${Bonus ? `(${f(Bonus)})` : ``}`.padStart(6)
|
||||
).join('\t|\t')}\t|
|
||||
`${attr.Level}${Bonus ? `(${f(Bonus)})` : ``}`.padStart(6)
|
||||
).join('\t|\t')}\t|
|
||||
${'Dein Wurf'.padEnd(15)} ${Throws.map(Throw => `${Throw}`.padStart(6)).join('\t|\t')}\t|
|
||||
${'Abzüge'.padEnd(15)} ${PointsUsed.map(Points => `${Points}`.replace(0, '--').padStart(6)).join(
|
||||
'\t|\t'
|
||||
)}\t|
|
||||
'\t|\t'
|
||||
)}\t|
|
||||
${'Gesamt'.padEnd(15)} ${PointsUsed.reduce((acc, cur) => acc + cur)
|
||||
.toString()
|
||||
.padStart(6)}
|
||||
.toString()
|
||||
.padStart(6)}
|
||||
\`\`\`
|
||||
`;
|
||||
};
|
||||
|
||||
module.exports = { CreateResultTable, f };
|
||||
|
@ -1,7 +1,5 @@
|
||||
const { Replies } = require('../globals');
|
||||
|
||||
const findMessage = value => {
|
||||
return Replies.find(r => r.id === value).string;
|
||||
};
|
||||
const findMessage = value => Replies.find(r => r.id === value).string;
|
||||
|
||||
module.exports = { findMessage };
|
||||
|
@ -1,30 +1,21 @@
|
||||
const { getAttributeLevels } = require('@dsabot/getAttributeLevels');
|
||||
const Spells = require('@Lib/Spells.json');
|
||||
const { isEmpty } = require('@dsabot/isEmpty');
|
||||
|
||||
const getSpell = ({ Character: Character = [], spell_name: spellName = '' } = {}) => {
|
||||
if (!Character.hasOwnProperty('spells')) return null;
|
||||
const spellEntry =
|
||||
Spells.find(spell => spell.id.toLowerCase() === spellName.toLowerCase()) ||
|
||||
Spells.find(spell => spell.name.toLowerCase() === spellName.toLowerCase());
|
||||
|
||||
if (!spellEntry) {
|
||||
console.log(`getSpell() did not find entry for ${spellName}`);
|
||||
return null;
|
||||
}
|
||||
if (isEmpty(spellEntry)) return null;
|
||||
|
||||
const Spell = Character.spells.find(spell => spell.id === spellEntry.id); //?+
|
||||
const Level = Spell.hasOwnProperty('level') ? Spell.level : 0;
|
||||
|
||||
let Level = 0; // This is the minimum attributes value.
|
||||
if (!Character.hasOwnProperty('spells')) return null;
|
||||
const Spell = Character.spells.find(spell => spell.id === spellEntry.id) || null; //?+
|
||||
if (Spell && Spell.hasOwnProperty('level')) {
|
||||
Level = Spell.level || 0;
|
||||
}
|
||||
const ModifiedBy = spellEntry.modified_by;
|
||||
const Attributes = getAttributeLevels(spellEntry.attributes, Character);
|
||||
|
||||
return {
|
||||
Name: spellEntry.name,
|
||||
Level: Level,
|
||||
Attributes: Attributes,
|
||||
ModifiedBy: ModifiedBy,
|
||||
};
|
||||
return { Name: spellEntry.name, Level: Level, Attributes: Attributes, ModifiedBy: ModifiedBy };
|
||||
};
|
||||
module.exports = { getSpell };
|
||||
|
5
package-lock.json
generated
5
package-lock.json
generated
@ -746,6 +746,7 @@
|
||||
"jest-resolve": "^26.6.2",
|
||||
"jest-util": "^26.6.2",
|
||||
"jest-worker": "^26.6.2",
|
||||
"node-notifier": "^8.0.0",
|
||||
"slash": "^3.0.0",
|
||||
"source-map": "^0.6.0",
|
||||
"string-length": "^4.0.1",
|
||||
@ -2254,7 +2255,8 @@
|
||||
"esprima": "^4.0.1",
|
||||
"estraverse": "^4.2.0",
|
||||
"esutils": "^2.0.2",
|
||||
"optionator": "^0.8.1"
|
||||
"optionator": "^0.8.1",
|
||||
"source-map": "~0.6.1"
|
||||
},
|
||||
"bin": {
|
||||
"escodegen": "bin/escodegen.js",
|
||||
@ -4553,6 +4555,7 @@
|
||||
"@types/node": "*",
|
||||
"anymatch": "^3.0.3",
|
||||
"fb-watchman": "^2.0.0",
|
||||
"fsevents": "^2.1.2",
|
||||
"graceful-fs": "^4.2.4",
|
||||
"jest-regex-util": "^26.0.0",
|
||||
"jest-serializer": "^26.6.2",
|
||||
|
Reference in New Issue
Block a user