Debian Codegraph @

frontend/test/charts.test.ts

raw
1 import { test } from "uvu";
2 import assert from "uvu/assert";
3
4 import { parseChartData } from "../src/charts";
5 import { bar } from "../src/charts/bar";
6 import {
7 colors10,
8 colors15,
9 filterTicks,
10 includeZero,
11 padExtent,
12 } from "../src/charts/helpers";
13 import { hierarchy, HierarchyChart } from "../src/charts/hierarchy";
14 import { balances, LineChart } from "../src/charts/line";
15 import { ScatterPlot, scatterplot } from "../src/charts/scatterplot";
16 import { loadJSONSnapshot } from "./helpers";
17
18 test("chart helpers (filter ticks)", () => {
19 assert.equal(filterTicks(["1", "2", "3"], 2), ["1", "3"]);
20 assert.equal(filterTicks(["1", "2", "3"], 4), ["1", "2", "3"]);
21 });
22
23 test("chart helpers (color scales)", () => {
24 assert.equal(colors10[0], "rgb(126, 174, 253)");
25 assert.equal(colors15[0], "rgb(173, 200, 254)");
26 });
27
28 test("chart helpers (include zero in extent)", () => {
29 assert.equal(includeZero([2, 5]), [0, 5]);
30 assert.equal(includeZero([-12, -5]), [-12, 0]);
31 assert.equal(includeZero([-5, 5]), [-5, 5]);
32 assert.equal(includeZero([undefined, undefined]), [0, 1]);
33 });
34
35 test("chart helpers (pad extent)", () => {
36 assert.equal(padExtent([0, 1]), [-0.03, 1.03]);
37 assert.equal(padExtent([undefined, undefined]), [0, 1]);
38 });
39
40 test("handle data for hierarchical chart", async () => {
41 const ctx = { currencies: ["USD"], dateFormat: () => "DATE" };
42 assert.ok(hierarchy("name", "", ctx).is_err);
43 const data = await loadJSONSnapshot("test_internal_api-test_chart_api.json");
44 const parsed = parseChartData(data, ctx).unwrap()[0];
45 assert.ok(parsed instanceof HierarchyChart);
46 assert.equal(parsed.currencies, ["USD"]);
47 assert.ok(parsed.data.get("USD"));
48 });
49
50 test("handle data for balances chart", () => {
51 assert.ok(balances("name", "").is_err);
52 const data: unknown = [
53 { date: "2000-01-01", balance: { EUR: 10, USD: 10 } },
54 { date: "2000-02-01", balance: { EUR: 10 } },
55 ];
56 const parsed = balances("name", data).unwrap();
57 assert.ok(parsed instanceof LineChart);
58 assert.equal(parsed.filter([]), [
59 {
60 name: "EUR",
61 values: [
62 { date: new Date("2000-01-01"), name: "EUR", value: 10 },
63 { date: new Date("2000-02-01"), name: "EUR", value: 10 },
64 ],
65 },
66 {
67 name: "USD",
68 values: [{ date: new Date("2000-01-01"), name: "USD", value: 10 }],
69 },
70 ]);
71 });
72
73 test("handle data for scatterplot chart", () => {
74 assert.ok(scatterplot("name", "").is_err);
75 const data: unknown = [
76 { type: "test", date: "2000-01-01", description: "desc" },
77 ];
78 const parsed = scatterplot("name", data).unwrap();
79 assert.equal(
80 parsed,
81 new ScatterPlot("name", [
82 { date: new Date("2000-01-01"), description: "desc", type: "test" },
83 ]),
84 );
85 });
86
87 test("handle data for bar chart with stacked data", () => {
88 const data: unknown = [
89 {
90 date: "2000-01-01",
91 balance: { EUR: 10, USD: 10 },
92 budgets: { USD: 20 },
93 account_balances: {
94 "Expenses:Dining": { USD: 8 },
95 "Expenses:Transportation": { EUR: 6 },
96 "Expenses:Taxes": { USD: 2, EUR: 4 },
97 },
98 },
99 {
100 date: "2000-02-01",
101 balance: { EUR: 100 },
102 budgets: { EUR: 50 },
103 account_balances: {
104 "Expenses:Shoes": { EUR: 60 },
105 "Expenses:Taxes": { EUR: 40 },
106 },
107 },
108 ];
109 const ctx = { currencies: ["EUR", "USD"], dateFormat: () => "DATE" };
110 const chart = bar("name", data, ctx).unwrap();
111 assert.is(true, chart.hasStackedData);
112 assert.equal(chart.accounts, [
113 "Expenses:Dining",
114 "Expenses:Shoes",
115 "Expenses:Taxes",
116 "Expenses:Transportation",
117 ]);
118 assert.equal(chart.filter([]).stacks, [
119 [
120 "EUR",
121 [
122 [
123 [0, 0],
124 [0, 0],
125 ],
126 [
127 [0, 0],
128 [0, 60],
129 ],
130 [
131 [0, 4],
132 [60, 100],
133 ],
134 [
135 [4, 10],
136 [0, 0],
137 ],
138 ],
139 ],
140 [
141 "USD",
142 [
143 [
144 [0, 8],
145 [0, 0],
146 ],
147 [
148 [0, 0],
149 [0, 0],
150 ],
151 [
152 [8, 10],
153 [0, 0],
154 ],
155 [
156 [0, 0],
157 [0, 0],
158 ],
159 ],
160 ],
161 ]);
162 assert.equal(chart.filter([]).bar_groups, [
163 {
164 date: new Date("2000-01-01"),
165 label: "DATE",
166 values: [
167 {
168 currency: "EUR",
169 value: 10,
170 budget: 0,
171 },
172 {
173 currency: "USD",
174 value: 10,
175 budget: 20,
176 },
177 ],
178 account_balances: {
179 "Expenses:Dining": { USD: 8 },
180 "Expenses:Transportation": { EUR: 6 },
181 "Expenses:Taxes": { USD: 2, EUR: 4 },
182 },
183 },
184 {
185 date: new Date("2000-02-01"),
186 label: "DATE",
187 values: [
188 {
189 currency: "EUR",
190 value: 100,
191 budget: 50,
192 },
193 {
194 currency: "USD",
195 value: 0,
196 budget: 0,
197 },
198 ],
199 account_balances: {
200 "Expenses:Shoes": { EUR: 60 },
201 "Expenses:Taxes": { EUR: 40 },
202 },
203 },
204 ]);
205 });
206
207 test("handle data for bar chart without stacked data", () => {
208 const data: unknown = [
209 {
210 date: "2000-01-01",
211 balance: { EUR: 10, USD: 10 },
212 budgets: { USD: 20 },
213 account_balances: {},
214 },
215 {
216 date: "2000-02-01",
217 balance: { EUR: 100 },
218 budgets: { EUR: 50 },
219 account_balances: {},
220 },
221 ];
222 // even without the operating currencies, the two most popular ones will be selected
223 const ctx = { currencies: [], dateFormat: () => "DATE" };
224 const chart = bar("name", data, ctx).unwrap();
225 assert.is(false, chart.hasStackedData);
226 assert.equal(chart.filter([]).stacks, [
227 ["EUR", []],
228 ["USD", []],
229 ]);
230 const without_usd = chart.filter(["USD"]);
231 assert.equal(without_usd.stacks, [["EUR", []]]);
232 assert.equal(without_usd.bar_groups, [
233 {
234 date: new Date("2000-01-01"),
235 label: "DATE",
236 values: [{ currency: "EUR", value: 10, budget: 0 }],
237 account_balances: {},
238 },
239 {
240 date: new Date("2000-02-01"),
241 label: "DATE",
242 values: [{ currency: "EUR", value: 100, budget: 50 }],
243 account_balances: {},
244 },
245 ]);
246 });
247
248 test("only use currencies in records for bar chart", () => {
249 const data: unknown = [
250 {
251 date: "2000-01-01",
252 balance: { AUD: 10, USD: 10 },
253 budgets: { USD: 20 },
254 account_balances: {},
255 },
256 {
257 date: "2000-02-01",
258 balance: { AUD: 100 },
259 budgets: { AUD: 50 },
260 account_balances: {},
261 },
262 ];
263 const ctx = { currencies: ["EUR", "USD"], dateFormat: () => "DATE" };
264 const chart = bar("name", data, ctx).unwrap();
265 assert.is(false, chart.hasStackedData);
266 assert.equal(chart.filter([]).stacks, [
267 ["USD", []],
268 ["AUD", []],
269 ]);
270 assert.equal(chart.filter([]).bar_groups, [
271 {
272 date: new Date("2000-01-01"),
273 label: "DATE",
274 values: [
275 { currency: "USD", value: 10, budget: 20 },
276 { currency: "AUD", value: 10, budget: 0 },
277 ],
278 account_balances: {},
279 },
280 {
281 date: new Date("2000-02-01"),
282 label: "DATE",
283 values: [
284 { currency: "USD", value: 0, budget: 0 },
285 { currency: "AUD", value: 100, budget: 50 },
286 ],
287 account_balances: {},
288 },
289 ]);
290 });
291
292 test.run();