Skip to content

Commit

Permalink
BREAKING: Refactor DraftPlayer to be a subclass of Player
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreiser committed Nov 12, 2023
1 parent 4dd7b57 commit 24cb3df
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 144 deletions.
4 changes: 2 additions & 2 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ describe('Client', () => {
const draftRoute = `${draftRouteBase}${draftRouteParams}`;

const playerRouteBase = `${seasonId}/segments/0/leagues/${leagueId}`;
const playerRouteParams = `?scoringPeriodId=${scoringPeriodId}&view=players_wl`;
const playerRouteParams = `?scoringPeriodId=${scoringPeriodId}&view=kona_player_info`;
const playerRoute = `${playerRouteBase}${playerRouteParams}`;

const config = {};
Expand All @@ -375,7 +375,7 @@ describe('Client', () => {
const draftRoute = `${draftRouteBase}${draftRouteParams}`;

const playerRouteBase = `${seasonId}/segments/0/leagues/${leagueId}`;
const playerRouteParams = '?scoringPeriodId=0&view=players_wl';
const playerRouteParams = '?scoringPeriodId=0&view=kona_player_info';
const playerRoute = `${playerRouteBase}${playerRouteParams}`;

const config = {};
Expand Down
45 changes: 3 additions & 42 deletions src/draft-player/draft-player.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
import _ from 'lodash';

import BaseCacheableObject from '../base-classes/base-cacheable-object/base-cacheable-object';

import {
nflTeamIdToNFLTeam,
nflTeamIdToNFLTeamAbbreviation,
slotCategoryIdToPositionMap
} from '../constants.js';
import Player from '../player/player';

/**
* Represents a player in a draft.
*
* @augments {BaseCacheableObject}
* @augments {Player}
*/
class DraftPlayer extends BaseCacheableObject {
constructor(options = {}) {
super(options);

this.seasonId = options.seasonId;
}

class DraftPlayer extends Player {
static displayName = 'DraftPlayer';

/**
Expand All @@ -43,14 +29,8 @@ class DraftPlayer extends BaseCacheableObject {
* @typedef {object} DraftPlayerMap
*
* @property {number} id The id of the player in the ESPN universe.
* @property {string} name The full name of the player.
* @property {number} teamId The teamId of the fantasy team that drafted the player. Use
* `Client#getTeamAtWeek` to access fantasy team data.
* @property {string} proTeam The NFL team the player is rostered on.
* @property {string} proTeamAbbreviation The NFL team abbreviation the player is rostered on.
* @property {string} defaultPosition The default position in a fantasy roster for the player.
* @property {string[]} eligiblePositions A list of the eligible positions in a fantasy roster the
* player may be slotted in.
*
* @property {number} overallPickNumber The overall pick number
* @property {number} roundNumber The round in which the pick occurred
Expand All @@ -69,26 +49,7 @@ class DraftPlayer extends BaseCacheableObject {
*/
static responseMap = {
id: 'playerId',
name: 'fullName',
teamId: 'teamId',
proTeam: {
key: 'proTeamId',
manualParse: (responseData) => _.get(nflTeamIdToNFLTeam, responseData)
},
proTeamAbbreviation: {
key: 'proTeamId',
manualParse: (responseData) => _.get(nflTeamIdToNFLTeamAbbreviation, responseData)
},
defaultPosition: {
key: 'defaultPositionId',
manualParse: (responseData) => _.get(slotCategoryIdToPositionMap, responseData)
},
eligiblePositions: {
key: 'eligibleSlots',
manualParse: (responseData) => _.map(responseData, (posId) => (
_.get(slotCategoryIdToPositionMap, posId)
))
},

overallPickNumber: 'overallPickNumber',
roundNumber: 'roundId',
Expand Down
100 changes: 0 additions & 100 deletions src/draft-player/draft-player.test.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,6 @@
import _ from 'lodash';

import BaseCacheableObject from '../base-classes/base-cacheable-object/base-cacheable-object.js';

import {
nflTeamIdToNFLTeam,
nflTeamIdToNFLTeamAbbreviation,
slotCategoryIdToPositionMap
} from '../constants.js';

import DraftPlayer from './draft-player.js';

describe('DraftPlayer', () => {
test('extends BaseCacheableObject', () => {
const instance = new DraftPlayer();
expect(instance).toBeInstanceOf(BaseCacheableObject);
});

describe('constructor', () => {
describe('when options are not passed', () => {
const testPropIsUndefined = (prop) => {
test(`${prop} is undefined`, () => {
const newInstance = new DraftPlayer();
expect(_.get(newInstance, prop)).toBeUndefined();
});
};

testPropIsUndefined('seasonId');
});

describe('when options are passed', () => {
const testPropIsSetFromOptions = (prop) => {
test(`${prop} is set from options`, () => {
const value = 25;
const newInstance = new DraftPlayer({ [prop]: value });
expect(_.get(newInstance, prop)).toBe(value);
});
};

testPropIsSetFromOptions('seasonId');
});
});

describe('responseMap', () => {
const buildPlayer = (data, options) => DraftPlayer.buildFromServer(data, options);

describe('proTeam', () => {
describe('manualParse', () => {
test('maps team id to human readable string', () => {
const proTeamId = 22;
const data = { proTeamId };

const player = buildPlayer(data);
expect(player.proTeam).toBe(_.get(nflTeamIdToNFLTeam, proTeamId));
});
});
});

describe('proTeamAbbreviation', () => {
describe('manualParse', () => {
test('maps team id to human readable abbreviation', () => {
const proTeamId = 22;
const data = { proTeamId };

const player = buildPlayer(data);
expect(player.proTeamAbbreviation).toBe(_.get(nflTeamIdToNFLTeamAbbreviation, proTeamId));
});
});
});

describe('defaultPosition', () => {
describe('manualParse', () => {
test('maps id to human readable position', () => {
const defaultPositionId = 2;
const data = { defaultPositionId };

const player = buildPlayer(data);
expect(player.defaultPosition).toBe(
_.get(slotCategoryIdToPositionMap, defaultPositionId)
);
});
});
});

describe('eligiblePositions', () => {
describe('manualParse', () => {
test('maps ids to positions', () => {
const eligibleSlots = [0, 1, 2];
const data = { eligibleSlots };

const player = buildPlayer(data);

expect.hasAssertions();
_.forEach(player.eligiblePositions, (position, index) => {
expect(position).toBe(
_.get(slotCategoryIdToPositionMap, eligibleSlots[index])
);
});
});
});
});
});

describe('class methods', () => {
describe('getIDParams', () => {
const testReturnsUndefined = ({ playerId, seasonId }) => {
Expand Down

0 comments on commit 24cb3df

Please sign in to comment.