Skip to content

Commit b70e3cb

Browse files
Merge pull request #158 from redbearsam/feature/first-ui-unit-test
Implemented an example UI/D3 based unit test
2 parents d84ef26 + 7fabfb1 commit b70e3cb

File tree

5 files changed

+152
-2
lines changed

5 files changed

+152
-2
lines changed

packages/perspective-viewer-d3fc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"test:build": "cpx \"test/html/*\" build",
2020
"watch": "webpack --color --watch --config src/config/d3fc.watch.config.js",
2121
"test:run": "jest --silent --color 2>&1",
22+
"test:unit": "jest --config=test/js/jest.unit.config.js --color --watch",
2223
"test": "npm-run-all test:build test:run",
2324
"clean": "rimraf build && rimraf cjs",
2425
"clean:screenshots": "rimraf \"screenshots/**/*.@(failed|diff).png\""

packages/perspective-viewer-d3fc/src/js/tooltip/generateHTML.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ function addDataValues(tooltipDiv, values) {
3030
});
3131
}
3232

33-
const formatNumber = value => (typeof value === "number" ? value.toLocaleString(undefined, {style: "decimal"}) : value);
33+
const formatNumber = value => value.toLocaleString(undefined, {style: "decimal"});

packages/perspective-viewer-d3fc/src/js/tooltip/selectionData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function getDataValues(data, settings) {
4646
return [
4747
{
4848
name: settings.mainValues[0].name,
49-
value: data.colorValue || data.mainValue - data.baseValue || data.mainValue
49+
value: toValue(settings.mainValues[0].type, data.colorValue || data.mainValue - data.baseValue || data.mainValue)
5050
}
5151
];
5252
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/******************************************************************************
2+
*
3+
* Copyright (c) 2017, the Perspective Authors.
4+
*
5+
* This file is part of the Perspective library, distributed under the terms of
6+
* the Apache License 2.0. The full license can be found in the LICENSE file.
7+
*
8+
*/
9+
10+
module.exports = {
11+
roots: ["unit"],
12+
verbose: true,
13+
testURL: "http://localhost/",
14+
automock: false,
15+
transform: {
16+
".js$": "./transform.js"
17+
},
18+
setupFiles: ["./beforeEachSpec.js"]
19+
};
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/******************************************************************************
2+
*
3+
* Copyright (c) 2017, the Perspective Authors.
4+
*
5+
* This file is part of the Perspective library, distributed under the terms of
6+
* the Apache License 2.0. The full license can be found in the LICENSE file.
7+
*
8+
*/
9+
import {select} from "d3";
10+
import {generateHtml} from "../../../../src/js/tooltip/generateHTML";
11+
12+
describe("tooltip should", () => {
13+
let tooltip = null;
14+
let settings = null;
15+
16+
beforeEach(() => {
17+
tooltip = select("body")
18+
.append("div")
19+
.classed("tooltip-test", true);
20+
tooltip.append("div").attr("id", "tooltip-values");
21+
22+
settings = {
23+
crossValues: [],
24+
splitValues: [],
25+
mainValues: [{name: "main-1", type: "integer"}]
26+
};
27+
});
28+
afterEach(() => {
29+
tooltip.remove();
30+
});
31+
32+
const getContent = () => {
33+
const content = [];
34+
tooltip.selectAll("li").each((d, i, nodes) => {
35+
content.push(select(nodes[i]).text());
36+
});
37+
return content;
38+
};
39+
40+
test("show single mainValue", () => {
41+
const data = {
42+
mainValue: 101
43+
};
44+
generateHtml(tooltip, data, settings);
45+
expect(getContent()).toEqual(["main-1: 101"]);
46+
});
47+
48+
test("show multiple mainValues", () => {
49+
settings.mainValues.push({name: "main-2", type: "float"});
50+
const data = {
51+
mainValues: [101, 202.22]
52+
};
53+
generateHtml(tooltip, data, settings);
54+
expect(getContent()).toEqual(["main-1: 101", "main-2: 202.22"]);
55+
});
56+
57+
test("format mainValue as date", () => {
58+
settings.mainValues[0].type = "datetime";
59+
const testDate = new Date("2019-04-03T15:15Z");
60+
const data = {
61+
mainValue: testDate.getTime()
62+
};
63+
generateHtml(tooltip, data, settings);
64+
expect(getContent()).toEqual([`main-1: ${testDate.toLocaleString()}`]);
65+
});
66+
67+
test("format mainValue as integer", () => {
68+
settings.mainValues[0].type = "integer";
69+
const data = {
70+
mainValue: 12345.6789
71+
};
72+
generateHtml(tooltip, data, settings);
73+
expect(getContent()).toEqual(["main-1: 12,345"]);
74+
});
75+
76+
test("format mainValue as decimal", () => {
77+
settings.mainValues[0].type = "float";
78+
const data = {
79+
mainValue: 12345.6789
80+
};
81+
generateHtml(tooltip, data, settings);
82+
expect(getContent()).toEqual(["main-1: 12,345.679"]);
83+
});
84+
85+
test("show with single crossValue", () => {
86+
settings.crossValues.push({name: "cross-1", type: "string"});
87+
const data = {
88+
crossValue: "tc-1",
89+
mainValue: 101
90+
};
91+
92+
generateHtml(tooltip, data, settings);
93+
expect(getContent()).toEqual(["cross-1: tc-1", "main-1: 101"]);
94+
});
95+
96+
test("show with multiple crossValues", () => {
97+
settings.crossValues.push({name: "cross-1", type: "string"});
98+
settings.crossValues.push({name: "cross-2", type: "integer"});
99+
const data = {
100+
crossValue: "tc-1|1001",
101+
mainValue: 101
102+
};
103+
104+
generateHtml(tooltip, data, settings);
105+
expect(getContent()).toEqual(["cross-1: tc-1", "cross-2: 1,001", "main-1: 101"]);
106+
});
107+
108+
test("show with single splitValue", () => {
109+
settings.splitValues.push({name: "split-1", type: "string"});
110+
const data = {
111+
key: "ts-1",
112+
mainValue: 101
113+
};
114+
115+
generateHtml(tooltip, data, settings);
116+
expect(getContent()).toEqual(["split-1: ts-1", "main-1: 101"]);
117+
});
118+
119+
test("show with multiple splitValues", () => {
120+
settings.splitValues.push({name: "split-1", type: "string"});
121+
settings.splitValues.push({name: "split-2", type: "integer"});
122+
const data = {
123+
key: "ts-1|1001",
124+
mainValue: 101
125+
};
126+
127+
generateHtml(tooltip, data, settings);
128+
expect(getContent()).toEqual(["split-1: ts-1", "split-2: 1,001", "main-1: 101"]);
129+
});
130+
});

0 commit comments

Comments
 (0)