Skip to content

Commit 5bd558a

Browse files
committed
Fixed a problem with milestone-report-data throwing an error because we changed the data from an object to an array. #2896
1 parent d3c121a commit 5bd558a

2 files changed

Lines changed: 138 additions & 16 deletions

File tree

Lines changed: 103 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,112 @@
11
import { MilestoneReportDataComponent } from './milestone-report-data.component';
22

3+
let comp: any = null;
4+
const nodeId1: string = 'node1';
5+
const nodeId2: string = 'node2';
6+
const componentId1: string = 'component1';
7+
const componentId2: string = 'component2';
8+
const counts1: any = { 1: 0, 2: 0, 3: 1, 4: 1, 5: 0 };
9+
const counts2: any = { 1: 0, 2: 0, 3: 0, 4: 2, 5: 0 };
10+
const average1: number = 3.5;
11+
const average2: number = 4;
12+
const scoreCount1: number = 2;
13+
const scoreCount2: number = 2;
14+
const scoreSum1: number = 7;
15+
const scoreSum2: number = 8;
16+
const stepTitle1: string = '1.1: Milestone Step 1';
17+
const stepTitle2: string = '1.2: Milestone Step 2';
18+
const componentData1: any = createComponentData(
19+
nodeId1,
20+
componentId1,
21+
counts1,
22+
average1,
23+
scoreCount1,
24+
scoreSum1,
25+
stepTitle1
26+
);
27+
const componentData2: any = createComponentData(
28+
nodeId2,
29+
componentId2,
30+
counts2,
31+
average2,
32+
scoreCount2,
33+
scoreSum2,
34+
stepTitle2
35+
);
36+
337
describe('MilestoneReportDataComponent', () => {
4-
it('#getPercent() should return percentage', () => {
5-
const comp = new MilestoneReportDataComponent();
6-
comp.data = { scoreCount: 5 };
38+
beforeEach(() => {
39+
comp = new MilestoneReportDataComponent();
40+
comp.data = [componentData1, componentData2];
41+
});
42+
43+
getPercent();
44+
getCount();
45+
getAverage();
46+
getScoreValuesCount();
47+
getComponentData();
48+
});
49+
50+
function getPercent(): void {
51+
it('getPercent() should return percentage', () => {
752
expect(comp.getPercent()).toEqual('100%');
853
});
54+
}
955

10-
it('#getScoreValuesCount() should count for current score value', () => {
11-
const comp = new MilestoneReportDataComponent();
56+
function getCount(): void {
57+
it('getCount() should get the count when scoreValues is not specified', () => {
58+
expect(comp.getCount()).toEqual(2);
59+
});
60+
61+
it('getCount() should get the count when scoreValues is specified', () => {
1262
comp.scoreValues = [5];
13-
comp.data = {
14-
average: 4.3333333333333,
15-
counts: { 1: 0, 2: 0, 3: 1, 4: 0, 5: 2 },
16-
scoreCount: 3,
17-
scoreSum: 13
18-
};
63+
expect(comp.getCount()).toEqual(0);
64+
});
65+
}
66+
67+
function getAverage(): void {
68+
it('getAverage() should get the average', () => {
69+
expect(comp.getAverage()).toEqual(4);
70+
});
71+
}
72+
73+
function getScoreValuesCount(): void {
74+
it('getScoreValuesCount() should get the count for the specified score value', () => {
75+
comp.scoreValues = [4];
1976
expect(comp.getScoreValuesCount()).toEqual(2);
2077
});
21-
});
78+
}
79+
80+
function getComponentData(): void {
81+
it(`getComponentData() should get the last component data if nodeId and componentId are not
82+
specified`, () => {
83+
expect(comp.getComponentData()).toEqual(componentData2);
84+
});
85+
86+
it(`getComponentData() should get the component data if nodeId and componentId are
87+
specified`, () => {
88+
comp.nodeId = nodeId1;
89+
comp.componentId = componentId1;
90+
expect(comp.getComponentData()).toEqual(componentData1);
91+
});
92+
}
93+
94+
function createComponentData(
95+
nodeId: string,
96+
componentId: string,
97+
counts: any,
98+
average: number,
99+
scoreCount: number,
100+
scoreSum: number,
101+
stepTitle: string
102+
): any {
103+
return {
104+
nodeId: nodeId,
105+
componentId: componentId,
106+
average: average,
107+
counts: counts,
108+
scoreCount: scoreCount,
109+
scoreSum: scoreSum,
110+
stepTitle: stepTitle
111+
};
112+
}

src/main/webapp/site/src/app/teacher/milestone/milestone-report-data/milestone-report-data.component.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { Component, Input } from '@angular/core';
55
template: `{{ output }}`
66
})
77
export class MilestoneReportDataComponent {
8+
@Input()
9+
nodeId: string = '';
10+
11+
@Input()
12+
componentId: string = '';
13+
814
@Input()
915
calc: string;
1016

@@ -30,30 +36,55 @@ export class MilestoneReportDataComponent {
3036
this.output = this.getCount();
3137
}
3238
if (this.calc === 'average') {
33-
this.output = this.data.average;
39+
this.output = this.getAverage();
3440
}
3541
}
3642

3743
getPercent() {
3844
const count = this.getCount();
39-
const total = this.data.scoreCount;
45+
const total = this.getComponentData().scoreCount;
4046
return `${Math.round((count / total) * 100)}%`;
4147
}
4248

4349
getCount() {
4450
if (this.scoreValues && this.scoreValues.length) {
4551
return this.getScoreValuesCount();
4652
}
47-
return this.data.scoreCount;
53+
return this.getComponentData().scoreCount;
54+
}
55+
56+
getAverage() {
57+
return this.getComponentData().average;
4858
}
4959

5060
getScoreValuesCount() {
5161
let count = 0;
52-
for (const [key, value] of Object.entries(this.data.counts)) {
62+
for (const [key, value] of Object.entries(this.getComponentData().counts)) {
5363
if (this.scoreValues.includes(Number(key))) {
5464
count += Number(value);
5565
}
5666
}
5767
return count;
5868
}
69+
70+
getComponentData(
71+
data: any[] = this.data,
72+
nodeId: string = this.nodeId,
73+
componentId: string = this.componentId
74+
): any {
75+
if (nodeId === '' && componentId === '') {
76+
return data[data.length - 1];
77+
} else {
78+
return this.getComponentDataByNodeIdAndComponentId(data, nodeId, componentId);
79+
}
80+
}
81+
82+
getComponentDataByNodeIdAndComponentId(data: any[], nodeId: string, componentId: string): any {
83+
for (const componentData of data) {
84+
if (componentData.nodeId === nodeId && componentData.componentId === componentId) {
85+
return componentData;
86+
}
87+
}
88+
return null;
89+
}
5990
}

0 commit comments

Comments
 (0)