aboutsummaryrefslogtreecommitdiff
path: root/web/frontend/analytics-logic.test.ts
blob: 74fdddd08b46ee3a00ed307f110f71d2977b3f27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

import assert from "node:assert/strict";
import test from "node:test";
import { formatCotacol, formatSpeed, niceStep, ticks } from "./analytics-logic.ts";

test("computes a pleasant axis step within a 1-2-5-10 series", () => {
  assert.equal(niceStep(25, 5), 5);
  assert.equal(niceStep(60, 5), 10);
  assert.equal(niceStep(3.6, 5), 0.5);
  assert.equal(niceStep(0, 5), 1);
});

test("produces ascending ticks that bracket the data range and include its max", () => {
  assert.deepEqual(ticks(0, 27.9), [0, 5, 10, 15, 20, 25]);
  assert.deepEqual(ticks(0, 315), [0, 50, 100, 150, 200, 250, 300]);
  const values = ticks(0, 27.9);
  for (let index = 1; index < values.length; index++) {
    assert.ok(values[index] > values[index - 1]);
  }
  assert.ok(values[values.length - 1] <= 27.9 + 1e-9);
});

test("formats speed and cotacol per 100 km for axis labels", () => {
  assert.equal(formatSpeed(25.2), "25.2");
  assert.equal(formatCotacol(300), "300");
  assert.equal(formatCotacol(50), "50.0");
});