Skip to content

Commit 174c43e

Browse files
committed
fix(charting): Add dispatching of ModelSetEvent on model update in charting component, add tests PD-3270
1 parent 401fde8 commit 174c43e

2 files changed

Lines changed: 142 additions & 1 deletion

File tree

packages/charting/controller/src/__tests__/index.test.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,139 @@ describe('createCorrectResponseSession', () => {
12071207
});
12081208
});
12091209

1210+
const mockFetchSession = jest.fn();
1211+
const question = {
1212+
addCategoryEnabled: true,
1213+
chartType: 'bar',
1214+
data: [
1215+
{
1216+
interactive: true,
1217+
value: 3,
1218+
label: 'Three',
1219+
},
1220+
{
1221+
interactive: true,
1222+
value: 0,
1223+
label: 'Four',
1224+
},
1225+
{
1226+
interactive: true,
1227+
value: 0,
1228+
label: 'Five',
1229+
},
1230+
],
1231+
correctAnswer: {
1232+
data: [
1233+
{
1234+
value: 3,
1235+
label: 'Three',
1236+
},
1237+
{
1238+
value: 2,
1239+
label: 'Four',
1240+
},
1241+
{
1242+
value: 4,
1243+
label: 'Five',
1244+
},
1245+
{
1246+
value: 0,
1247+
label: 'Six',
1248+
},
1249+
{
1250+
value: 1,
1251+
label: 'Seven',
1252+
},
1253+
{
1254+
value: 1,
1255+
label: 'Eight',
1256+
},
1257+
],
1258+
},
1259+
domain: {
1260+
label: 'Number of Letters',
1261+
},
1262+
graph: {
1263+
height: 500,
1264+
width: 500,
1265+
},
1266+
prompt:
1267+
'<p>Ms. Byrd shows her third grade students how to make a bar graph. She uses the number of letters in some of her students&#39; first names. The students&#39; names she uses for the bar graph are shown below.</p><p><img alt="image 2eade8e8a2fa445dae432c417e8a2731" id="2eade8e8a2fa445dae432c417e8a2731" src="https://storage.googleapis.com/pie-prod-221718-assets/image/cf2c334e-340c-4523-9a47-cb835fddfefe"></p><p>Ms. Byrd starts the bar graph by writing some of the labels.&#160;Finish the bar graph below by dragging the bars to show the correct number of first name letters of all the students and adding labels as needed.</p><p></p><p></p><p></p>',
1268+
range: {
1269+
label: 'Number of Students',
1270+
max: 6,
1271+
labelStep: 0.25,
1272+
step: 0.25,
1273+
min: 0,
1274+
},
1275+
rationale: null,
1276+
title: 'Number of Letters in First Name',
1277+
disabled: false,
1278+
teacherInstructions: null,
1279+
};
1280+
1281+
mockFetchSession.mockResolvedValue({
1282+
answer: [
1283+
{
1284+
interactive: false,
1285+
value: 3,
1286+
label: 'Three',
1287+
},
1288+
{
1289+
value: 2,
1290+
label: 'Four',
1291+
interactive: false,
1292+
},
1293+
{
1294+
value: 4,
1295+
label: 'Five',
1296+
interactive: false,
1297+
},
1298+
{
1299+
interactive: false,
1300+
value: 0.25,
1301+
label: 'six',
1302+
},
1303+
{
1304+
value: 1,
1305+
label: 'seven',
1306+
interactive: false,
1307+
},
1308+
{
1309+
interactive: false,
1310+
value: 1,
1311+
label: 'eight',
1312+
},
1313+
],
1314+
});
1315+
1316+
// Test to check if getScore correctly processes the session data from API
1317+
describe('getScore with API session data', () => {
1318+
it('correctly processes session data from API in evaluate mode', async () => {
1319+
const session = await mockFetchSession();
1320+
const env = { mode: 'evaluate', partialScoring: true };
1321+
const mod = await model(question, session, env);
1322+
1323+
// Simulate the API call and process the data
1324+
const scoreResult = getScore(mod, session, env);
1325+
1326+
// Check if the answers from getScore match the correctedAnswer in mod
1327+
expect(scoreResult.answers).toEqual(mod.correctedAnswer);
1328+
});
1329+
1330+
it('correctly processes session data from API in view mode', async () => {
1331+
const session = await mockFetchSession();
1332+
const env = { mode: 'view', partialScoring: true };
1333+
const mod = await model(question, session, env);
1334+
1335+
// Simulate the API call and process the data
1336+
const scoreResult = getScore(mod, session, env);
1337+
1338+
// Check if the answers from getScore match the correctedAnswer in mod
1339+
expect(scoreResult.answers).toEqual(setCorrectness(mod.correctedAnswer, true));
1340+
});
1341+
});
1342+
12101343
describe('outcome', () => {
12111344
const session = {
12121345
answer: [
@@ -1353,6 +1486,7 @@ describe('outcome', () => {
13531486

13541487
const result = await outcome(mod, session, env);
13551488

1489+
//expect(getScore(mod, session, { mode: 'view', partialScoring: 'true' }).answers).toEqual(mod.correctedAnswer);
13561490
expect(result.score).toEqual(expected);
13571491
},
13581492
);

packages/charting/src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import Main from './main';
4-
import { SessionChangedEvent } from '@pie-framework/pie-player-events';
4+
import { SessionChangedEvent, ModelSetEvent } from '@pie-framework/pie-player-events';
55
import { renderMath } from '@pie-lib/math-rendering';
66

77
export { Main as Component };
@@ -13,6 +13,13 @@ export default class Graphing extends HTMLElement {
1313

1414
set model(m) {
1515
this._model = m;
16+
17+
if (this._session && this.isComplete(this._session.answer)) {
18+
this.dispatchEvent(new ModelSetEvent(this.tagName.toLowerCase(), true, !!this._model));
19+
} else {
20+
this.dispatchEvent(new ModelSetEvent(this.tagName.toLowerCase(), false, !!this._model));
21+
}
22+
1623
this._render();
1724
}
1825

0 commit comments

Comments
 (0)