|
| 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