Skip to content

Commit

Permalink
Add test suite for license decoration
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-At-Home committed Jul 16, 2020
1 parent 9d28194 commit 7649ab5
Show file tree
Hide file tree
Showing 3 changed files with 460 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/utils/stats/LineupDisplayUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ export class LineupDisplayUtils {
colorField: string,
decorateLineup: boolean
) {
const tooltipTexts = _.flatMap(sortedLineup, (cid: {id: string, code: string}) => {
return LineupDisplayUtils.buildTooltipText(cid, perLineupPlayerMap, positionFromPlayerKey);
});
const tooltip = <Tooltip id={`${key}_info`}>{_.map(tooltipTexts,
(t: string, i: number) => <span key={"" + i}>{t}<br/></span>
)}</Tooltip>;
const tooltip = LineupDisplayUtils.buildTooltipTexts(
key, sortedLineup, perLineupPlayerMap, positionFromPlayerKey
);
if (decorateLineup) {
return sortedLineup.map((cid: { code: string, id: string }, pid: number) => {
return <OverlayTrigger placement="auto" overlay={tooltip} key={"" + pid}>{
Expand All @@ -44,7 +41,24 @@ export class LineupDisplayUtils {
}
}

static buildTooltipText(
/** Builds a tooltip element for the entire lineup */
static buildTooltipTexts(
key: string,
sortedLineup: { code: string, id: string }[],
perLineupPlayerMap: Record<string, Record<string, any>>,
positionFromPlayerKey: Record<string, {posClass: string}>
) {
const tooltipTexts = _.flatMap(sortedLineup, (cid: {id: string, code: string}) => {
return LineupDisplayUtils.buildTooltipText(cid, perLineupPlayerMap, positionFromPlayerKey);
});
const tooltip = <Tooltip id={`${key}_info`}>{_.map(tooltipTexts,
(t: string, i: number) => <span key={"" + i}>{t}<br/></span>
)}</Tooltip>;

return tooltip;
}

private static buildTooltipText(
cid: { code: string, id: string },
perLineupPlayerMap: Record<string, Record<string, any>>,
positionFromPlayerKey: Record<string, {posClass: string}>
Expand All @@ -68,10 +82,10 @@ export class LineupDisplayUtils {
colorField: string,
finalPlayer: boolean
) {
const fontWeight = (id: string) => {
const fontWeight = (playerInfo: Record<string, any>) => {
const usage = _.max(
[ 0.10, _.min(
[ perLineupPlayerMap[id]?.off_usage?.value || 0.20, 0.35 ]
[ playerInfo.off_usage?.value || 0.20, 0.35 ]
)]
);
return 100*_.round((usage < 0.20) ? //10 == 100 weight
Expand All @@ -88,16 +102,16 @@ export class LineupDisplayUtils {
};
};

const singleColorField = (id: string, field: string) => {
const val = perLineupPlayerMap[id]?.[field]?.value;
const singleColorField = (playerInfo: Record<string, any>, field: string) => {
const val = playerInfo[field]?.value;
const color = colorChooser(field)(val) + "80"; //(opacity at the end)
return color;
};

const buildBadges = (id: string) => {
const _3pr = perLineupPlayerMap[id]?.off_3pr?.value;
const ftr = perLineupPlayerMap[id]?.off_ftr?.value;
const assist = perLineupPlayerMap[id]?.off_assist?.value;
const buildBadges = (playerInfo: Record<string, any>) => {
const _3pr = playerInfo.off_3pr?.value;
const ftr = playerInfo.off_ftr?.value;
const assist = playerInfo.off_assist?.value;
return <span style={{}}>
{ _3pr <= 0.05 ? <sup className="megaTwoPointBadge"></sup> : null }
{ _3pr >= 0.05 && _3pr < 0.2 ? <sup className="twoPointBadge"></sup> : null }
Expand All @@ -110,19 +124,20 @@ export class LineupDisplayUtils {
</span>;
};

const playerInfo = perLineupPlayerMap[cid.id];
return <span key={cid.code}>
<span style={{whiteSpace: 'nowrap'}}><Badge variant="light"
style={{
backgroundColor: singleColorField(cid.id, colorField)
backgroundColor: singleColorField(playerInfo, colorField)
// consider this in the future:
// background: `linear-gradient(to right, ${singleColorField(cid.id, colorField)}, white, ${singleColorField(cid.id, "def_adj_rtg")})`
}}>
<span style={{
fontSize: "small",
fontWeight: fontWeight(cid.id)
fontWeight: fontWeight(playerInfo)
}}>{cid.code}</span>
</Badge>
{buildBadges(cid.id)}</span>
{buildBadges(playerInfo)}</span>
{finalPlayer ? null : <span style={{opacity: 0}}> ; </span> }
</span>;
}
Expand Down
89 changes: 89 additions & 0 deletions src/utils/stats/__tests__/LineupDisplayUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import renderer from 'react-test-renderer';
import React from 'react';
import { LineupDisplayUtils } from '../LineupDisplayUtils';
import { shallow } from 'enzyme'
import toJson from 'enzyme-to-json'

describe("LineupDisplayUtils", () => {

const testLineup = [
{ code: "p1", id: "player1" },
{ code: "p2", id: "player2" },
{ code: "p3", id: "player3" },
{ code: "p4", id: "player4" },
{ code: "p5", id: "player5" },
];
const positionFromPlayerKey = { //(just used for display)
"player1": { posClass: "p-PG" },
"player2": { posClass: "CG" },
"player3": { posClass: "WG" },
"player4": { posClass: "WF" },
"player5": { posClass: "PF/C" },
};
const perLineupPlayerMap = {
"player1": { // *3* A
off_rtg: { value: 101 }, off_adj_rtg: { value : 0.5 }, //(rtg just used for display)
off_usage: { value: 0.1 }, off_assist: { value: 0.20 },
off_3pr: { value: 0.8 }, off_ftr: { value: 0.1 },
def_orb: { value: 0.05 }, //(just used for display)
},
"player2": { // *A* *F*
off_rtg: { value: 102 }, off_adj_rtg: { value : 1.0 }, //(rtg just used for display)
off_usage: { value: 0.15 }, off_assist: { value: 0.30 },
off_3pr: { value: 0.3 }, off_ftr: { value: 0.9 },
def_orb: { value: 0.10 }, //(just used for display)
},
"player3": { // *2*
off_rtg: { value: 103 }, off_adj_rtg: { value : 1.5 }, //(rtg just used for display)
off_usage: { value: 0.20 }, off_assist: { value: 0.15 },
off_3pr: { value: 0.01 }, off_ftr: { value: 0.3 },
def_orb: { value: 0.15 }, //(just used for display)
},
"player4": { // 2 F
off_rtg: { value: 104 }, off_adj_rtg: { value : 2.0 }, //(rtg just used for display)
off_usage: { value: 0.25 }, off_assist: { value: 0.15 },
off_3pr: { value: 0.15 }, off_ftr: { value: 0.5 },
def_orb: { value: 0.20 }, //(just used for display)
},
"player5": { // 3 A F
off_rtg: { value: 105 }, off_adj_rtg: { value : 2.5 }, //(rtg just used for display)
off_usage: { value: 0.30 }, off_assist: { value: 0.20 },
off_3pr: { value: 0.50 }, off_ftr: { value: 0.5 },
def_orb: { value: 0.25 }, //(just used for display)
},
}

test("buildDecoratedLineup - plain version", () => {
const component = renderer.create(
<span>{
LineupDisplayUtils.buildDecoratedLineup(
"p1_p2_p3_p4_p5", testLineup, perLineupPlayerMap, positionFromPlayerKey, "off_adj_rtg", false
)
}</span>
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
test("buildDecoratedLineup - decorated version", () => {
const component = renderer.create(
<span>{
LineupDisplayUtils.buildDecoratedLineup(
"p1_p2_p3_p4_p5", testLineup, perLineupPlayerMap, positionFromPlayerKey, "off_adj_rtg", true
)
}</span>
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
test("buildDecoratedLineup - buildTooltipTexts", () => {
const component = renderer.create(
<span>{
LineupDisplayUtils.buildTooltipTexts(
"p1_p2_p3_p4_p5", testLineup, perLineupPlayerMap, positionFromPlayerKey
)
}</span>
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});
Loading

0 comments on commit 7649ab5

Please sign in to comment.