Skip to content

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina committed Apr 30, 2024
1 parent ae46232 commit 10cfb22
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ const TRUNCATION_STYLE = `
text-overflow: ellipsis;
`;

// TODO Check if there's any column alignment different than ['left', 'right', 'right']
// TODO: Remove colored border from all chart types?
export function tooltipHtml(
data: string[][],
columnAlignments: ('left' | 'right' | 'middle')[],
title?: string,
focusedRow?: number,
) {
Expand All @@ -43,12 +40,11 @@ export function tooltipHtml(
${data
.map((row, i) => {
const rowStyle =
i === focusedRow ? 'font-weight: 700;' : 'opacity: 0.7;';
i === focusedRow ? 'font-weight: 700;' : 'opacity: 0.8;';
let padding = 0;
const cells = row.map((cell, j) => {
// TODO: Maybe make the font a little darker
const cellStyle = `
text-align: ${columnAlignments[j]};
text-align: ${j > 0 ? 'right' : 'left'};
padding-left: ${padding}px;
${TRUNCATION_STYLE}
`;
Expand Down
115 changes: 115 additions & 0 deletions superset-frontend/packages/superset-ui-core/test/utils/tooltip.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { tooltipHtml } from '@superset-ui/core';

const TITLE_STYLE =
'style="font-weight: 700;max-width:300px;overflow:hidden;text-overflow:ellipsis;"';
const TR_STYLE = 'style="opacity:0.8;"';
const TR_FOCUS_STYLE = 'style="font-weight:700;"';
const TD_TEXT_STYLE =
'style="text-align:left;padding-left:0px;max-width:300px;overflow:hidden;text-overflow:ellipsis;"';
const TD_NUMBER_STYLE =
'style="text-align:right;padding-left:16px;max-width:300px;overflow:hidden;text-overflow:ellipsis;"';

const data = [
['a', 'b', 'c'],
['1', '2', '3'],
];

function removeWhitespaces(text: string) {
return text.replace(/\s/g, '');
}

test('should return a table with the given data', () => {
const title = 'Title';
const html = removeWhitespaces(tooltipHtml(data, title));
const expectedHtml = removeWhitespaces(`
<div>
<span ${TITLE_STYLE}>Title</span>
<table>
<tr ${TR_STYLE}>
<td ${TD_TEXT_STYLE}>a</td>
<td ${TD_NUMBER_STYLE}>b</td>
<td ${TD_NUMBER_STYLE}>c</td>
</tr>
<tr ${TR_STYLE}>
<td ${TD_TEXT_STYLE}>1</td>
<td ${TD_NUMBER_STYLE}>2</td>
<td ${TD_NUMBER_STYLE}>3</td>
</tr>
</table>
</div>`);
expect(html).toMatch(expectedHtml);
});

test('should return a table with the given data and a focused row', () => {
const title = 'Title';
const focusedRow = 1;
const html = removeWhitespaces(tooltipHtml(data, title, focusedRow));
const expectedHtml = removeWhitespaces(`
<div>
<span ${TITLE_STYLE}>Title</span>
<table>
<tr ${TR_STYLE}>
<td ${TD_TEXT_STYLE}>a</td>
<td ${TD_NUMBER_STYLE}>b</td>
<td ${TD_NUMBER_STYLE}>c</td>
</tr>
<tr ${TR_FOCUS_STYLE}>
<td ${TD_TEXT_STYLE}>1</td>
<td ${TD_NUMBER_STYLE}>2</td>
<td ${TD_NUMBER_STYLE}>3</td>
</tr>
</table>
</div>`);
expect(html).toMatch(expectedHtml);
});

test('should return a table with no data', () => {
const title = 'Title';
const html = removeWhitespaces(tooltipHtml([], title));
const expectedHtml = removeWhitespaces(`
<div>
<span ${TITLE_STYLE}>Title</span>
<table>
<tr><td>No data</td></tr>
</table>
</div>`);
expect(html).toMatch(expectedHtml);
});

test('should return a table with the given data and no title', () => {
const html = removeWhitespaces(tooltipHtml(data));
const expectedHtml = removeWhitespaces(`
<div>
<table>
<tr ${TR_STYLE}>
<td ${TD_TEXT_STYLE}>a</td>
<td ${TD_NUMBER_STYLE}>b</td>
<td ${TD_NUMBER_STYLE}>c</td>
</tr>
<tr ${TR_STYLE}>
<td ${TD_TEXT_STYLE}>1</td>
<td ${TD_NUMBER_STYLE}>2</td>
<td ${TD_NUMBER_STYLE}>3</td>
</tr>
</table>
</div>`);
expect(html).toMatch(expectedHtml);
});
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ export default function transformProps(
: headerFormatter.format(params[0].data[1]),
],
],
['left', 'right'],
formatTime(params[0].data[0]),
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export function formatTooltip(
[yAxisLabel, yAxisFormatter(params.data[1])],
[sizeLabel, tooltipSizeFormatter(params.data[2])],
],
['left', 'right'],
title,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export default function transformProps(
if (enumName.includes('Percent')) {
row.push(formattedPercent);
}
return tooltipHtml([row], ['left', 'right', 'right'], title);
return tooltipHtml([row], title);
},
},
legend: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,7 @@ export default function transformProps(
...getDefaultTooltip(refs),
formatter: (params: CallbackDataParams) => {
const { name, value } = params;
return tooltipHtml(
[[metricLabel, formatValue(value as number)]],
['left', 'right'],
name,
);
return tooltipHtml([[metricLabel, formatValue(value as number)]], name);
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,7 @@ export default function transformProps(
getKeyByValue(nodes, Number(params.data.target)),
);
const title = `${source} > ${target}`;
return tooltipHtml(
[[metricLabel, `${params.value}`]],
['left', 'right'],
title,
);
return tooltipHtml([[metricLabel, `${params.value}`]], title);
},
},
legend: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default function transformProps(
if (showPercentage) {
row.push(`${percentFormatter(percentage)} (${suffix})`);
}
return tooltipHtml([row], ['left', 'right', 'right'], title);
return tooltipHtml([row], title);
},
},
visualMap: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,6 @@ export default function transformProps(
}
return tooltipHtml(
rows,
['left', 'right', 'right'],
tooltipFormatter(xValue),
keys.findIndex(key => key === focusedSeries),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ export default function transformProps(
});
return tooltipHtml(
[[metricLabel, formattedValue, formattedPercent]],
['left', 'right', 'right'],
name,
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function formatTooltip({
compareValuePercentage,
]);
}
return tooltipHtml(rows, ['left', 'right'], title);
return tooltipHtml(rows, title);
}

export default function transformProps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,6 @@ export default function transformProps(
}
return tooltipHtml(
rows,
['left', 'right', 'right'],
tooltipFormatter(xValue),
keys.findIndex(key => key === focusedSeries),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function formatTooltip({
.map(pathInfo => pathInfo?.name || '')
.filter(path => path !== '');
const row = value ? [metricLabel, String(value)] : [];
return tooltipHtml([row], ['left', 'right'], treePath.join(' ▸ '));
return tooltipHtml([row], treePath.join(' ▸ '));
}

export default function transformProps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function formatTooltip({
if (formattedPercent) {
row.push(formattedPercent);
}
return tooltipHtml([row], ['left', 'right', 'right'], treePath.join(' ▸ '));
return tooltipHtml([row], treePath.join(' ▸ '));
}

export default function transformProps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function formatTooltip({
]);
}
rows.push([TOTAL_MARK, defaultFormatter(series.data.totalSum)]);
return tooltipHtml(rows, ['left', 'right'], title);
return tooltipHtml(rows, title);
}

function transformer({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
* under the License.
*/
import { isNumber } from 'lodash';
import {
DataRecord,
DataRecordValue,
DTTM_ALIAS,
ValueFormatter,
} from '@superset-ui/core';
import { DataRecord, DTTM_ALIAS, ValueFormatter } from '@superset-ui/core';
import { OptionName } from 'echarts/types/src/util/types';
import { TooltipMarker } from 'echarts/types/src/util/format';
import {
Expand Down Expand Up @@ -112,8 +107,9 @@ export const formatForecastTooltipSeries = ({
value += `ŷ = ${formatter(forecastTrend)}`;
}
if (forecastLower && forecastUpper) {
if (value) value += ' ';
// the lower bound needs to be added to the upper bound
value += ` (${formatter(forecastLower)}, ${formatter(
value += `(${formatter(forecastLower)}, ${formatter(
forecastLower + forecastUpper,
)})`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Refs } from '../types';
export function getDefaultTooltip(refs: Refs) {
return {
appendToBody: true,
borderColor: 'transparent',
position: (
canvasMousePos: [number, number],
params: CallbackDataParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,42 +160,44 @@ describe('Bubble formatTooltip', () => {
data: [10000, 20000, 3, 'bubble title', 'bubble dimension'],
};

expect(
formatTooltip(
params,
'x-axis-label',
'y-axis-label',
'size-label',
dollerFormatter,
dollerFormatter,
percentFormatter,
),
).toEqual(
`<p>bubble title </br> bubble dimension</p>
x-axis-label: $10,000.00 <br/>
y-axis-label: $20,000.00 <br/>
size-label: 300.0%`,
const html = formatTooltip(
params,
'x-axis-label',
'y-axis-label',
'size-label',
dollerFormatter,
dollerFormatter,
percentFormatter,
);
expect(html).toContain('bubble title');
expect(html).toContain('bubble dimension');
expect(html).toContain('x-axis-label');
expect(html).toContain('y-axis-label');
expect(html).toContain('size-label');
expect(html).toContain('$10,000.00');
expect(html).toContain('$20,000.00');
expect(html).toContain('300.0%');
});
it('Should generate correct bubble label content without dimension', () => {
const params = {
data: [10000, 25000, 3, 'bubble title', null],
};
expect(
formatTooltip(
params,
'x-axis-label',
'y-axis-label',
'size-label',
dollerFormatter,
dollerFormatter,
percentFormatter,
),
).toEqual(
`<p>bubble title</p>
x-axis-label: $10,000.00 <br/>
y-axis-label: $25,000.00 <br/>
size-label: 300.0%`,
const html = formatTooltip(
params,
'x-axis-label',
'y-axis-label',
'size-label',
dollerFormatter,
dollerFormatter,
percentFormatter,
);
expect(html).toContain('bubble title');
expect(html).not.toContain('bubble dimension');
expect(html).toContain('x-axis-label');
expect(html).toContain('y-axis-label');
expect(html).toContain('size-label');
expect(html).toContain('$10,000.00');
expect(html).toContain('$25,000.00');
expect(html).toContain('300.0%');
});
});
Loading

0 comments on commit 10cfb22

Please sign in to comment.