Skip to content

Commit

Permalink
feat(getskills): add getSkills method
Browse files Browse the repository at this point in the history
  • Loading branch information
trezy committed Aug 30, 2021
1 parent e3fe5f4 commit ad87b0e
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 219 deletions.
6 changes: 5 additions & 1 deletion src/api/getEntities.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export async function getEntities(options) {
} = options
let { ids } = options

if (typeof type !== 'string') {
throw new TypeError('type must be a string')
}

// If no ids provided, get all entities
if (typeof ids === 'undefined') {
ids = await getDirectory(type, patch)
Expand All @@ -48,6 +52,6 @@ export async function getEntities(options) {

// If ids is an array of strings, get
return Promise.all(ids.map(filename => {
return getFile(`${type}/${filename}`)
return getFile(`${type}/${filename}`, patch)
}))
}
8 changes: 1 addition & 7 deletions src/getHeldItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ export async function getHeldItems(options = {}) {
throw new TypeError('options must be an object')
}

const {
ids,
patch,
} = options

return getEntities({
ids,
patch,
...options,
type: 'held-items',
})
}
8 changes: 1 addition & 7 deletions src/getPokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ export async function getPokemon(options = {}) {
throw new TypeError('options must be an object')
}

const {
ids,
patch,
} = options

return getEntities({
ids,
patch,
...options,
type: 'pokemon',
})
}
27 changes: 27 additions & 0 deletions src/getSkills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Local imports
import './models/Skill.js'
import { getEntities } from './api/index.js'





/**
* Returns data for held items
*
* @param {Object} [options] An object containing filtering options
* @param {string[]} [options.ids] Array of item IDs to be returned
* @param {string} [options.patch] Maximum patch version to return data for
*
* @returns {Promise<Skill[]>} An array containing data for each item requested
*/
export async function getSkills(options = {}) {
if ((typeof options !== 'object') || Array.isArray(options)) {
throw new TypeError('options must be an object')
}

return getEntities({
...options,
type: 'skills',
})
}
12 changes: 12 additions & 0 deletions src/models/Skill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* A Pokémon's special ability
*
* @typedef {Object} Skill
* @property {number} cooldown The amount of time the ability requires to recharge after use
* @property {string} description A description of the ability
* @property {string} displayName The display name of the ability
* @property {string} id The skill's ID, prefixed by the name of the Pokémon to whom the skill belongs
* @property {string[]} requires A list of requirements that must be met for a Pokémon to be allowed to have the skill
* @property {number} slot The slot in which this ability can be set
* @property {number} type The attack category of this skill
*/
248 changes: 226 additions & 22 deletions test/api/getEntities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,236 @@ describe('getEntities', function () {
expect(getEntities).to.be.a('function')
})

