Skip to content

Commit

Permalink
[TRON-17810] Add unit tests
Browse files Browse the repository at this point in the history
- add unit test for ScatterplotGraph and CFDRenderer
  • Loading branch information
ClaudiaGivan committed Dec 21, 2023
1 parent 844a96f commit d048e34
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/graphs/UIControlsRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default class UIControlsRenderer extends Renderer {

constructor(data) {
super(data);
this.reportingRangeDays = localStorage.getItem('reportingRangeDays') || this.reportingRangeDays;
this.timeInterval = localStorage.getItem('timeInterval') || this.timeInterval;
// this.reportingRangeDays = localStorage.getItem('reportingRangeDays') || this.reportingRangeDays;
// this.timeInterval = localStorage.getItem('timeInterval') || this.timeInterval;
}

/**
Expand Down
48 changes: 24 additions & 24 deletions src/graphs/cfd/CFDRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,9 @@ class CFDRenderer extends UIControlsRenderer {
const cumulativeCountOfWorkItems = this.currentYScale.invert(yPosition);
const excludeCycleTime = eventName === 'scatterplot-mousemove';

const metrics = this.#computeMetrics(date, Math.floor(cumulativeCountOfWorkItems), excludeCycleTime);
const metrics = this.computeMetrics(date, Math.floor(cumulativeCountOfWorkItems), excludeCycleTime);
this.#drawMetricLines(metrics.metricLinesData);
delete metrics.metricLinesData;
const observation = this.observations?.data?.find((o) => o.chart_type === 'CFD' && areDatesEqual(o.date_from, date));
const data = {
date: date,
Expand All @@ -567,13 +569,12 @@ class CFDRenderer extends UIControlsRenderer {

/**
* Computes the CFD metrics for a given date and cumulative count.
* @private
* @param {Date} currentDate - The current date for metrics computation.
* @param {Number} currentCumulativeCount - The current cumulative count of items.
* @param {Boolean} excludeCycleTime
* @returns {Object} The computed metrics.
*/
#computeMetrics(currentDate, currentCumulativeCount, excludeCycleTime = false) {
computeMetrics(currentDate, currentCumulativeCount, excludeCycleTime = false) {
currentDate = new Date(currentDate);
const currentDataEntry = this.data.find((d) => areDatesEqual(new Date(d.date), currentDate));
if (currentDataEntry) {
Expand All @@ -597,16 +598,6 @@ class CFDRenderer extends UIControlsRenderer {
const wip = noOfItemsAfter - noOfItemsBefore;
const throughput = averageLeadTime ? parseFloat((wip / averageLeadTime).toFixed(1)) : undefined;
excludeCycleTime && (averageCycleTime = null);
this.#drawMetricLines(
averageCycleTime,
averageLeadTime,
leadTimeDateBefore,
cycleTimeDateBefore,
currentDate,
currentStateCumulativeCount,
currentDataEntry
);

return {
currentState: this.states[currentStateIndex],
cycleTimeDateBefore: formatDateToLocalString(cycleTimeDateBefore),
Expand All @@ -615,6 +606,15 @@ class CFDRenderer extends UIControlsRenderer {
averageCycleTime,
averageLeadTime,
throughput,
metricLinesData: {
averageCycleTime,
averageLeadTime,
leadTimeDateBefore,
cycleTimeDateBefore,
currentDate,
currentStateCumulativeCount,
currentDataEntry,
},
};
}
return {};
Expand Down Expand Up @@ -650,25 +650,25 @@ class CFDRenderer extends UIControlsRenderer {

/**
* Draws metric lines on the chart for average cycle time, lead time, and work-in-progress (WIP).
*
* @param {number|null} averageCycleTime - The average cycle time.
* @param {number|null} averageLeadTime - The average lead time.
* @param {Date|null} leadTimeDateBefore - The date before the current date for lead time computation.
* @param {Date|null} cycleTimeDateBefore - The date before the current date for cycle time computation.
* @param {Date} currentDate - The current date.
* @param {number} currentStateCumulativeCount - The cumulative count in the current state.
* @param {Object} currentDataEntry - The current data entry object.
* @param {Object} params - The parameters object.
* @param {number|null} params.averageCycleTime - The average cycle time.
* @param {number|null} params.averageLeadTime - The average lead time.
* @param {Date|null} params.leadTimeDateBefore - The date before the current date for lead time computation.
* @param {Date|null} params.cycleTimeDateBefore - The date before the current date for cycle time computation.
* @param {Date} params.currentDate - The current date.
* @param {number} params.currentStateCumulativeCount - The cumulative count in the current state.
* @param {Object} params.currentDataEntry - The current data entry object.
* @private
*/
#drawMetricLines(
#drawMetricLines({
averageCycleTime,
averageLeadTime,
leadTimeDateBefore,
cycleTimeDateBefore,
currentDate,
currentStateCumulativeCount,
currentDataEntry
) {
currentDataEntry,
}) {
if (averageLeadTime) {
this.#drawHorizontalMetricLine(leadTimeDateBefore, currentDate, currentDataEntry.delivered, 'lead-time-line', this.#leadTimeColor, 3);
}
Expand Down
19 changes: 0 additions & 19 deletions test/graphs/CFDGraph.test.js

This file was deleted.

20 changes: 20 additions & 0 deletions test/graphs/cfd/CFDGraph.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import CFDGraph from '../../../src/graphs/cfd/CFDGraph.js';
import graphsTestData from '../../testData/GraphTestData.js';
import cfdGraphOutput from '../../testData/CFDGraphExpectedOutput.js';

describe('CFDGraph', () => {
let cfdGraph;

test('computes dataset correctly', () => {
cfdGraph = new CFDGraph(graphsTestData);
const dataSet = cfdGraph.computeDataSet();
expect(JSON.stringify(dataSet)).toEqual(JSON.stringify(cfdGraphOutput));
});

test('empty data computes to empty dataset', () => {
cfdGraph = new CFDGraph([]);
const dataSet = cfdGraph.computeDataSet();
expect(dataSet).toEqual([]);
});

});
15 changes: 15 additions & 0 deletions test/graphs/cfd/CFDRenderer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import CFDRenderer from '../../../src/graphs/cfd/CFDRenderer.js';
import cfdGraphOutput from '../../testData/CFDGraphExpectedOutput.js';
import metricsOutput from '../../testData/CFDRendererExpectedOutput.js';

describe('CFDRenderer', () => {
let cfdRenderer;

test('computes metrics correctly', () => {
cfdRenderer = new CFDRenderer(cfdGraphOutput);
const date = new Date("2023-04-07T21:00:00.000Z")
const cumulativeCount = 6
const metrics = cfdRenderer.computeMetrics(date,cumulativeCount);
expect(JSON.stringify(metrics)).toEqual(JSON.stringify(metricsOutput));
});
});
21 changes: 21 additions & 0 deletions test/graphs/scatterplot/ScatterplotGraph.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ScatterplotGraph from '../../../src/graphs/scatterplot/ScatterplotGraph.js';
import graphsTestData from '../../testData/GraphTestData.js';
import scatterplotGraphOutput from '../../testData/ScatterplotGraphExpectedOutput.js';

describe('ScatterplotGraph', () => {
let scatterplotGraph;

test('computes dataset correctly', () => {
scatterplotGraph = new ScatterplotGraph(graphsTestData);
const dataSet = scatterplotGraph.computeDataSet();
console.log(JSON.stringify(dataSet))
expect(JSON.stringify(dataSet)).toEqual(JSON.stringify(scatterplotGraphOutput));
});

test('empty data computes to empty dataset', () => {
scatterplotGraph = new ScatterplotGraph([]);
const dataSet = scatterplotGraph.computeDataSet();
expect(dataSet).toEqual([]);
});

});
4 changes: 2 additions & 2 deletions test/testData/CFDGraphExpectedOutput.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const outputData = [
const cfdGraphOutput = [
{
"date": "2023-03-20T22:00:00.000Z",
"analysis_active": 0,
Expand Down Expand Up @@ -226,4 +226,4 @@ const outputData = [
}
]

export default outputData;
export default cfdGraphOutput;
28 changes: 28 additions & 0 deletions test/testData/CFDRendererExpectedOutput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const metricsOutput = {
"currentState": "delivered",
"cycleTimeDateBefore": "April 7, 2023",
"leadTimeDateBefore": "April 6, 2023",
"wip": -6,
"averageCycleTime": 1,
"averageLeadTime": 2,
"throughput": -3,
"metricLinesData": {
"averageCycleTime": 1,
"averageLeadTime": 2,
"leadTimeDateBefore": "2023-04-05T21:00:00.000Z",
"cycleTimeDateBefore": "2023-04-06T21:00:00.000Z",
"currentDate": "2023-04-07T21:00:00.000Z",
"currentStateCumulativeCount": 6,
"currentDataEntry": {
"date": "2023-04-07T21:00:00.000Z",
"analysis_active": 0,
"analysis_done": 0,
"in_progress": 1,
"dev_complete": 0,
"verification_start": 0,
"delivered": 5
}
}
}

export default metricsOutput
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const testData =[
const graphsTestData =[
{
"analysis_active": "1680804888",
"analysis_done": "1680804888",
Expand Down Expand Up @@ -101,4 +101,4 @@ const testData =[
}
]

export default testData;
export default graphsTestData;
33 changes: 33 additions & 0 deletions test/testData/ScatterplotGraphExpectedOutput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const scatterplotGraphOutput = [
{
"delivered": "2023-03-20T22:00:00.000Z",
"noOfDays": 42,
"ticketId": "TRON-13136"
},
{
"delivered": "2023-04-03T21:00:00.000Z",
"noOfDays": 4,
"ticketId": "TRON-13806"
},
{
"delivered": "2023-04-03T21:00:00.000Z",
"noOfDays": 0,
"ticketId": "TRON-13847"
},
{
"delivered": "2023-04-04T21:00:00.000Z",
"noOfDays": 1,
"ticketId": "TRON-13832"
},
{
"delivered": "2023-04-05T21:00:00.000Z",
"noOfDays": 0,
"ticketId": "TRON-13873"
},
{
"delivered": "2023-04-13T21:00:00.000Z",
"noOfDays": 7,
"ticketId": "TRON-12964"
}
]
export default scatterplotGraphOutput;

0 comments on commit d048e34

Please sign in to comment.