| 1 |
import { before, expect } from "@odoo/hoot"; |
| 2 |
import { queryAllTexts, queryOne } from "@odoo/hoot-dom"; |
| 3 |
import { contains, findComponent, preloadBundle } from "@web/../tests/web_test_helpers"; |
| 4 |
|
| 5 |
import { ensureArray } from "@web/core/utils/arrays"; |
| 6 |
import { patch } from "@web/core/utils/patch"; |
| 7 |
import { GraphController } from "@web/views/graph/graph_controller"; |
| 8 |
import { GraphRenderer } from "@web/views/graph/graph_renderer"; |
| 9 |
|
| 10 |
/** |
| 11 |
* @typedef {"bar" | "line" | "pie"} GraphMode |
| 12 |
* |
| 13 |
* @typedef {import("@web/views/view").View} GraphView |
| 14 |
*/ |
| 15 |
|
| 16 |
/** |
| 17 |
* @param {GraphView} view |
| 18 |
* @param {string | Iterable<string>} keys |
| 19 |
* @param {Record<string, any> | Iterable<Record<string, any>>} expectedDatasets |
| 20 |
*/ |
| 21 |
export const checkDatasets = (view, keys, expectedDatasets) => { |
| 22 |
keys = ensureArray(keys); |
| 23 |
|
| 24 |
const datasets = getChart(view).data.datasets; |
| 25 |
const actualValues = []; |
| 26 |
for (const dataset of datasets) { |
| 27 |
const partialDataset = {}; |
| 28 |
for (const key of keys) { |
| 29 |
partialDataset[key] = dataset[key]; |
| 30 |
} |
| 31 |
actualValues.push(partialDataset); |
| 32 |
} |
| 33 |
expect(actualValues).toEqual(ensureArray(expectedDatasets)); |
| 34 |
}; |
| 35 |
|
| 36 |
/** |
| 37 |
* @param {GraphView} view |
| 38 |
* @param {GraphMode} mode |
| 39 |
*/ |
| 40 |
export const checkModeIs = (view, mode) => { |
| 41 |
expect(getGraphModelMetaData(view).mode).toBe(mode); |
| 42 |
expect(getChart(view).config.type).toBe(mode); |
| 43 |
expect(getModeButton(mode)).toHaveClass("active"); |
| 44 |
}; |
| 45 |
|
| 46 |
/** |
| 47 |
* @param {GraphView} view |
| 48 |
* @param {{ lines: { label: string, value: string }[], title?: string }} expectedTooltip |
| 49 |
* @param {number} index |
| 50 |
* @param {number} datasetIndex |
| 51 |
*/ |
| 52 |
export const checkTooltip = (view, { title, lines }, index, datasetIndex = null) => { |
| 53 |
// If the tooltip options are changed, this helper should change: we construct the dataPoints |
| 54 |
// similarly to Chart.js according to the values set for the tooltips options 'mode' and 'intersect'. |
| 55 |
const chart = getChart(view); |
| 56 |
const { datasets } = chart.data; |
| 57 |
const dataPoints = []; |
| 58 |
for (let i = 0; i < datasets.length; i++) { |
| 59 |
const dataset = datasets[i]; |
| 60 |
const raw = dataset.data[index]; |
| 61 |
if (raw !== undefined && (datasetIndex === null || datasetIndex === i)) { |
| 62 |
dataPoints.push({ |
| 63 |
datasetIndex: i, |
| 64 |
dataIndex: index, |
| 65 |
raw, |
| 66 |
}); |
| 67 |
} |
| 68 |
} |
| 69 |
chart.config.options.plugins.tooltip.external({ |
| 70 |
tooltip: { opacity: 1, x: 1, y: 1, dataPoints }, |
| 71 |
}); |
| 72 |
const lineLabels = []; |
| 73 |
const lineValues = []; |
| 74 |
for (const line of lines) { |
| 75 |
lineLabels.push(line.label); |
| 76 |
lineValues.push(String(line.value)); |
| 77 |
} |
| 78 |
|
| 79 |
expect(`.o_graph_custom_tooltip`).toHaveCount(1); |
| 80 |
expect(`table thead tr th.o_measure`).toHaveText(title || "Count"); |
| 81 |
expect(queryAllTexts(`table tbody tr td small.o_label`)).toEqual(lineLabels); |
| 82 |
expect(queryAllTexts(`table tbody tr td.o_value`)).toEqual(lineValues); |
| 83 |
}; |
| 84 |
|
| 85 |
/** |
| 86 |
* @param {"asc" | "desc"} direction |
| 87 |
*/ |
| 88 |
export const clickSort = (direction) => contains(`.fa-sort-amount-${direction}`).click(); |
| 89 |
|
| 90 |
/** |
| 91 |
* @param {GraphView} view |
| 92 |
*/ |
| 93 |
export const getChart = (view) => getGraphRenderer(view).chart; |
| 94 |
|
| 95 |
/** |
| 96 |
* @param {GraphView} view |
| 97 |
*/ |
| 98 |
export const getGraphModelMetaData = (view) => getGraphModel(view).metaData; |
| 99 |
|
| 100 |
/** |
| 101 |
* @param {GraphMode} mode |
| 102 |
*/ |
| 103 |
export const getModeButton = (mode) => queryOne`.o_graph_button[data-mode=${mode}]`; |
| 104 |
|
| 105 |
/** |
| 106 |
* @param {GraphView} view |
| 107 |
*/ |
| 108 |
export const getScaleY = (view) => getChart(view).config.options.scales.y; |
| 109 |
|
| 110 |
/** |
| 111 |
* @param {GraphView} view |
| 112 |
*/ |
| 113 |
export const getYAxisLabel = (view) => getChart(view).config.options.scales.y.title.text; |
| 114 |
|
| 115 |
/** |
| 116 |
* @param {GraphView} view |
| 117 |
* @param {string | Iterable<string>} expectedLabels |
| 118 |
*/ |
| 119 |
export function checkLabels(view, expectedLabels) { |
| 120 |
expect(getChart(view).data.labels.map(String)).toEqual(ensureArray(expectedLabels)); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @param {GraphView} view |
| 125 |
* @param {string | Iterable<string>} expectedLabels |
| 126 |
*/ |
| 127 |
export function checkYTicks(view, expectedLabels) { |
| 128 |
const labels = getChart(view).scales.y.ticks.map((l) => l.label); |
| 129 |
expect(labels).toEqual(expectedLabels); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param {GraphView} view |
| 134 |
* @param {string | Iterable<string>} expectedLabels |
| 135 |
*/ |
| 136 |
export function checkLegend(view, expectedLabels) { |
| 137 |
const chart = getChart(view); |
| 138 |
const labels = chart.config.options.plugins.legend.labels |
| 139 |
.generateLabels(chart) |
| 140 |
.map((o) => o.text); |
| 141 |
const expectedLabelsList = ensureArray(expectedLabels); |
| 142 |
expect(labels).toEqual(expectedLabelsList, { |
| 143 |
message: `Legend should be matching: ${expectedLabelsList |
| 144 |
.map((label) => `"${label}"`) |
| 145 |
.join(", ")}`, |
| 146 |
}); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* @param {GraphView} view |
| 151 |
*/ |
| 152 |
export async function clickOnDataset(view, options = {}) { |
| 153 |
const chart = getChart(view); |
| 154 |
const point = chart.getDatasetMeta(0).data[0].getCenterPoint(); |
| 155 |
return contains(chart.canvas).click({ position: point, relative: true, ...options }); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @param {GraphView} view |
| 160 |
*/ |
| 161 |
export function getGraphController(view) { |
| 162 |
return findComponent(view, (c) => c instanceof GraphController); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* @param {GraphView} view |
| 167 |
*/ |
| 168 |
export function getGraphModel(view) { |
| 169 |
return getGraphController(view).model; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @param {GraphView} view |
| 174 |
* @returns {GraphRenderer} |
| 175 |
*/ |
| 176 |
export function getGraphRenderer(view) { |
| 177 |
return findComponent(view, (c) => c instanceof GraphRenderer); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* @param {GraphMode} mode |
| 182 |
*/ |
| 183 |
export function selectMode(mode) { |
| 184 |
return contains(getModeButton(mode)).click(); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @param {GraphView} view |
| 189 |
* @param {string} text |
| 190 |
*/ |
| 191 |
export async function clickOnLegend(view, text) { |
| 192 |
const chart = getChart(view); |
| 193 |
const index = chart.legend.legendItems.findIndex((e) => e.text === text); |
| 194 |
const { left, top, width, height } = chart.legend.legendHitBoxes[index]; |
| 195 |
const point = { |
| 196 |
x: left + width / 2, |
| 197 |
y: top + height / 2, |
| 198 |
}; |
| 199 |
return contains(chart.canvas).click({ position: point, relative: true }); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Helper to call at the start of a test suite using the Chart.js lib. |
| 204 |
* |
| 205 |
* It will: |
| 206 |
* - pre-load the Chart.js lib before tests are run; |
| 207 |
* - disable all animations in the lib. |
| 208 |
*/ |
| 209 |
export function setupChartJsForTests() { |
| 210 |
preloadBundle("web.chartjs_lib"); |
| 211 |
before(() => patch(Chart.defaults, { animation: false })); |
| 212 |
} |