Skip to content

Commit

Permalink
Merge pull request #159 from redbearsam/feature/unit-test-interaction
Browse files Browse the repository at this point in the history
Test UI interaction in tooltip component
  • Loading branch information
andy-lee-eng authored Apr 5, 2019
2 parents 7c6d77f + 638ce41 commit 1b2370b
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/perspective-viewer-d3fc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"testURL": "http://localhost/",
"automock": false,
"transform": {
".js$": "./test/js/transform.js"
".js$": "./test/js/transform.js",
".html$": "html-loader-jest"
},
"setupFiles": [
"./test/js/beforeEachSpec.js"
Expand All @@ -59,6 +60,7 @@
},
"devDependencies": {
"babel-loader": "^8.0.5",
"html-loader-jest": "^0.2.1",
"webpack": "^3.5.6"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const component = settings => {
};

const getXAxisBoundsRect = s => {
const chart = s.node().closest(".cartesian-chart");
const chart = getChartContainer(s.node()).querySelector(".cartesian-chart");
const axis = chart.querySelector(".x-axis");

const chartRect = chart.getBoundingClientRect();
Expand Down
2 changes: 1 addition & 1 deletion packages/perspective-viewer-d3fc/src/js/plugin/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export function getChartElement(element) {
}

export function getChartContainer(element) {
return getChartElement(element).getContainer();
return element.closest("#container.chart");
}
3 changes: 2 additions & 1 deletion packages/perspective-viewer-d3fc/test/js/jest.unit.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
testURL: "http://localhost/",
automock: false,
transform: {
".js$": "./transform.js"
".js$": "./transform.js",
".html$": "html-loader-jest"
},
setupFiles: ["./beforeEachSpec.js"]
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

import {groupAndStackData} from "../../../src/js/data/groupData";
import {groupAndStackData} from "../../../../src/js/data/groupData";

describe("groupAndStackData should", () => {
test("include globals", () => {
Expand All @@ -16,7 +16,7 @@ describe("groupAndStackData should", () => {
expect(typeof customElements.define).toBe("function");
});

test("groupAndStackData should use settings data if no specific data is supplied", () => {
test("use settings data if no specific data is supplied", () => {
const settings = {
crossValues: [{name: "cross1", type: "string"}],
data: [{value1: 10, __ROW_PATH__: ["CROSS1.1"]}, {value1: 20, __ROW_PATH__: ["CROSS1.2"]}, {value1: 30, __ROW_PATH__: ["CROSS1.1"]}],
Expand All @@ -28,7 +28,7 @@ describe("groupAndStackData should", () => {
expect(groupedResult[0].length).toEqual(3);
});

test("groupAndStackData should use specific data if supplied", () => {
test("use specific data if supplied", () => {
const suppliedData = [{value1: 10, __ROW_PATH__: ["CROSS1.1"]}, {value1: 20, __ROW_PATH__: ["CROSS1.2"]}, {value1: 30, __ROW_PATH__: ["CROSS1.1"]}];
const settings = {
crossValues: [{name: "cross1", type: "string"}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {select} from "d3";
import {generateHtml} from "../../../../src/js/tooltip/generateHTML";

describe("tooltip should", () => {
describe("tooltip generateHTML should", () => {
let tooltip = null;
let settings = null;

Expand Down
105 changes: 105 additions & 0 deletions packages/perspective-viewer-d3fc/test/js/unit/tooltip/tooltip.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/
import {select} from "d3";
import {tooltip} from "../../../../src/js/tooltip/tooltip";

describe("tooltip with", () => {
let container = null;
let testElement = null;
let settings = null;
const data = [{mainValue: 101}];

const awaitTransition = selection => {
return new Promise(resolve => {
const transition = selection.transition();
let n = transition.size();
if (n === 0) {
resolve();
}
transition.on("end", () => {
if (!--n) {
resolve();
}
});
});
};

beforeEach(() => {
container = select("body")
.append("div")
.attr("id", "container")
.classed("chart", true);

testElement = container
.selectAll("div.element")
.data(data)
.enter()
.append("div")
.classed("element", true)
.style("position", "absolute")
.style("top", "150px")
.style("left", "300px")
.style("width", "25px")
.style("height", "40px");

settings = {
crossValues: [],
splitValues: [],
mainValues: [{name: "main-1", type: "integer"}]
};
});
afterEach(() => {
container.remove();
});

describe("on-hover should", () => {
let tooltipDiv;
beforeEach(async () => {
tooltip().settings(settings)(testElement);
tooltipDiv = container.select("div.tooltip");
await awaitTransition(tooltipDiv);
});

test("not show a tooltip initially", () => {
expect(tooltipDiv.style("opacity")).toEqual("0");
});

test("show a tooltip on mouse over", async () => {
testElement.node().dispatchEvent(new MouseEvent("mouseover"));
await awaitTransition(tooltipDiv);

expect(tooltipDiv.style("opacity")).not.toEqual("0");
});
});

describe("always-show should", () => {
let tooltipDiv;
let tooltipComponent;
beforeEach(async () => {
tooltipComponent = tooltip()
.settings(settings)
.alwaysShow(true);

tooltipComponent(testElement);
tooltipDiv = container.select("div.tooltip");
await awaitTransition(tooltipDiv);
});

test("show a tooltip initially", () => {
expect(tooltipDiv.style("opacity")).not.toEqual("0");
});

test("hide a tooltip if no element", async () => {
tooltipComponent(container.select("div.notexists"));
await awaitTransition(tooltipDiv);

expect(tooltipDiv.style("opacity")).not.toEqual("0");
});
});
});
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6745,6 +6745,13 @@ html-entities@^1.2.0:
resolved "https://registry.yarnpkg.com/html-entities/-/html-entities-1.2.1.tgz#0df29351f0721163515dfb9e5543e5f6eed5162f"
integrity sha1-DfKTUfByEWNRXfueVUPl9u7VFi8=

html-loader-jest@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/html-loader-jest/-/html-loader-jest-0.2.1.tgz#1803c2bcd9e41ccc738a0979cc6de0acbabac3f8"
integrity sha512-Sq9eDpsr/8kI+kyiQAL8jawa+aGRphANCeIeoLyU05DEfHd9vCi4Zz8AXUQTbqnF0TRGfVn9qN69/ox378kyGg==
dependencies:
html-loader "^0.5.1"

html-loader@^0.5.1:
version "0.5.5"
resolved "https://registry.yarnpkg.com/html-loader/-/html-loader-0.5.5.tgz#6356dbeb0c49756d8ebd5ca327f16ff06ab5faea"
Expand Down

0 comments on commit 1b2370b

Please sign in to comment.