@yuneta/gobj-ui 7.31.1 → 7.31.2

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,222 @@
1
+ /***********************************************************************
2
+ * treedb_schema_lit_edge.wiring.test.js
3
+ *
4
+ * The schema graph (C_YUI_TREEDB_SCHEMA) has a minimap, and a click
5
+ * on an edge LIGHTS it: the line takes the `lit` state, and its two
6
+ * cards are repainted in amber with the hook row and the fkey row
7
+ * washed. A second click on the same edge, or a click on the
8
+ * canvas, puts it out; a theme switch keeps it lit.
9
+ *
10
+ * Driven through the real gclass, on a document double, with a G6
11
+ * double that keeps the data it is given.
12
+ *
13
+ * Copyright (c) 2026, ArtGins.
14
+ * All Rights Reserved.
15
+ ***********************************************************************/
16
+ import {describe, test, expect, beforeAll, vi} from "vitest";
17
+ import {install_dom_double} from "../test/dom_double.js";
18
+
19
+ install_dom_double();
20
+
21
+ if(typeof globalThis.ResizeObserver === "undefined") {
22
+ globalThis.ResizeObserver = class {
23
+ observe() {
24
+ }
25
+ unobserve() {
26
+ }
27
+ disconnect() {
28
+ }
29
+ };
30
+ }
31
+
32
+ const graphs = vi.hoisted(() => []);
33
+ vi.mock("@antv/g6", async (importOriginal) => {
34
+ const real = await importOriginal();
35
+ class FakeGraph {
36
+ constructor(options) {
37
+ this.options = options;
38
+ this.handlers = {};
39
+ this.nodes = {};
40
+ this.edges = {};
41
+ for(const nd of options.data.nodes) {
42
+ this.nodes[nd.id] = JSON.parse(JSON.stringify(nd));
43
+ }
44
+ for(const ed of options.data.edges) {
45
+ this.edges[ed.id] = {...JSON.parse(JSON.stringify(ed)), states: []};
46
+ }
47
+ this.edge_options = options.edge;
48
+ graphs.push(this);
49
+ return new Proxy(this, {
50
+ get: (t, k) => (k in t) ? t[k] : (() => undefined),
51
+ });
52
+ }
53
+ render() {
54
+ return Promise.resolve();
55
+ }
56
+ on(name, fn) {
57
+ (this.handlers[name] = this.handlers[name] || []).push(fn);
58
+ }
59
+ emit(name, ev) {
60
+ for(const fn of (this.handlers[name] || [])) {
61
+ fn(ev);
62
+ }
63
+ }
64
+ getZoom() {
65
+ return 1;
66
+ }
67
+ getNodeData(id) {
68
+ if(id === undefined) {
69
+ return Object.values(this.nodes);
70
+ }
71
+ if(!this.nodes[id]) {
72
+ throw new Error(`node ${id} not found`);
73
+ }
74
+ return this.nodes[id];
75
+ }
76
+ getEdgeData(id) {
77
+ if(!this.edges[id]) {
78
+ throw new Error(`edge ${id} not found`);
79
+ }
80
+ return this.edges[id];
81
+ }
82
+ updateNodeData(updates) {
83
+ for(const u of updates) {
84
+ Object.assign(this.nodes[u.id].style, u.style);
85
+ }
86
+ }
87
+ updateData(data) {
88
+ for(const u of (data.edges || [])) {
89
+ this.edges[u.id].states = u.states;
90
+ }
91
+ }
92
+ setEdge(options) {
93
+ this.edge_options = options;
94
+ }
95
+ getPlugins() {
96
+ return [];
97
+ }
98
+ getBehaviors() {
99
+ return [];
100
+ }
101
+ getOptions() {
102
+ return {behaviors: []};
103
+ }
104
+ getCanvas() {
105
+ return {getLayers: () => ({}), getContainer: () => null};
106
+ }
107
+ draw() {
108
+ return Promise.resolve();
109
+ }
110
+ destroy() {
111
+ }
112
+ }
113
+ return {...real, Graph: FakeGraph};
114
+ });
115
+
116
+ const {
117
+ SDATA_END,
118
+ gclass_create,
119
+ gobj_start_up, gobj_create_yuno, gobj_create, gobj_start, gobj_send_event,
120
+ set_log_callback,
121
+ } = await import("@yuneta/gobj-js");
122
+ const {EdgeEvent, CanvasEvent} = await import("@antv/g6");
123
+ const {register_c_yui_treedb_schema} = await import("./c_yui_treedb_schema.js");
124
+
125
+ const logged = [];
126
+
127
+ const DESCS = {
128
+ places: {
129
+ id: "places",
130
+ cols: [
131
+ {id: "id", flag: ["persistent", "required"], type: "string"},
132
+ {id: "sites", flag: ["hook"], type: "dict", hook: {sites: "place"}},
133
+ ],
134
+ },
135
+ sites: {
136
+ id: "sites",
137
+ cols: [
138
+ {id: "id", flag: ["persistent", "required"], type: "string"},
139
+ {id: "name", flag: ["persistent"], type: "string"},
140
+ {id: "place", flag: ["fkey"], type: "list", fkey: {places: "sites"}},
141
+ ],
142
+ },
143
+ };
144
+
145
+ let yuno = null;
146
+ let seq = 0;
147
+
148
+ beforeAll(() => {
149
+ set_log_callback((priority, ...args) => {
150
+ logged.push(args.join(" "));
151
+ });
152
+ gobj_start_up(null, null, null, null, null, null, null);
153
+ gclass_create("C_LIT_HOST", [], [["ST_IDLE", []]], {}, 0,
154
+ [SDATA_END()], {}, 0, 0, 0, 0);
155
+ register_c_yui_treedb_schema();
156
+ yuno = gobj_create_yuno("yuno", "C_LIT_HOST", {});
157
+ });
158
+
159
+ async function mount()
160
+ {
161
+ let host = gobj_create(`host_${++seq}`, "C_LIT_HOST", {}, yuno);
162
+ let view = gobj_create(`schema_${seq}`, "C_YUI_TREEDB_SCHEMA", {descs: DESCS}, host);
163
+ gobj_start(view);
164
+ await Promise.resolve();
165
+ if(!graphs.length) {
166
+ throw new Error(logged.join("\n"));
167
+ }
168
+ return {view, graph: graphs[graphs.length - 1]};
169
+ }
170
+
171
+ const EDGE = "places|sites|sites|place";
172
+
173
+ describe("C_YUI_TREEDB_SCHEMA: minimap and lit edge", () => {
174
+ test("the graph carries a minimap anchored by css", async () => {
175
+ const {graph} = await mount();
176
+ const minimap = (graph.options.plugins || []).find((p) => p.key === "minimap");
177
+ expect(minimap).toBeTruthy();
178
+ expect(minimap.type).toBe("minimap");
179
+ expect(minimap.containerStyle).toMatchObject({top: "auto", bottom: "12px"});
180
+ expect(graph.edge_options.state.lit).toBeTruthy();
181
+ });
182
+
183
+ test("an edge click lights the edge, its two cards and the two rows", async () => {
184
+ const {graph} = await mount();
185
+ const before = graph.nodes.sites.style.innerHTML;
186
+ expect(before).not.toContain("3px solid");
187
+
188
+ graph.emit(EdgeEvent.CLICK, {target: {id: EDGE}});
189
+
190
+ expect(graph.edges[EDGE].states).toEqual(["lit"]);
191
+ for(const topic of ["places", "sites"]) {
192
+ expect(graph.nodes[topic].style.innerHTML).toContain("3px solid");
193
+ }
194
+ /* the fkey row of the child and the hook row of the parent,
195
+ * and not the other rows */
196
+ const washed = (html) => (html.match(/color-mix\(in srgb, #[0-9a-f]+ \d+%, transparent\)/g) || []).length;
197
+ expect(washed(graph.nodes.sites.style.innerHTML)).toBe(1);
198
+ expect(washed(graph.nodes.places.style.innerHTML)).toBe(1);
199
+ });
200
+
201
+ test("a second click on it, or a canvas click, puts it out", async () => {
202
+ const {graph} = await mount();
203
+ graph.emit(EdgeEvent.CLICK, {target: {id: EDGE}});
204
+ graph.emit(EdgeEvent.CLICK, {target: {id: EDGE}});
205
+ expect(graph.edges[EDGE].states).toEqual([]);
206
+ expect(graph.nodes.sites.style.innerHTML).not.toContain("3px solid");
207
+
208
+ graph.emit(EdgeEvent.CLICK, {target: {id: EDGE}});
209
+ graph.emit(CanvasEvent.CLICK, {});
210
+ expect(graph.edges[EDGE].states).toEqual([]);
211
+ expect(graph.nodes.places.style.innerHTML).not.toContain("3px solid");
212
+ });
213
+
214
+ test("a theme switch keeps the lit cards lit and the lit state defined", async () => {
215
+ const {view, graph} = await mount();
216
+ graph.emit(EdgeEvent.CLICK, {target: {id: EDGE}});
217
+ gobj_send_event(view, "EV_THEME", {theme: "light"}, view);
218
+ expect(graph.nodes.sites.style.innerHTML).toContain("3px solid");
219
+ expect(graph.edge_options.state.lit.stroke).toBe("#b45309");
220
+ expect(logged.filter((l) => /ERROR|failed/i.test(l))).toEqual([]);
221
+ });
222
+ });