2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupTest } from 'ember-qunit';
|
2018-09-11 03:47:55 +00:00
|
|
|
import d3Format from 'd3-format';
|
2021-02-23 01:34:24 +00:00
|
|
|
import setupGlimmerComponentFactory from 'nomad-ui/tests/helpers/glimmer-factory';
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module('Unit | Component | line-chart', function (hooks) {
|
2019-03-13 00:04:16 +00:00
|
|
|
setupTest(hooks);
|
2021-02-23 01:34:24 +00:00
|
|
|
setupGlimmerComponentFactory(hooks, 'line-chart');
|
2019-03-13 00:04:16 +00:00
|
|
|
|
|
|
|
const data = [
|
|
|
|
{ foo: 1, bar: 100 },
|
|
|
|
{ foo: 2, bar: 200 },
|
|
|
|
{ foo: 3, bar: 300 },
|
|
|
|
{ foo: 8, bar: 400 },
|
|
|
|
{ foo: 4, bar: 500 },
|
|
|
|
];
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('x scale domain is the min and max values in data based on the xProp value', function (assert) {
|
2021-02-23 01:34:24 +00:00
|
|
|
const chart = this.createComponent({
|
2019-03-13 00:04:16 +00:00
|
|
|
xProp: 'foo',
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
let [xDomainLow, xDomainHigh] = chart.xScale.domain();
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(
|
|
|
|
xDomainLow,
|
|
|
|
Math.min(...data.mapBy('foo')),
|
|
|
|
'Domain lower bound is the lowest foo value'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
xDomainHigh,
|
|
|
|
Math.max(...data.mapBy('foo')),
|
|
|
|
'Domain upper bound is the highest foo value'
|
|
|
|
);
|
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.args.data = [...data, { foo: 12, bar: 600 }];
|
2019-03-13 00:04:16 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
[, xDomainHigh] = chart.xScale.domain();
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
xDomainHigh,
|
|
|
|
12,
|
|
|
|
'When the data changes, the xScale is recalculated'
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('y scale domain uses the max value in the data based off of yProp, but is always zero-based', function (assert) {
|
2021-02-23 01:34:24 +00:00
|
|
|
const chart = this.createComponent({
|
2019-03-13 00:04:16 +00:00
|
|
|
yProp: 'bar',
|
|
|
|
data,
|
|
|
|
});
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
let [yDomainLow, yDomainHigh] = chart.yScale.domain();
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(yDomainLow, 0, 'Domain lower bound is always 0');
|
|
|
|
assert.equal(
|
|
|
|
yDomainHigh,
|
|
|
|
Math.max(...data.mapBy('bar')),
|
|
|
|
'Domain upper bound is the highest bar value'
|
|
|
|
);
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.args.data = [...data, { foo: 12, bar: 600 }];
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
[, yDomainHigh] = chart.yScale.domain();
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
yDomainHigh,
|
|
|
|
600,
|
|
|
|
'When the data changes, the yScale is recalculated'
|
|
|
|
);
|
2018-09-11 03:47:55 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the number of yTicks is always odd (to always have a mid-line) and is based off the chart height', function (assert) {
|
2021-02-23 01:34:24 +00:00
|
|
|
const chart = this.createComponent({
|
2019-03-13 00:04:16 +00:00
|
|
|
yProp: 'bar',
|
|
|
|
data,
|
|
|
|
});
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.height = 100;
|
|
|
|
assert.equal(chart.yTicks.length, 3);
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.height = 240;
|
|
|
|
assert.equal(chart.yTicks.length, 5);
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.height = 242;
|
|
|
|
assert.equal(chart.yTicks.length, 7);
|
2018-09-11 03:47:55 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the values for yTicks are rounded to whole numbers', function (assert) {
|
2021-02-23 01:34:24 +00:00
|
|
|
const chart = this.createComponent({
|
2019-03-13 00:04:16 +00:00
|
|
|
yProp: 'bar',
|
|
|
|
data,
|
|
|
|
});
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.height = 100;
|
|
|
|
assert.deepEqual(chart.yTicks, [0, 250, 500]);
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.height = 240;
|
|
|
|
assert.deepEqual(chart.yTicks, [0, 125, 250, 375, 500]);
|
2018-09-11 03:47:55 +00:00
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.height = 242;
|
|
|
|
assert.deepEqual(chart.yTicks, [0, 83, 167, 250, 333, 417, 500]);
|
2018-09-11 03:47:55 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the values for yTicks are fractions when the domain is between 0 and 1', function (assert) {
|
2021-02-23 01:34:24 +00:00
|
|
|
const chart = this.createComponent({
|
2019-03-13 00:04:16 +00:00
|
|
|
yProp: 'bar',
|
|
|
|
data: [
|
|
|
|
{ foo: 1, bar: 0.1 },
|
|
|
|
{ foo: 2, bar: 0.2 },
|
|
|
|
{ foo: 3, bar: 0.3 },
|
|
|
|
{ foo: 8, bar: 0.4 },
|
|
|
|
{ foo: 4, bar: 0.5 },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.height = 100;
|
|
|
|
assert.deepEqual(chart.yTicks, [0, 0.25, 0.5]);
|
2018-09-11 03:47:55 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('activeDatumLabel is the xProp value of the activeDatum formatted with xFormat', function (assert) {
|
2021-02-23 01:34:24 +00:00
|
|
|
const chart = this.createComponent({
|
2019-03-13 00:04:16 +00:00
|
|
|
xProp: 'foo',
|
|
|
|
yProp: 'bar',
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.activeDatum = data[1];
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.activeDatumLabel,
|
2019-03-13 00:04:16 +00:00
|
|
|
d3Format.format(',')(data[1].foo),
|
|
|
|
'activeDatumLabel correctly formats the correct prop of the correct datum'
|
|
|
|
);
|
2018-09-11 03:47:55 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('activeDatumValue is the yProp value of the activeDatum formatted with yFormat', function (assert) {
|
2021-02-23 01:34:24 +00:00
|
|
|
const chart = this.createComponent({
|
2019-03-13 00:04:16 +00:00
|
|
|
xProp: 'foo',
|
|
|
|
yProp: 'bar',
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.activeDatum = data[1];
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(
|
2021-02-23 01:34:24 +00:00
|
|
|
chart.activeDatumValue,
|
2019-03-13 00:04:16 +00:00
|
|
|
d3Format.format(',.2~r')(data[1].bar),
|
|
|
|
'activeDatumValue correctly formats the correct prop of the correct datum'
|
|
|
|
);
|
2018-09-11 03:47:55 +00:00
|
|
|
});
|
|
|
|
});
|