diff --git a/__tests__/commands/Roll.js b/__tests__/commands/Roll.js index a8ba018..e06495d 100644 --- a/__tests__/commands/Roll.js +++ b/__tests__/commands/Roll.js @@ -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(); }); }); diff --git a/__tests__/commands/Spells.js b/__tests__/commands/Spells.js new file mode 100644 index 0000000..3f2a300 --- /dev/null +++ b/__tests__/commands/Spells.js @@ -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)'); + }); +}); diff --git a/__tests__/functions/getSpells.js b/__tests__/functions/getSpells.js new file mode 100644 index 0000000..de5d8a6 --- /dev/null +++ b/__tests__/functions/getSpells.js @@ -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(); + }); +}); diff --git a/commands/List.js b/commands/List.js index a4da4f9..e1a1819 100644 --- a/commands/List.js +++ b/commands/List.js @@ -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; }, diff --git a/commands/Roll.js b/commands/Roll.js index 4ed917e..b179794 100644 --- a/commands/Roll.js +++ b/commands/Roll.js @@ -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}` : ``})` ); } }, diff --git a/commands/Spells.js b/commands/Spells.js index 07500a5..948b8be 100644 --- a/commands/Spells.js +++ b/commands/Spells.js @@ -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(' ')} diff --git a/functions/Capitalize.js b/functions/Capitalize.js index ee483a2..237598e 100644 --- a/functions/Capitalize.js +++ b/functions/Capitalize.js @@ -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 }; diff --git a/functions/CountOccurences.js b/functions/CountOccurences.js index 5a9ab17..c5ffa01 100644 --- a/functions/CountOccurences.js +++ b/functions/CountOccurences.js @@ -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 }; diff --git a/functions/CreateResultTable.js b/functions/CreateResultTable.js index 32e5b96..f0576cd 100644 --- a/functions/CreateResultTable.js +++ b/functions/CreateResultTable.js @@ -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 }; diff --git a/functions/findMessage.js b/functions/findMessage.js index e2bcece..5961cbc 100644 --- a/functions/findMessage.js +++ b/functions/findMessage.js @@ -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 }; diff --git a/functions/getSpell.js b/functions/getSpell.js index 1103d80..8fb8e26 100644 --- a/functions/getSpell.js +++ b/functions/getSpell.js @@ -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 }; diff --git a/package-lock.json b/package-lock.json index 635fdbe..122c552 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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",