Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added getHistoricalTeamsAtWeek #176

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"eslint-plugin-jest": "^24.0.0",
"eslint-plugin-jsdoc": "^30.4.0",
"http-server": "^0.12.3",
"jest": "26.4.2",
"jest": "^26.4.2",
"jsdoc": "^3.6.5",
"q": "^1.5.1",
"webpack": "^4.44.1",
Expand Down
28 changes: 28 additions & 0 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,35 @@ class Client {
));
});
}

/**
* Returns an array of Team object representing each fantasy football team in the FF league.
*
* @param {object} options Required options object.
* @param {number} options.seasonId The season to grab data from. This value must be before 2018
* @param {number} options.scoringPeriodId The scoring period in which to grab teams from.
* @returns {Team[]} The list of teams.
*/
getHistoricalTeamsAtWeek({ seasonId, scoringPeriodId }) {
const route = this.constructor._buildRoute({
base: `${this.leagueId}`,
params: `?scoringPeriodId=${scoringPeriodId}&seasonId=${seasonId}` +
'&view=mMatchupScore&view=mScoreboard&view=mSettings&view=mTopPerformers&view=mTeam'
});

const axiosConfig = this._buildAxiosConfig({
baseURL: 'https://fantasy.espn.com/apis/v3/games/ffl/leagueHistory/'
});

return axios.get(route, axiosConfig).then((response) => {
const data = _.get(response.data, 'teams');
return _.map(data, (team) => (
Team.buildFromServer(team, { leagueId: this.leagueId, seasonId })
));
});

}

/**
* Returns all NFL games that occur in the passed timeframe. NOTE: Date format must be "YYYYMMDD".
*
Expand Down
128 changes: 128 additions & 0 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,134 @@ describe('Client', () => {
});
});


describe('getHistoricalTeamsAtWeek', () => {
let client;
let leagueId;
let scoringPeriodId;
let seasonId;

beforeEach(() => {
leagueId = 213213;
scoringPeriodId = 3;
seasonId = 2017;

client = new Client({ leagueId });

jest.spyOn(axios, 'get').mockImplementation();
});

test('calls axios.get with the correct params', () => {
const routeBase = `${leagueId}`;
const routeParams = `?scoringPeriodId=${scoringPeriodId}&seasonId=${seasonId}&view=mMatchupScore&view=mScoreboard&view=mSettings&view=mTopPerformers&view=mTeam`;
const route = `${routeBase}${routeParams}`;
const config = {};
jest.spyOn(client, '_buildAxiosConfig').mockReturnValue(config);
axios.get.mockReturnValue(q());

client.getHistoricalTeamsAtWeek({ seasonId, scoringPeriodId });
expect(axios.get).toBeCalledWith(route, config);
});

describe('before the promise resolves', () => {
test('does not invoke callback', () => {
jest.spyOn(Team, 'buildFromServer').mockImplementation();
axios.get.mockReturnValue(q());

client.getHistoricalTeamsAtWeek({ seasonId, scoringPeriodId });
expect(Team.buildFromServer).not.toBeCalled();
});
});

describe('after the promise resolves', () => {
test('maps response data into Teams', async () => {
const response = {
data: {
teams: [{
abbrev: 'SWAG',
location: 'First ',
nickname: 'Last',
record: {
overall: {
wins: 3,
losses: 11
}
},
roster: {
entries: [{
playerPoolEntry: {
firstName: 'Joe',
lastName: 'Montana'
}
}]
}
}, {
abbrev: 'JS',
location: 'First ',
nickname: 'Last',
record: {
overall: {
wins: 5,
losses: 11
}
},
roster: {
entries: [{
playerPoolEntry: {
firstName: 'Joe',
lastName: 'Smith'
}
}]
}
}, {
abbrev: 'SWAG',
location: 'First ',
nickname: 'Last',
record: {
overall: {
wins: 11,
losses: 8
}
},
roster: {
entries: [{
playerPoolEntry: {
firstName: 'Joe',
lastName: 'Brown'
}
}]
}
}]
}
};

const promise = q(response);
axios.get.mockReturnValue(promise);

const teams = await client.getHistoricalTeamsAtWeek({ seasonId, scoringPeriodId });

expect.hasAssertions();
expect(teams.length).toBe(3);
_.forEach(teams, (team, index) => {
expect(team).toBeInstanceOf(Team);
expect(team.abbreviation).toBe(response.data.teams[index].abbrev);

expect(team.wins).toBe(response.data.teams[index].record.overall.wins);
expect(team.losses).toBe(response.data.teams[index].record.overall.losses);

expect(team.roster).toEqual(expect.any(Array));
expect(team.roster[0]).toBeInstanceOf(Player);
expect(team.roster[0].firstName).toBe(
response.data.teams[index].roster.entries[0].playerPoolEntry.firstName
);
});
});
});
});




describe('getNFLGamesForPeriod', () => {
let client;
let endDate;
Expand Down