diff --git a/.eslintrc b/.eslintrc index 269ed94..a099f96 100644 --- a/.eslintrc +++ b/.eslintrc @@ -69,7 +69,8 @@ "no-console": 0, "no-unneeded-ternary": 0, "object-shorthand": 0, - "no-useless-rename": 0 + "no-useless-rename": 0, + "arrow-body-style": ["error", "as-needed", { "requireReturnForObjectLiteral": true }] }, "env": { "node": true, diff --git a/__tests__/commands/Attack.js b/__tests__/commands/Attack.js index 7287fd0..b89906e 100644 --- a/__tests__/commands/Attack.js +++ b/__tests__/commands/Attack.js @@ -1,217 +1,231 @@ require('module-alias/register'); -const rewire = require('rewire'); +require('babel-plugin-rewire'); const Attack = require('@Commands/Attack'); -const rewireUtils = rewire('@Commands/Attack'); -const getWeapon = rewireUtils.__get__('getWeapon'); -const getAttributeLevel = rewireUtils.__get__('getAttributeLevel'); -const getCombatTechniqueLevel = rewireUtils.__get__('getCombatTechniqueLevel'); -const isMeleeWeapon = rewireUtils.__get__('isMeleeWeapon'); -const getAttribute = rewireUtils.__get__('getAttribute'); -const CompareAttackResult = rewireUtils.__get__('CompareAttackResult'); -const getCombatTechnique = rewireUtils.__get__('getCombatTechnique'); - -it('should be undefined without value', () => { - expect(getCombatTechnique({})).toBeUndefined(); +const getWeapon = Attack.__get__('getWeapon'); +const getAttributeLevel = Attack.__get__('getAttributeLevel'); +const getCombatTechniqueLevel = Attack.__get__('getCombatTechniqueLevel'); +const isMeleeWeapon = Attack.__get__('isMeleeWeapon'); +const getAttribute = Attack.__get__('getAttribute'); +const CompareAttackResult = Attack.__get__('CompareAttackResult'); +const getCombatTechnique = Attack.__get__('getCombatTechnique'); +describe('getCombatTechnique', () => { + it('should be undefined without value', () => { + expect(getCombatTechnique({})).toBeUndefined(); + }); + it('should be undefined without value', () => { + expect(getCombatTechnique({ combattechnique: 'made-up' })).toBeUndefined(); + }); + it('should be null without any params.', () => { + expect(getCombatTechnique()).toBeNull(); + }); + it('should return defined object', () => { + expect(getCombatTechnique({ combattechnique: 'dolche' })).toEqual( + expect.objectContaining({ + id: expect.any(String), + name: expect.any(String), + Leiteigenschaft: expect.anything(), + }) + ); + }); }); -it('should be undefined without value', () => { - expect(getCombatTechnique({ combattechnique: 'made-up' })).toBeUndefined(); -}); -it('should be null without any params.', () => { - expect(getCombatTechnique()).toBeNull(); -}); -it('should return defined object', () => { - expect(getCombatTechnique({ combattechnique: 'dolche' })).toEqual( - expect.objectContaining({ - id: expect.any(String), - name: expect.any(String), - Leiteigenschaft: expect.anything(), - }) - ); -}); -test('getAttribute should return Object', () => { - const obj = { id: 'mut', kuerzel: 'MU', name: 'Mut' }; - expect(getAttribute('KK')).toEqual( - expect.objectContaining({ - id: expect.any(String), - kuerzel: expect.any(String), - name: expect.any(String), - }) - ); - expect(getAttribute('MU')).toEqual(obj); +describe('getAttribute', () => { + test('should return Object', () => { + const obj = { id: 'mut', kuerzel: 'MU', name: 'Mut' }; + expect(getAttribute('KK')).toBeInstanceOf(Object); + expect(getAttribute('KK')).toEqual( + expect.objectContaining({ + id: expect.any(String), + kuerzel: expect.any(String), + name: expect.any(String), + }) + ); + expect(getAttribute('MU')).toEqual(obj); + }); }); -test('CompareAttackResults', () => { - expect(CompareAttackResult()).toEqual( - expect.objectContaining({ - Ok: expect.any(Boolean), - Patzer: expect.any(Boolean), - CriticalHit: expect.any(Boolean), - DoubleDamage: expect.any(Boolean), - Dice: expect.anything(), - }) - ); +describe('CompareAttackResults', () => { + test('CompareAttackResults', () => { + expect(CompareAttackResult()).toEqual( + expect.objectContaining({ + Ok: expect.any(Boolean), + Patzer: expect.any(Boolean), + CriticalHit: expect.any(Boolean), + DoubleDamage: expect.any(Boolean), + Dice: expect.anything(), + }) + ); + }); + + it('should return object with fumble', () => { + const obj = { + Ok: false, + Patzer: true, + CriticalHit: false, + DoubleDamage: false, + Dice: [20, 14], + }; + expect(CompareAttackResult([20, 14], 8)).toEqual(obj); + }); + it('should return object with crit', () => { + const obj = { + Ok: true, + Patzer: false, + CriticalHit: true, + DoubleDamage: false, + Dice: [1, 14], + }; + expect(CompareAttackResult([1, 14], 8)).toEqual(obj); + }); + + it('should return object with double damage', () => { + const obj = { + Ok: true, + Patzer: false, + CriticalHit: true, + DoubleDamage: true, + Dice: [1, 4], + }; + expect(CompareAttackResult([1, 4], 8)).toEqual(obj); + }); + + it('should return object without passing', () => { + const obj = { + Ok: false, + Patzer: false, + CriticalHit: false, + DoubleDamage: false, + Dice: [10], + }; + expect(CompareAttackResult([10, 14], 8)).toEqual(obj); + }); +}); +describe('getAttributeLevel', () => { + it('returns a number ', () => { + expect(getAttributeLevel({ attributes: [{ id: 'mut', level: 8 }] }, 'mut')).toBe(8); + }); }); -it('should return object with fumble', () => { - const obj = { - Ok: false, - Patzer: true, - CriticalHit: false, - DoubleDamage: false, - Dice: [20, 14], - }; - expect(CompareAttackResult([20, 14], 8)).toEqual(obj); +describe('getCombatTechniqueLevel', () => { + it('returnsa defined object ', () => { + const Player = { combattechniques: [{ id: 'dolche', level: 9 }] }; + const CombatTechnique = { name: 'Dolche', id: 'dolche', Leiteigenschaft: ['GE'] }; + + expect(getCombatTechniqueLevel(Player, CombatTechnique)).toEqual( + expect.objectContaining({ + id: expect.any(String), + name: expect.any(String), + level: expect.any(Number), + Leiteigenschaft: expect.any(Array), + }) + ); + }); }); -it('should return object with crit', () => { - const obj = { - Ok: true, - Patzer: false, - CriticalHit: true, - DoubleDamage: false, - Dice: [1, 14], - }; - expect(CompareAttackResult([1, 14], 8)).toEqual(obj); +describe('getWeapon', () => { + it('returns an object', () => { + expect(getWeapon('waqqif')).toBeInstanceOf(Object); + }); + it('returns a defined object ', () => { + expect(getWeapon('waqqif')).toEqual( + expect.objectContaining({ + id: expect.any(String), + name: expect.any(String), + dice: expect.any(Number), + diemodificator: expect.any(Number), + at_mod: expect.any(Number), + pa_mod: expect.any(Number), + article: expect.any(Number), + DmgThreshold: expect.any(Number), + combattechnique: expect.any(String), + }) + ); + }); +}); +describe('isMeleeWeapon', () => { + it('returns true ', () => { + expect(isMeleeWeapon({ id: 'waqqif' })).toBeTruthy(); + }); + it('returns false ', () => { + expect(isMeleeWeapon({ id: 'bogen' })).toBeFalsy(); + }); }); -it('should return object with double damage', () => { - const obj = { - Ok: true, - Patzer: false, - CriticalHit: true, - DoubleDamage: true, - Dice: [1, 4], - }; - expect(CompareAttackResult([1, 4], 8)).toEqual(obj); -}); - -it('should return object without passing', () => { - const obj = { - Ok: false, - Patzer: false, - CriticalHit: false, - DoubleDamage: false, - Dice: [10], - }; - expect(CompareAttackResult([10, 14], 8)).toEqual(obj); -}); - -it('returns a number ', () => { - expect(getAttributeLevel({ attributes: [{ id: 'mut', level: 8 }] }, 'mut')).toBe(8); -}); - -it('returnsa defined object ', () => { - const Player = { combattechniques: [{ id: 'dolche', level: 9 }] }; - const CombatTechnique = { name: 'Dolche', id: 'dolche', Leiteigenschaft: ['GE'] }; - - expect(getCombatTechniqueLevel(Player, CombatTechnique)).toEqual( - expect.objectContaining({ - id: expect.any(String), - name: expect.any(String), - level: expect.any(Number), - Leiteigenschaft: expect.any(Array), - }) - ); -}); - -it('returns a defined object ', () => { - expect(getWeapon('waqqif')).toEqual( - expect.objectContaining({ - id: expect.any(String), - name: expect.any(String), - dice: expect.any(Number), - diemodificator: expect.any(Number), - at_mod: expect.any(Number), - pa_mod: expect.any(Number), - article: expect.any(Number), - DmgThreshold: expect.any(Number), - combattechnique: expect.any(String), - }) - ); -}); -it('returns true ', () => { - expect(isMeleeWeapon({ id: 'waqqif' })).toBeTruthy(); -}); -it('returns false ', () => { - expect(isMeleeWeapon({ id: 'bogen' })).toBeFalsy(); -}); - -// main function - -it('should abort with a message: no entry found', () => { - const reply = jest.fn(str => str); - - const message = { - reply: reply, - }; - const handleAttack = rewireUtils.__get__('handleAttack'); - //expect(handleAttack(err)).toThrowError(); - expect(handleAttack({}, { message: message })).toEqual( - 'Sorry, für dich habe ich leider keinen Eintrag 😥' - ); -}); - -it('should abort with a message: No such weapon', () => { - const reply = jest.fn(str => str); - - const message = { - reply: reply, - }; - const handleAttack = rewireUtils.__get__('handleAttack'); - const args = ['']; - expect(handleAttack([{ character: {} }], { message: message, args: args })).toEqual( - 'Diese Waffe gibt es nicht.' - ); -}); - -it('complete run with melee weapon', () => { - const reply = jest.fn(str => str); - - const message = { - reply: reply, - }; - const character = { - attributes: [ - { id: 'mut', level: 10 }, - { id: 'fingerfertigkeit', level: 10 }, - { id: 'klugheit', level: 10 }, - { id: 'intuition', level: 10 }, - { id: 'charisma', level: 10 }, - { id: 'gewandtheit', level: 16 }, - { id: 'konstitution', level: 10 }, - { id: 'koerperkraft', level: 10 }, - ], - combattechniques: [{ id: 'dolche', level: 8 }], - }; - const handleAttack = rewireUtils.__get__('handleAttack'); - const args = ['messer']; - expect(handleAttack({ character: character }, { message: message, args: args })).toEqual( - expect.any(String) - ); -}); - -it('complete run with ranged weapon', () => { - const reply = jest.fn(str => str); - - const message = { - reply: reply, - }; - const character = { - attributes: [ - { id: 'mut', level: 10 }, - { id: 'fingerfertigkeit', level: 14 }, - { id: 'klugheit', level: 10 }, - { id: 'intuition', level: 10 }, - { id: 'charisma', level: 10 }, - { id: 'gewandtheit', level: 16 }, - { id: 'konstitution', level: 10 }, - { id: 'koerperkraft', level: 10 }, - ], - combattechniques: [{ id: 'boegen', level: 8 }], - }; - const handleAttack = rewireUtils.__get__('handleAttack'); - const args = ['langbogen']; - expect(handleAttack({ character: character }, { message: message, args: args })).toEqual( - expect.any(String) - ); +describe('main Function', () => { + it('should abort with a message: no entry found', () => { + const reply = jest.fn(str => str); + + const message = { + reply: reply, + }; + const handleAttack = Attack.__get__('handleAttack'); + //expect(handleAttack(err)).toThrowError(); + expect(handleAttack({}, { message: message })).toEqual( + 'Sorry, für dich habe ich leider keinen Eintrag 😥' + ); + }); + + it('should abort with a message: No such weapon', () => { + const reply = jest.fn(str => str); + + const message = { + reply: reply, + }; + const handleAttack = Attack.__get__('handleAttack'); + const args = ['']; + expect(handleAttack([{ character: {} }], { message: message, args: args })).toEqual( + 'Diese Waffe gibt es nicht.' + ); + }); + + it('complete run with melee weapon', () => { + const reply = jest.fn(str => str); + + const message = { + reply: reply, + }; + const character = { + attributes: [ + { id: 'mut', level: 10 }, + { id: 'fingerfertigkeit', level: 10 }, + { id: 'klugheit', level: 10 }, + { id: 'intuition', level: 10 }, + { id: 'charisma', level: 10 }, + { id: 'gewandtheit', level: 16 }, + { id: 'konstitution', level: 10 }, + { id: 'koerperkraft', level: 10 }, + ], + combattechniques: [{ id: 'dolche', level: 8 }], + }; + const handleAttack = Attack.__get__('handleAttack'); + const args = ['messer']; + expect(handleAttack({ character: character }, { message: message, args: args })).toEqual( + expect.any(String) + ); + }); + + it('complete run with ranged weapon', () => { + const reply = jest.fn(str => str); + + const message = { + reply: reply, + }; + const character = { + attributes: [ + { id: 'mut', level: 10 }, + { id: 'fingerfertigkeit', level: 14 }, + { id: 'klugheit', level: 10 }, + { id: 'intuition', level: 10 }, + { id: 'charisma', level: 10 }, + { id: 'gewandtheit', level: 16 }, + { id: 'konstitution', level: 10 }, + { id: 'koerperkraft', level: 10 }, + ], + combattechniques: [{ id: 'boegen', level: 8 }], + }; + const handleAttack = Attack.__get__('handleAttack'); + const args = ['langbogen']; + expect(handleAttack({ character: character }, { message: message, args: args })).toEqual( + expect.any(String) + ); + }); }); diff --git a/__tests__/commands/Attribute.js b/__tests__/commands/Attribute.js index d5fcd5d..daad8c8 100644 --- a/__tests__/commands/Attribute.js +++ b/__tests__/commands/Attribute.js @@ -1,39 +1,43 @@ require('module-alias/register'); -const rewire = require('rewire'); +require('babel-plugin-rewire'); const Attribute = require('@Commands/Attribute'); -const rewireUtils = rewire('@Commands/Attribute'); -const HandleNamedAttributes = rewireUtils.__get__('HandleNamedAttributes'); -const getAttributeLevel = rewireUtils.__get__('getAttributeLevel'); -const getAttribute = rewireUtils.__get__('getAttribute'); -const handleAttributeCheck = rewireUtils.__get__('handleAttributeCheck'); -test('getAttribute should return Object', () => { - const obj = { id: 'mut', kuerzel: 'MU', name: 'Mut' }; - expect(getAttribute('KK')).toEqual( - expect.objectContaining({ - id: expect.any(String), - kuerzel: expect.any(String), - name: expect.any(String), - }) - ); - expect(getAttribute('MU')).toEqual(obj); - expect(getAttribute('mut')).toEqual(obj); -}); -it('should return undefined', () => { - expect(getAttribute()).toBeUndefined(); -}); +const HandleNamedAttributes = Attribute.__get__('HandleNamedAttributes'); +const getAttributeLevel = Attribute.__get__('getAttributeLevel'); +const getAttribute = Attribute.__get__('getAttribute'); +const handleAttributeCheck = Attribute.__get__('handleAttributeCheck'); -it('returns a number ', () => { - expect(getAttributeLevel({ attributes: [{ id: 'mut', level: 8 }] }, { id: 'mut' })).toBe(8); +describe('getAttribute', () => { + test('getAttribute should return Object', () => { + const obj = { id: 'mut', kuerzel: 'MU', name: 'Mut' }; + expect(getAttribute('KK')).toEqual( + expect.objectContaining({ + id: expect.any(String), + kuerzel: expect.any(String), + name: expect.any(String), + }) + ); + expect(getAttribute('MU')).toEqual(obj); + expect(getAttribute('mut')).toEqual(obj); + }); + it('should return undefined', () => { + expect(getAttribute()).toBeUndefined(); + }); }); - -it('should return an object', () => { - const Character = { - attributes: [{ id: 'mut', level: 8 }], - }; - expect(HandleNamedAttributes({ Character: Character, args: ['mut'] })).toEqual({ - Name: 'Mut', - Level: 8, +describe('getAttributeLevel', () => { + it('returns a number ', () => { + expect(getAttributeLevel({ attributes: [{ id: 'mut', level: 8 }] }, { id: 'mut' })).toBe(8); + }); +}); +describe('HandleNamedAttribute', () => { + it('should return an object', () => { + const Character = { + attributes: [{ id: 'mut', level: 8 }], + }; + expect(HandleNamedAttributes({ Character: Character, args: ['mut'] })).toEqual({ + Name: 'Mut', + Level: 8, + }); }); }); diff --git a/__tests__/commands/Chants.js b/__tests__/commands/Chants.js new file mode 100644 index 0000000..dc9652d --- /dev/null +++ b/__tests__/commands/Chants.js @@ -0,0 +1,76 @@ +require('module-alias/register'); +require('babel-plugin-rewire'); + +const Chants = require('@Commands/Chants'); +const createChantList = Chants.__get__('createChantList'); +const TestValue = { + Name: 'Test', + Level: 10, + Attributes: [ + { Name: 'Klugheit', Level: 10 }, + { Name: 'Charisma', Level: 11 }, + { Name: 'Mut', Level: 12 }, + ], +}; + +describe('createChantList', () => { + it('should return an array with expected object(s)', () => { + const Test = { chants: [{ id: 'test', level: 10 }] }; + const expected = { + Name: 'Test', + Level: 10, + Attributes: [ + { Name: 'Klugheit', Level: 10 }, + { Name: 'Charisma', Level: 11 }, + { Name: 'Mut', Level: 12 }, + ], + }; + Chants.__Rewire__('getChant', () => expected); + + expect(createChantList(Test)).toEqual(expect.arrayContaining([expected])); + Chants.__ResetDependency__('getChant'); + }); + it('should abort if character has no chants', () => { + expect(createChantList({ attributes: [] })).toBeNull(); + expect(createChantList()).toBeNull(); + expect(createChantList({})).toBeNull(); + }); +}); + +describe('ReplyChant', () => { + const ReplyChant = Chants.__get__('ReplyChant'); + it('should return null if no param given.', () => { + expect(ReplyChant()).toBeNull(); + }); + + it('should return a string', () => { + // toBeInstanceOf(String) is not working :( + expect( + ReplyChant({ + Name: 'Test', + Level: 9, + Attributes: [ + { Name: 'Klugheit', Level: 10 }, + { Name: 'Charisma', Level: 11 }, + { Name: 'Mut', Level: 12 }, + ], + }) + ).toMatch(/.*Test.*(9)?.*/); + }); + + Chants.__ResetDependency__('ReplyChant'); +}); + +describe('ReplyChantList', () => { + const ReplyChantList = Chants.__get__('ReplyChantList'); + it('should return null if params are empty', () => { + expect(ReplyChantList('')).toBeNull(); + expect(ReplyChantList([])).toBeNull(); + expect(ReplyChantList()).toBeNull(); + }); + + it('should return Name and Level', () => { + const input = [TestValue]; + expect(ReplyChantList(input)).toMatch('Test (10)'); + }); +}); diff --git a/__tests__/commands/List.js b/__tests__/commands/List.js index d930db5..bc9ec45 100644 --- a/__tests__/commands/List.js +++ b/__tests__/commands/List.js @@ -1,39 +1,134 @@ require('module-alias/register'); +require('babel-plugin-rewire'); const List = require('@Commands/List'); -const rewire = require('rewire'); - -const reWireUtils = rewire('@Commands/List'); // const getStats = reWireUtils.__get__('getStats'); -const getAttribute = reWireUtils.__get__('getAttribute'); -const printHeader = reWireUtils.__get__('printHeader'); -const listStats = reWireUtils.__get__('listStats'); -it('should return an attribute object', () => { - expect(getAttribute({ id: 'mut', level: 9 })).toEqual( - expect.objectContaining({ - id: 'mut', - Name: 'Mut', - Level: 9, - }) - ); - expect(getAttribute()).toEqual( - expect.objectContaining({ - id: expect.any(String), - Name: expect.any(String), - Level: expect.any(Number), - }) - ); +const getAttribute = List.__get__('getAttribute'); +const printHeader = List.__get__('printHeader'); +const listStats = List.__get__('listStats'); + +describe('getAttributes', () => { + it('should return an attribute object', () => { + expect(getAttribute({ id: 'mut', level: 9 })).toEqual( + expect.objectContaining({ + id: 'mut', + Name: 'Mut', + Level: 9, + }) + ); + expect(getAttribute()).toEqual( + expect.objectContaining({ + id: expect.any(String), + Name: expect.any(String), + Level: expect.any(Number), + }) + ); + }); }); -it('should return null', () => { - expect(printHeader()).toBeNull(); -}); -it('should return a string', () => { - const attributes = [{ Short: 'AA' }, { Short: 'BB' }, { Short: 'CC' }]; - expect(printHeader(attributes)).toMatch(/\s+AA\s+[|]\s+BB\s+[|]\s+CC\s+/g); +describe('printHeader', () => { + it('should return null with no params', () => { + expect(printHeader()).toBeNull(); + }); + it('should return a string', () => { + const attributes = [{ Short: 'AA' }, { Short: 'BB' }, { Short: 'CC' }]; + expect(printHeader(attributes)).toMatch(/\s+AA\s+[|]\s+BB\s+[|]\s+CC\s+/g); + }); }); -it('should return a string', () => { - expect(listStats([{ Level: 8 }, { Level: 9 }, { Level: 10 }])).toMatch( - /\s+8\s+[|]\s+9\s+[|]\s+10\s+/ - ); +describe('listStats', () => { + it('should return a string', () => { + expect(listStats([{ Level: 8 }, { Level: 9 }, { Level: 10 }])).toMatch( + /\s+8\s+[|]\s+9\s+[|]\s+10\s+/ + ); + }); +}); + +describe('findUser', () => { + const expected = { + user: 'Test', + character: { + name: 'test', + }, + }; + + List.__Rewire__('db', { + findOne: () => Promise.resolve(expected), + }); + const findUser = List.__get__('findUser'); + expect(findUser()).toBeInstanceOf(Promise); + expect(findUser()).resolves.toEqual(expected); +}); +/* +describe('findUsers', () => { + const expected = { + user: 'Test', + character: { + name: 'test', + attributes: [{ id: 'mut', level: 9 }], + }, + }; + + List.__Rewire__('db', { + findOne: () => { + return Promise.resolve(expected); + }, + }); + const findUsers = List.__get__('findUsers'); + expect(findUser()).toBeInstanceOf(Promise); + expect(findUser()).resolves.toEqual(expected); +});*/ + +describe('getStats', () => { + it('should give name, id and level in order', () => { + const getStats = List.__get__('getStats'); + const user = { + character: { + attributes: [ + { id: 'mut', level: 10 }, + { id: 'klugheit', level: 11 }, + ], + }, + }; + + expect(getStats(user)).toEqual( + expect.arrayContaining([ + { + Name: expect.any(String), + Level: expect.any(Number), + Short: expect.any(String), + id: expect.any(String), + }, + ]) + ); + }); +}); + +describe('returnResult', () => { + const returnResult = List.__get__('returnResult'); + + const message = { reply: str => str }; + const characters = [ + { + Name: 'Zulu', + Attributes: [ + { id: 'charisma', Level: 7, Short: 'CH' }, + { id: 'mut', Level: 14, Short: 'MU' }, + ], + }, + { + Name: 'Alfa', + Attributes: [ + { id: 'mut', Level: 10, Short: 'MU' }, + { id: 'charisma', Level: 11, Short: 'CH' }, + ], + }, + ]; + it('returns if no characters were found', () => { + expect(returnResult(message, [])).toBe('Keine Benutzer auf dieser Liste gefunden.'); + expect(returnResult(message)).toBe('Keine Benutzer auf dieser Liste gefunden.'); + }); + + it('returns a string ', () => { + expect(returnResult(message, characters)).toMatch(/.*/); + }); }); diff --git a/__tests__/commands/Roll.js b/__tests__/commands/Roll.js new file mode 100644 index 0000000..a8ba018 --- /dev/null +++ b/__tests__/commands/Roll.js @@ -0,0 +1,12 @@ +require('module-alias/register'); +require('babel-plugin-rewire'); + +const command = require('@Commands/Roll'); +//const exec = command.__get__('exec'); +describe('roll command', () => { + const message = { reply: str => str }; + it('should not return anything without correct arguments', () => { + const args = ['1']; + expect(command.exec(message, args)).resolves.toBeUndefined(); + }); +}); diff --git a/__tests__/functions/getChant.js b/__tests__/functions/getChant.js new file mode 100644 index 0000000..d51dbee --- /dev/null +++ b/__tests__/functions/getChant.js @@ -0,0 +1,103 @@ +require('module-alias/register'); +require('babel-plugin-rewire'); + +const command = require('@dsabot/getChant'); + +describe('getChant integration test', () => { + it('should not find an entry for a chant', () => { + const getChant = command.__get__('getChant'); + expect(getChant({ Character: { name: '' }, chant_name: 'test' })).toBeNull(); + }); + + it('should return null if char has no chants.', () => { + const Character = { + attributes: [ + { id: 'mut', level: 10 }, + { id: 'klugheit', level: 11 }, + { id: 'charisma', level: 12 }, + ], + }; + command.__set__('Chants', [{ id: 'test', name: 'Test', attributes: ['MU', 'KL', 'CH'] }]); + + const getChant = command.__get__('getChant'); + + expect(getChant({ Character: Character, chant_name: 'test' })).toBeNull(); + }); + + it('should return a correct chant result.', () => { + const Character = { + attributes: [ + { id: 'mut', level: 10 }, + { id: 'klugheit', level: 11 }, + { id: 'charisma', level: 12 }, + ], + chants: [{ id: 'test', level: 7 }, { id: 'no-level' }], + }; + const Chants = [ + { + id: 'test', + name: 'Testchant', + attributes: ['MU', 'KL', 'CH'], + }, + { id: 'no-level', name: 'No Level', attributes: ['MU', 'KL', 'CH'] }, + ]; + + command.__set__('Chants', Chants); + + const getChant = command.__get__('getChant'); + + expect(getChant({ Character: Character, chant_name: 'test' })).toEqual( + expect.objectContaining({ + Name: 'Testchant', + Level: 7, + Attributes: [ + { Level: 10, Name: 'MU' }, + { Level: 11, Name: 'KL' }, + { Level: 12, Name: 'CH' }, + ], + }) + ); + + expect(getChant({ Character: Character, chant_name: 'Testchant' })).toEqual( + expect.objectContaining({ + Name: 'Testchant', + Level: 7, + Attributes: [ + { Level: 10, Name: 'MU' }, + { Level: 11, Name: 'KL' }, + { Level: 12, Name: 'CH' }, + ], + }) + ); + + expect(getChant({ Character: Character, chant_name: 'no-level' })).toEqual( + expect.objectContaining({ + Name: 'No Level', + Level: 0, + Attributes: [ + { Level: 10, Name: 'MU' }, + { Level: 11, Name: 'KL' }, + { Level: 12, Name: 'CH' }, + ], + }) + ); + }); + + it('should not find the chant.', () => { + const Character = { + attributes: [ + { id: 'mut', level: 10 }, + { id: 'klugheit', level: 11 }, + { id: 'charisma', level: 12 }, + ], + chants: [{ id: 'test', level: 7 }], + }; + const Chants = [{ id: 'test', name: 'Testchant', attributes: ['MU', 'KL', 'CH'] }]; + + command.__set__('Chants', Chants); + + const getChant = command.__get__('getChant'); + + expect(getChant({ Character: Character, chant_name: 'well-hidden' })).toBeNull(); + }); +}); diff --git a/babel.config.js b/babel.config.js index 26cf509..e7d0bf1 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,3 +1,16 @@ module.exports = { - plugins: ['babel-plugin-rewire'], + plugins: [ + 'babel-plugin-rewire', + [ + 'module-resolver', + { + root: ['.'], + alias: { + '@Lib': './lib', + '@dsabot': './functions', + '@Commands': './commands', + }, + }, + ], + ], }; diff --git a/commands/Chants.js b/commands/Chants.js index fb202ca..d5868c6 100644 --- a/commands/Chants.js +++ b/commands/Chants.js @@ -1,3 +1,4 @@ +require('module-alias/register'); const Discord = require('discord.js'); const { findMessage } = require('@dsabot/findMessage'); const { getChant } = require('@dsabot/getChant'); @@ -11,19 +12,18 @@ const createChantList = (Character = {}) => { Character.chants.forEach(chant => ChantList.push(getChant({ Character: Character, chant_name: chant.id })) ); - console.log(ChantList); return ChantList.filter(value => value !== undefined && value !== null); }; const ReplyChantList = (ChantList = []) => { - if (!ChantList) return null; + if (!ChantList || ChantList.length === 0) return null; return `${ChantList.map(chant => `${chant.Name} ${chant.Level ? `(${chant.Level})` : ''}`).join( '\n' )}`; }; const ReplyChant = (Chant = {}) => { - if (!Chant) return null; + if (!Chant || Object.keys(Chant).length === 0) return null; return `Deine Werte für ${Chant.Name} ${Chant.Level ? `(${Chant.Level})` : ''} sind: ${Chant.Attributes.map(attribute => `${attribute.Name}: ${attribute.Level}`).join(' ')} diff --git a/functions/Capitalize.js b/functions/Capitalize.js index 91360c3..ee483a2 100644 --- a/functions/Capitalize.js +++ b/functions/Capitalize.js @@ -1,5 +1,5 @@ const Capitalize = (Word = 'none') => { - return Word[0].toUpperCase() + Word.substring(1); + return Word[0].toUpperCase() + Word.substring(1); }; module.exports = { Capitalize }; diff --git a/functions/CountOccurences.js b/functions/CountOccurences.js index 2f98f8f..5a9ab17 100644 --- a/functions/CountOccurences.js +++ b/functions/CountOccurences.js @@ -1,5 +1,5 @@ const CountOccurences = (arr, value) => { - return arr.filter((v) => (v === value)).length; + return arr.filter(v => v === value).length; }; -module.exports = { CountOccurences }; \ No newline at end of file +module.exports = { CountOccurences }; diff --git a/functions/getChant.js b/functions/getChant.js index 04d784f..fb833f5 100644 --- a/functions/getChant.js +++ b/functions/getChant.js @@ -1,22 +1,18 @@ +require('module-alias/register'); const { getAttributeLevels } = require('@dsabot/getAttributeLevels'); const Chants = require('@Lib/Chants.json'); +const { isEmpty } = require('@dsabot/isEmpty'); const getChant = ({ Character: Character = [], chant_name: chantName = '' } = {}) => { - //if (!Character.hasOwnProperty('chants')) return; + if (!Character.hasOwnProperty('chants')) return null; const chantEntry = Chants.find(chant => chant.id.toLowerCase() === chantName.toLowerCase()) || Chants.find(chant => chant.name.toLowerCase() === chantName.toLowerCase()); - if (!chantEntry) { - console.log(`getChant() Did not find entry for ${chantName}`); - return null; - } - - let Level = 0; // This is the minimum attributes value. - const Chant = Character.chants.find(chant => chant.id === chantEntry.id) || null; - if (Chant && Chant.hasOwnProperty('level')) { - Level = Chant.level || 0; - } + // let us filter out blessings. + if (isEmpty(chantEntry)) return null; + const Chant = Character.chants.find(chant => chant.id === chantEntry.id); // || null; + const Level = Chant.hasOwnProperty('level') ? Chant.level : 0; const Attributes = getAttributeLevels(chantEntry.attributes, Character); return { Name: chantEntry.name, Level: Level, Attributes: Attributes }; diff --git a/package-lock.json b/package-lock.json index a610f44..bca40cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "dsabot", - "version": "1.6.1", + "version": "1.6.2", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "1.6.1", + "version": "1.6.2", "license": "ISC", "dependencies": { "discord.js": "^12.5.3", @@ -17,13 +17,14 @@ }, "devDependencies": { "@types/jest": "^26.0.23", + "babel-jest": "^26.6.3", + "babel-plugin-module-resolver": "^4.1.0", "babel-plugin-rewire": "^1.2.0", "eslint": "^6.8.0", "eslint-config-airbnb": "^18.2.1", "eslint-config-prettier": "^6.8.0", "eslint-plugin-prettier": "^3.4.0", - "jest": "^26.6.3", - "rewire": "^5.0.0" + "jest": "^26.6.3" } }, "node_modules/@babel/code-frame": { @@ -1341,6 +1342,22 @@ "node": ">= 10.14.2" } }, + "node_modules/babel-plugin-module-resolver": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/babel-plugin-module-resolver/-/babel-plugin-module-resolver-4.1.0.tgz", + "integrity": "sha512-MlX10UDheRr3lb3P0WcaIdtCSRlxdQsB1sBqL7W0raF070bGl1HQQq5K3T2vf2XAYie+ww+5AKC/WrkjRO2knA==", + "dev": true, + "dependencies": { + "find-babel-config": "^1.2.0", + "glob": "^7.1.6", + "pkg-up": "^3.1.0", + "reselect": "^4.0.0", + "resolve": "^1.13.1" + }, + "engines": { + "node": ">= 8.0.0" + } + }, "node_modules/babel-plugin-rewire": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/babel-plugin-rewire/-/babel-plugin-rewire-1.2.0.tgz", @@ -3437,6 +3454,37 @@ "node": ">=8" } }, + "node_modules/find-babel-config": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/find-babel-config/-/find-babel-config-1.2.0.tgz", + "integrity": "sha512-jB2CHJeqy6a820ssiqwrKMeyC6nNdmrcgkKWJWmpoxpE8RKciYJXCcXRq1h2AzCo5I5BJeN2tkGEO3hLTuePRA==", + "dev": true, + "dependencies": { + "json5": "^0.5.1", + "path-exists": "^3.0.0" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/find-babel-config/node_modules/json5": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/json5/-/json5-0.5.1.tgz", + "integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=", + "dev": true, + "bin": { + "json5": "lib/cli.js" + } + }, + "node_modules/find-babel-config/node_modules/path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -6092,6 +6140,64 @@ "node": ">=8" } }, + "node_modules/pkg-up": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/pkg-up/-/pkg-up-3.1.0.tgz", + "integrity": "sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA==", + "dev": true, + "dependencies": { + "find-up": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/pkg-up/node_modules/find-up": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", + "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", + "dev": true, + "dependencies": { + "locate-path": "^3.0.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/pkg-up/node_modules/locate-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", + "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", + "dev": true, + "dependencies": { + "p-locate": "^3.0.0", + "path-exists": "^3.0.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/pkg-up/node_modules/p-locate": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", + "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", + "dev": true, + "dependencies": { + "p-limit": "^2.0.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/pkg-up/node_modules/path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/posix-character-classes": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/posix-character-classes/-/posix-character-classes-0.1.1.tgz", @@ -6502,6 +6608,12 @@ "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==", "dev": true }, + "node_modules/reselect": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/reselect/-/reselect-4.0.0.tgz", + "integrity": "sha512-qUgANli03jjAyGlnbYVAV5vvnOmJnODyABz51RdBN7M4WaVu8mecZWgyQNkG8Yqe3KRGRt0l4K4B3XVEULC4CA==", + "dev": true + }, "node_modules/resolve": { "version": "1.19.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.19.0.tgz", @@ -6565,16 +6677,6 @@ "node": ">=0.12" } }, - "node_modules/rewire": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/rewire/-/rewire-5.0.0.tgz", - "integrity": "sha512-1zfitNyp9RH5UDyGGLe9/1N0bMlPQ0WrX0Tmg11kMHBpqwPJI4gfPpP7YngFyLbFmhXh19SToAG0sKKEFcOIJA==", - "dev": true, - "license": "MIT", - "dependencies": { - "eslint": "^6.8.0" - } - }, "node_modules/rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", @@ -9529,6 +9631,19 @@ "@types/babel__traverse": "^7.0.6" } }, + "babel-plugin-module-resolver": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/babel-plugin-module-resolver/-/babel-plugin-module-resolver-4.1.0.tgz", + "integrity": "sha512-MlX10UDheRr3lb3P0WcaIdtCSRlxdQsB1sBqL7W0raF070bGl1HQQq5K3T2vf2XAYie+ww+5AKC/WrkjRO2knA==", + "dev": true, + "requires": { + "find-babel-config": "^1.2.0", + "glob": "^7.1.6", + "pkg-up": "^3.1.0", + "reselect": "^4.0.0", + "resolve": "^1.13.1" + } + }, "babel-plugin-rewire": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/babel-plugin-rewire/-/babel-plugin-rewire-1.2.0.tgz", @@ -11188,6 +11303,30 @@ "to-regex-range": "^5.0.1" } }, + "find-babel-config": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/find-babel-config/-/find-babel-config-1.2.0.tgz", + "integrity": "sha512-jB2CHJeqy6a820ssiqwrKMeyC6nNdmrcgkKWJWmpoxpE8RKciYJXCcXRq1h2AzCo5I5BJeN2tkGEO3hLTuePRA==", + "dev": true, + "requires": { + "json5": "^0.5.1", + "path-exists": "^3.0.0" + }, + "dependencies": { + "json5": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/json5/-/json5-0.5.1.tgz", + "integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=", + "dev": true + }, + "path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true + } + } + }, "find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -13223,6 +13362,51 @@ "find-up": "^4.0.0" } }, + "pkg-up": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/pkg-up/-/pkg-up-3.1.0.tgz", + "integrity": "sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA==", + "dev": true, + "requires": { + "find-up": "^3.0.0" + }, + "dependencies": { + "find-up": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", + "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", + "dev": true, + "requires": { + "locate-path": "^3.0.0" + } + }, + "locate-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", + "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", + "dev": true, + "requires": { + "p-locate": "^3.0.0", + "path-exists": "^3.0.0" + } + }, + "p-locate": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", + "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", + "dev": true, + "requires": { + "p-limit": "^2.0.0" + } + }, + "path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true + } + } + }, "posix-character-classes": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/posix-character-classes/-/posix-character-classes-0.1.1.tgz", @@ -13529,6 +13713,12 @@ "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==", "dev": true }, + "reselect": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/reselect/-/reselect-4.0.0.tgz", + "integrity": "sha512-qUgANli03jjAyGlnbYVAV5vvnOmJnODyABz51RdBN7M4WaVu8mecZWgyQNkG8Yqe3KRGRt0l4K4B3XVEULC4CA==", + "dev": true + }, "resolve": { "version": "1.19.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.19.0.tgz", @@ -13576,15 +13766,6 @@ "integrity": "sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==", "dev": true }, - "rewire": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/rewire/-/rewire-5.0.0.tgz", - "integrity": "sha512-1zfitNyp9RH5UDyGGLe9/1N0bMlPQ0WrX0Tmg11kMHBpqwPJI4gfPpP7YngFyLbFmhXh19SToAG0sKKEFcOIJA==", - "dev": true, - "requires": { - "eslint": "^6.8.0" - } - }, "rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", diff --git a/package.json b/package.json index 3d95831..e142c7b 100644 --- a/package.json +++ b/package.json @@ -26,13 +26,14 @@ }, "devDependencies": { "@types/jest": "^26.0.23", + "babel-jest": "^26.6.3", + "babel-plugin-module-resolver": "^4.1.0", "babel-plugin-rewire": "^1.2.0", "eslint": "^6.8.0", "eslint-config-airbnb": "^18.2.1", "eslint-config-prettier": "^6.8.0", "eslint-plugin-prettier": "^3.4.0", - "jest": "^26.6.3", - "rewire": "^5.0.0" + "jest": "^26.6.3" }, "jest": { "collectCoverage": true,