(dev) remove rewire, add babel-plugin-rewire. (#44)

* removed rewire. now using babel-rewire instead

* implemented more tests

* changed some eslint params

* linting

* linting

* coverage on getChant

Co-authored-by: Marcus Netz <marcus.netz@godyo.com>
This commit is contained in:
2021-05-06 12:52:33 +02:00
committed by GitHub
parent 4fa2dc7ab7
commit 79c95cea97
14 changed files with 807 additions and 311 deletions

View File

@ -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(/.*/);
});
});