Skip to content

Commit 6195a7e

Browse files
authored
Merge pull request #1 from srmukher/users/srmukher/TestModule
Unit test for Sparkline Chart using Rewire
2 parents 4fc9532 + e544074 commit 6195a7e

File tree

5 files changed

+4081
-0
lines changed

5 files changed

+4081
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const script = require('../script')
2+
const sparkline1Points = {
3+
chartTitle: 'Sparkline chart',
4+
lineChartData: [
5+
{
6+
legend: '19.64',
7+
color: '#00AA00',
8+
data: [
9+
{
10+
x: 1,
11+
y: 58.13,
12+
},
13+
{
14+
x: 2,
15+
y: 140.98,
16+
},
17+
{
18+
x: 3,
19+
y: 20,
20+
},
21+
{
22+
x: 4,
23+
y: 89.7,
24+
},
25+
{
26+
x: 5,
27+
y: 99,
28+
},
29+
{
30+
x: 6,
31+
y: 13.28,
32+
},
33+
{
34+
x: 7,
35+
y: 31.32,
36+
},
37+
{
38+
x: 8,
39+
y: 10.21,
40+
},
41+
],
42+
},
43+
],
44+
};
45+
46+
describe('Unit test _isChartEmpty function', () => {
47+
test('Should return false when the chart is not empty', () => {
48+
const rewire = require('rewire');
49+
const SparklineBaseModule = rewire('./SparklineBase.js');
50+
const SparklineBase = SparklineBaseModule.__get__('SparklineBase');
51+
const instance = new SparklineBase({data: sparkline1Points, width: 100, height: 100, _valueTextWidth: 100});
52+
const result = instance._isChartEmpty();
53+
expect(result).toBe(false);
54+
});
55+
test('Should return true when the chart is empty', () => {
56+
const rewire = require('rewire');
57+
const SparklineBaseModule = rewire('./SparklineBase.js');
58+
const SparklineBase = SparklineBaseModule.__get__('SparklineBase');
59+
const instance = new SparklineBase({data: [], width: 100, height: 100, _valueTextWidth: 100});
60+
const result = instance._isChartEmpty();
61+
expect(result).toBe(true);
62+
});
63+
});

tools/UnitTestApp/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@fluentui/fluentui-charting-contrib-unit-test-app",
3+
"version": "1.0",
4+
"description": "Unit test app for Fluent UI Charting",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/microsoft/fluentui-charting-contrib/tools/UnitTestApp"
8+
},
9+
"license": "MIT",
10+
"scripts": {
11+
"test": "jest"
12+
},
13+
"devDependencies": {
14+
"jest": "29.7.0",
15+
"@types/jest": "29.5.5",
16+
"@testing-library/dom": "8.11.3",
17+
"@testing-library/jest-dom": "5.16.5",
18+
"@testing-library/react": "12.1.2",
19+
"react": "17.0.2",
20+
"react-dom": "17.0.2",
21+
"react-test-renderer": "17.0.2",
22+
"@types/react": "17.0.44",
23+
"@types/react-dom": "17.0.15",
24+
"@types/react-test-renderer": "17.0.2",
25+
"@types/react-addons-test-utils": "0.14.18",
26+
"fs-extra": "8.1.0",
27+
"@types/fs-extra": "8.0.1",
28+
"rewire": "^4.0.1",
29+
"@types/rewire": "^2.5.28",
30+
"@fluentui/react-charting": "5.18.17"
31+
},
32+
"exports": {
33+
"./README.md": "./README.md"
34+
}
35+
}

tools/UnitTestApp/script.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const fs = require('fs');
2+
const readFilePath = './node_modules/@fluentui/react-charting/lib-commonjs/components/Sparkline/Sparkline.base.js';
3+
const writeFilePath = './Sparkline/SparklineBase.js';
4+
// Read the file
5+
const updatedFile = () => {
6+
fs.readFile(readFilePath, 'utf8', (err, data) => {
7+
if (err) {
8+
console.error(`Error reading file from disk: ${err}`);
9+
} else {
10+
// Replace the words
11+
let result = data.replace(/require\("d3-scale"\)/g, "import('d3-scale')");
12+
result = result.replace(/require\("d3-shape"\)/g, "import('d3-shape')");
13+
result = result.replace(/require\("d3-array"\)/g, "import('d3-array')");
14+
// Write the file back
15+
fs.writeFile(writeFilePath, result, 'utf8', err => {
16+
if (err) {
17+
console.error(`Error writing file to disk: ${err}`);
18+
}
19+
});
20+
}
21+
});
22+
};
23+
24+
updatedFile();

0 commit comments

Comments
 (0)