forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiod-change.test.ts
More file actions
42 lines (37 loc) · 1.21 KB
/
Copy pathperiod-change.test.ts
File metadata and controls
42 lines (37 loc) · 1.21 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { describe, expect, it } from "vitest";
import {
calculatePeriodChange,
formatPeriodChange,
latestPeriodChange,
} from "@/lib/period-change";
describe("period change", () => {
it("calculates signed changes between matching periods", () => {
expect(calculatePeriodChange(120, 100, "vs previous week")).toEqual({
percentage: 20,
label: "vs previous week",
});
expect(calculatePeriodChange(75, 100)?.percentage).toBe(-25);
expect(formatPeriodChange(20)).toBe("+20%");
expect(formatPeriodChange(-3.25)).toBe("-3.3%");
});
it("uses the latest two matching buckets", () => {
const change = latestPeriodChange(
[{ total: 80 }, { total: 100 }],
(point) => point.total,
);
expect(change?.percentage).toBe(25);
expect(
latestPeriodChange(
[{ total: 80 }, { total: 100 }, { total: Number.NaN }],
(point) => point.total,
),
).toBeNull();
});
it("does not invent a percentage without a valid baseline", () => {
expect(calculatePeriodChange(5, 0)).toBeNull();
expect(
latestPeriodChange([{ total: 5 }], (point) => point.total),
).toBeNull();
expect(calculatePeriodChange(0, 0)?.percentage).toBe(0);
});
});