@yuneta/gobj-ui 7.31.4 → 7.31.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,260 @@
1
+ // @vitest-environment jsdom
2
+ /***********************************************************************
3
+ * json_graph_lit_edge.wiring.test.js
4
+ *
5
+ * The JSON graph (C_YUI_JSON_GRAPH) zooms out to 2% (a document
6
+ * of a few hundred cards did not fit at the old 10% floor), shows
7
+ * a minimap from `minimap_min_nodes` cards on, and a click on an
8
+ * edge LIGHTS it: the line in amber, its two cards ringed, and
9
+ * the row of the parent's key the line leaves from washed. A
10
+ * second click on it, or a click on the canvas, puts it out; a
11
+ * fold that takes its child card away puts it out too.
12
+ *
13
+ * Driven through the real gclass, in jsdom, with a G6 double that
14
+ * keeps the data it is given.
15
+ *
16
+ * Copyright (c) 2026, ArtGins.
17
+ * All Rights Reserved.
18
+ ***********************************************************************/
19
+ import {describe, test, expect, beforeAll, beforeEach, vi} from "vitest";
20
+
21
+ globalThis.ResizeObserver = class {
22
+ observe() {
23
+ }
24
+ unobserve() {
25
+ }
26
+ disconnect() {
27
+ }
28
+ };
29
+
30
+ const graphs = vi.hoisted(() => []);
31
+ vi.mock("@antv/g6", async (importOriginal) => {
32
+ const real = await importOriginal();
33
+ class FakeGraph {
34
+ constructor(options) {
35
+ this.options = options;
36
+ this.handlers = {};
37
+ this.plugins = (options.plugins || []).slice();
38
+ this.data = {nodes: [], edges: []};
39
+ graphs.push(this);
40
+ return new Proxy(this, {
41
+ get: (t, k) => (k in t) ? t[k] : (() => undefined),
42
+ });
43
+ }
44
+ setData(data) {
45
+ /* Edges get the ids G6 would give them */
46
+ data.edges.forEach((e, i) => {
47
+ e.id = e.id || `e${i}`;
48
+ });
49
+ this.data = data;
50
+ }
51
+ clear() {
52
+ this.data = {nodes: [], edges: []};
53
+ }
54
+ render() {
55
+ return Promise.resolve();
56
+ }
57
+ draw() {
58
+ return Promise.resolve();
59
+ }
60
+ on(name, fn) {
61
+ (this.handlers[name] = this.handlers[name] || []).push(fn);
62
+ }
63
+ emit(name, ev) {
64
+ for(const fn of (this.handlers[name] || [])) {
65
+ fn(ev);
66
+ }
67
+ }
68
+ getZoom() {
69
+ return 1;
70
+ }
71
+ getPosition() {
72
+ return [0, 0];
73
+ }
74
+ zoomTo() {
75
+ return Promise.resolve();
76
+ }
77
+ translateTo() {
78
+ return Promise.resolve();
79
+ }
80
+ getData() {
81
+ return this.data;
82
+ }
83
+ getNodeData(id) {
84
+ if(id === undefined) {
85
+ return this.data.nodes;
86
+ }
87
+ const nd = this.data.nodes.find((n) => n.id === id);
88
+ if(!nd) {
89
+ throw new Error(`node ${id} not found`);
90
+ }
91
+ return nd;
92
+ }
93
+ getEdgeData(id) {
94
+ const ed = this.data.edges.find((e) => e.id === id);
95
+ if(!ed) {
96
+ throw new Error(`edge ${id} not found`);
97
+ }
98
+ return ed;
99
+ }
100
+ getPlugins() {
101
+ return this.plugins;
102
+ }
103
+ setPlugins(p) {
104
+ this.plugins = (typeof p === "function")? p(this.plugins) : p;
105
+ }
106
+ getPluginInstance() {
107
+ return {destroyed: false, destroy() {
108
+ }};
109
+ }
110
+ getBehaviors() {
111
+ return [];
112
+ }
113
+ getOptions() {
114
+ return {behaviors: []};
115
+ }
116
+ getCanvas() {
117
+ return {getLayers: () => ({}), getContainer: () => null};
118
+ }
119
+ fitView() {
120
+ return Promise.resolve();
121
+ }
122
+ destroy() {
123
+ }
124
+ }
125
+ return {...real, Graph: FakeGraph};
126
+ });
127
+
128
+ const {
129
+ SDATA_END, gclass_create, gobj_start_up, gobj_create_yuno, gobj_create_pure_child,
130
+ gobj_start, gobj_read_attr, gobj_send_event, set_log_callback, register_c_timer,
131
+ } = await import("@yuneta/gobj-js");
132
+ const {EdgeEvent, CanvasEvent} = await import("@antv/g6");
133
+ const {register_c_yui_json_graph} = await import("./c_yui_json_graph.js");
134
+
135
+ const logged = [];
136
+ let yuno = null;
137
+ let seq = 0;
138
+
139
+ beforeAll(async () => {
140
+ const i18next = (await import("i18next")).default;
141
+ await i18next.init({lng: "en", resources: {en: {translation: {}}}});
142
+ gobj_start_up(null, null, null, null, null, null, null);
143
+ set_log_callback((level, msg) => {
144
+ logged.push({level: String(level), msg: String(msg)});
145
+ });
146
+ register_c_timer();
147
+ register_c_yui_json_graph();
148
+ gclass_create("C_TEST_JG_YUNO", [], [["ST_IDLE", []]], {}, 0,
149
+ [SDATA_END()], {}, 0, 0, 0, 0);
150
+ yuno = gobj_create_yuno("jg_yuno", "C_TEST_JG_YUNO", {});
151
+ });
152
+
153
+ beforeEach(() => {
154
+ logged.length = 0;
155
+ });
156
+
157
+ function settle()
158
+ {
159
+ return new Promise((resolve) => setTimeout(resolve, 0));
160
+ }
161
+
162
+ async function mount(json, attrs)
163
+ {
164
+ const $container = document.createElement("div");
165
+ document.body.appendChild($container);
166
+ const view = gobj_create_pure_child(`json-lit-${++seq}`, "C_YUI_JSON_GRAPH",
167
+ Object.assign({$container: $container, json_data: json}, attrs || {}), yuno);
168
+ document.body.appendChild(gobj_read_attr(view, "$container"));
169
+ gobj_start(view);
170
+ await settle();
171
+ return {view, graph: graphs[graphs.length - 1]};
172
+ }
173
+
174
+ const DOC = {a: 1, b: {x: 1, y: {z: 2}}, c: [1, 2]};
175
+
176
+ /* The edge to the card of `path`, and the card of a path */
177
+ function edge_to(graph, path)
178
+ {
179
+ const nd = graph.data.nodes.find((n) => n.data && n.data.path === path && !n.data.phantom);
180
+ return graph.data.edges.find((e) => e.target === nd.id);
181
+ }
182
+ function card(graph, path)
183
+ {
184
+ return graph.data.nodes.find((n) => n.data && n.data.path === path && !n.data.phantom).style.innerHTML;
185
+ }
186
+ const RING = /border: 3px solid/;
187
+ const WASH = /background:color-mix\(in srgb, #[0-9a-f]+ 28%, transparent\)/g;
188
+
189
+ function problems()
190
+ {
191
+ return logged.filter((l) => l.level === "error" || l.level === "warning").map((l) => l.msg);
192
+ }
193
+
194
+ describe("C_YUI_JSON_GRAPH: zoom floor, minimap, lit edge", () => {
195
+ test("the zoom goes down to 2%, and an edge takes a click well around its line", async () => {
196
+ const {graph} = await mount(DOC);
197
+ expect(graph.options.zoomRange[0]).toBeLessThanOrEqual(0.02);
198
+ for(const e of graph.data.edges) {
199
+ if(e.style.opacity === 0) {
200
+ continue;
201
+ }
202
+ expect(e.style.increasedLineWidthForHitTesting).toBeGreaterThanOrEqual(10);
203
+ }
204
+ });
205
+
206
+ test("the minimap is there from minimap_min_nodes cards on, and not below", async () => {
207
+ const few = await mount(DOC, {minimap_min_nodes: 10});
208
+ expect(few.graph.plugins.some((p) => p.key === "minimap")).toBe(false);
209
+ const many = await mount(DOC, {minimap_min_nodes: 2});
210
+ const minimap = many.graph.plugins.find((p) => p.key === "minimap");
211
+ expect(minimap).toBeTruthy();
212
+ expect(minimap.containerStyle).toMatchObject({top: "auto", bottom: "12px", left: "12px"});
213
+ });
214
+
215
+ test("an edge click lights the line, its two cards and the parent's row; again puts it out", async () => {
216
+ const {graph} = await mount(DOC);
217
+ const root = graph.data.nodes[0].data.path;
218
+ const child = graph.data.nodes.find((n) => n.data && n.data.path !== root && !n.data.phantom).data.path;
219
+ expect(card(graph, child)).not.toMatch(RING);
220
+
221
+ graph.emit(EdgeEvent.CLICK, {target: {id: edge_to(graph, child).id}});
222
+ await settle();
223
+
224
+ const lit = edge_to(graph, child);
225
+ expect(lit.style.lineWidth).toBe(3);
226
+ expect(lit.style.halo).toBe(true);
227
+ expect(card(graph, child)).toMatch(RING);
228
+ expect(card(graph, root)).toMatch(RING);
229
+ expect((card(graph, root).match(WASH) || []).length).toBe(1);
230
+ /* only those two */
231
+ const ringed = graph.data.nodes.filter((n) => RING.test(n.style.innerHTML || ""));
232
+ expect(ringed.length).toBe(2);
233
+
234
+ graph.emit(EdgeEvent.CLICK, {target: {id: edge_to(graph, child).id}});
235
+ await settle();
236
+ expect(edge_to(graph, child).style.lineWidth).not.toBe(3);
237
+ expect(card(graph, root)).not.toMatch(RING);
238
+ expect(problems()).toEqual([]);
239
+ });
240
+
241
+ test("a canvas click puts it out; a new document starts with nothing lit", async () => {
242
+ const {view, graph} = await mount(DOC);
243
+ const root = graph.data.nodes[0].data.path;
244
+ const child = graph.data.nodes.find((n) => n.data && n.data.path !== root && !n.data.phantom).data.path;
245
+
246
+ graph.emit(EdgeEvent.CLICK, {target: {id: edge_to(graph, child).id}});
247
+ await settle();
248
+ expect(card(graph, child)).toMatch(RING);
249
+ graph.emit(CanvasEvent.CLICK, {});
250
+ await settle();
251
+ expect(card(graph, child)).not.toMatch(RING);
252
+
253
+ graph.emit(EdgeEvent.CLICK, {target: {id: edge_to(graph, child).id}});
254
+ await settle();
255
+ gobj_send_event(view, "EV_LOAD_DATA", {data: DOC}, view);
256
+ await settle();
257
+ expect(graph.data.nodes.some((n) => RING.test(n.style.innerHTML || ""))).toBe(false);
258
+ expect(problems()).toEqual([]);
259
+ });
260
+ });
@@ -287,7 +287,7 @@ describe("the buttons of a file field", () => {
287
287
  });
288
288
 
289
289
  describe("a topic tab of the treedb view", () => {
290
- test("carries a title and an aria-label, with the topic as key", () => {
290
+ test("carries a title and an aria-label: the topic's own name, never translated", () => {
291
291
  const remote = gobj_create_service(`tremote${++seq}`, "C_TEST_NAMED_REMOTE", {}, yuno);
292
292
  gobj_change_state(remote, "ST_SESSION");
293
293
  expect(gobj_current_state(remote)).toBe("ST_SESSION");
@@ -309,7 +309,13 @@ describe("a topic tab of the treedb view", () => {
309
309
  }, remote);
310
310
 
311
311
  const $tab = gobj_read_attr(topics, "$container").querySelector(`a[href="ttopics${seq}?users"]`);
312
- expect_named($tab, "users");
312
+ expect(!!$tab, "the tab is drawn").toBe(true);
313
+ expect($tab.getAttribute("title")).toBe("users");
314
+ expect($tab.getAttribute("aria-label")).toBe("users");
315
+ expect($tab.hasAttribute("data-i18n-title")).toBe(false);
316
+ expect($tab.hasAttribute("data-i18n-aria-label")).toBe(false);
317
+ expect($tab.querySelector("[data-i18n]")).toBe(null);
318
+ expect($tab.textContent.trim()).toBe("users");
313
319
  });
314
320
  });
315
321