it('given an array of IDs, returns each entity in the array', async () => {
const IDS = ['aeos-cookie', 'buddy-barrier']
const ENTITY_DATA = IDS.map(id => mockData.heldItems[id])
const RESPONSE = await getEntities({
ids: IDS,
type: 'held-items',
describe('with valid arguments', () => {
describe('options.ids', () => {
it('given an array of Pokémon IDs, returns each Pokémon in the array', async () => {
const POKEMON_IDS = ['crustle', 'zeraora']
const POKEMON_DATA = POKEMON_IDS.map(id => mockData.pokemon[id])
const RESPONSE = await getEntities({
ids: POKEMON_IDS,
type: 'pokemon',
})

expect(RESPONSE).to.be.an('array')
RESPONSE.forEach((item, index) => {
expect(item).to.deep.equal(POKEMON_DATA[index])
})
})
})

expect(RESPONSE).to.be.an('array')
RESPONSE.forEach((item, index) => {
expect(item).to.deep.equal(ENTITY_DATA[index])
xdescribe('options.patch', () => {
it('given a patch version, returns only Pokémon that would be available in that patch', async () => {
// const POKEMON_IDS = ['crustle', 'zeraora']
// const POKEMON_DATA = POKEMON_IDS.map(id => mockData.pokemon[id])
// const RESPONSE = await getEntities({ ids: POKEMON_IDS })

// expect(RESPONSE).to.be.an('array')
// RESPONSE.forEach((item, index) => {
// expect(item).to.deep.equal(POKEMON_DATA[index])
// })
})
})
})

it('given anything other than an array, throws an error', async () => {
const errorStuff = [TypeError, 'First argument must be an array of strings.']

// @ts-ignore
expect(getEntities(undefined)).to.eventually.throw(...errorStuff)
// @ts-ignore
expect(getEntities(null)).to.eventually.throw(...errorStuff)
// @ts-ignore
expect(getEntities(456)).to.eventually.throw(...errorStuff)
// @ts-ignore
expect(getEntities({})).to.eventually.throw(...errorStuff)
// @ts-ignore
expect(getEntities('mr-mime')).to.eventually.throw(...errorStuff)
describe('with invalid arguments', () => {
describe('given no options', () => {
it('throws an error', async () => {
const errorStuff = [TypeError, 'options must be an object']

// @ts-expect-error
expect(getEntities()).to.eventually.throw(...errorStuff)
})
})

describe('given `null` as options', () => {
it('throws an error', async () => {
const errorStuff = [TypeError, 'options must be an object']

// @ts-expect-error
expect(getEntities(null)).to.eventually.throw(...errorStuff)
})
})

describe('given an array as options', () => {
it('throws an error', async () => {
const errorStuff = [TypeError, 'options must be an object']

// @ts-expect-error
expect(getEntities([])).to.eventually.throw(...errorStuff)
})
})

describe('given a number as options', () => {
it('throws an error', async () => {
const errorStuff = [TypeError, 'options must be an object']

// @ts-expect-error
expect(getEntities(12345)).to.eventually.throw(...errorStuff)
})
})

describe('given a string as options', () => {
const errorStuff = [TypeError, 'options must be an object']

it('throws an error', async () => {
// @ts-expect-error
expect(getEntities('mr-mime')).to.eventually.throw(...errorStuff)
})
})

describe('given an object as options', () => {
describe('options.type', () => {
describe('given no type', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'type must be a string']

// @ts-expect-error
expect(getEntities({})).to.eventually.throw(...errorStuff)
})
})

describe('given `null` as type', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'type must be a string']

// @ts-expect-error
expect(getEntities({ type: null })).to.eventually.throw(...errorStuff)
})
})

describe('given an object as type', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'type must be a string']

// @ts-expect-error
expect(getEntities({ type: {} })).to.eventually.throw(...errorStuff)
})
})

describe('given an array as type', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'type must be a string']

// @ts-expect-error
expect(getEntities({ type: [] })).to.eventually.throw(...errorStuff)
})
})

describe('given a number as type', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'type must be a string']

// @ts-expect-error
expect(getEntities({ type: 12345 })).to.eventually.throw(...errorStuff)
})
})
})

describe('options.ids', () => {
describe('given `null`', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'ids must be an array of strings']

expect(getEntities({
ids: null,
type: 'pokemon',
// @ts-expect-error
})).to.eventually.throw(...errorStuff)
})
})

describe('given an object', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'ids must be an array of strings']

expect(getEntities({
// @ts-expect-error
ids: {},
type: 'pokemon',
// @ts-expect-error
})).to.eventually.throw(...errorStuff)
})
})

describe('given a string', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'ids must be an array of strings']

expect(getEntities({
// @ts-expect-error
ids: 'foo',
type: 'pokemon',
// @ts-expect-error
})).to.eventually.throw(...errorStuff)
})
})

describe('given a number', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'ids must be an array of strings']

expect(getEntities({
// @ts-expect-error
ids: 12345,
type: 'pokemon',
// @ts-expect-error
})).to.eventually.throw(...errorStuff)
})
})

describe('given an array', () => {
describe('of `null`', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'ids must be an array of strings']

expect(getEntities({
ids: [null],
type: 'pokemon',
// @ts-expect-error
})).to.eventually.throw(...errorStuff)
})
})

describe('of objects', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'ids must be an array of strings']

expect(getEntities({
// @ts-expect-error
ids: [{}],
type: 'pokemon',
// @ts-expect-error
})).to.eventually.throw(...errorStuff)
})
})

describe('of arrays', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'ids must be an array of strings']

expect(getEntities({
// @ts-expect-error
ids: [[]],
type: 'pokemon',
// @ts-expect-error
})).to.eventually.throw(...errorStuff)
})
})

describe('of numbers', () => {
it('throws an error', () => {
const errorStuff = [TypeError, 'ids must be an array of strings']

expect(getEntities({
// @ts-expect-error
ids: [12345],
type: 'pokemon',
// @ts-expect-error
})).to.eventually.throw(...errorStuff)
})
})
})
})
})
})
})
Loading

0 comments on commit ad87b0e

Please sign in to comment.