-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfireQuery.test.ts
More file actions
128 lines (119 loc) · 3.45 KB
/
Copy pathfireQuery.test.ts
File metadata and controls
128 lines (119 loc) · 3.45 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { beforeEach, describe, expect, it, vi } from "vitest";
type ConditionToDatalogArgs = {
source: string;
target: string;
};
vi.mock("~/utils/conditionToDatalog", () => ({
default: vi.fn(({ source, target }: ConditionToDatalogArgs) => [
{
type: "data-pattern",
arguments: [
{ type: "variable", value: source },
{ type: "constant", value: ":rel" },
/^:in /.test(target)
? { type: "variable", value: target.substring(4) }
: { type: "constant", value: '"value"' },
],
},
]),
}));
vi.mock("~/utils/predefinedSelections", () => ({
default: [
{
test: /^created$/,
pull: () => "(pull ?node [:create/time])",
mapper: (r: Record<string, string>) => r[":create/time"] || "",
},
],
}));
vi.mock("roamjs-components/util/env", () => ({ getNodeEnv: () => "test" }));
import fireQuery, { fireQuerySync, getDatalogQuery } from "~/utils/fireQuery";
describe("getDatalogQuery", () => {
it("includes :in variables and de-duplicates expected inputs", async () => {
const built = getDatalogQuery({
conditions: [
{
type: "clause",
relation: "r",
source: "node",
target: ":in title",
uid: "1",
not: false,
},
{
type: "clause",
relation: "r",
source: "node",
target: ":in title",
uid: "2",
not: false,
},
],
selections: [{ uid: "s1", text: "created", label: "Created" }],
inputs: { title: "Graph" },
});
expect(built.query).toContain(":in $ ?title");
expect(built.inputs).toEqual(["Graph"]);
const formatted = await built.formatResult([
{ ":node/title": "A", ":block/uid": "u1" },
{ ":block/uid": "u1" },
{ ":create/time": "123" },
]);
expect(formatted).toMatchObject({ text: "A", uid: "u1", Created: "123" });
});
});
describe("fireQuery", () => {
beforeEach(() => {
(globalThis as { window: unknown }).window = {
roamAlphaAPI: {
data: {
async: {
fast: {
q: vi
.fn()
.mockResolvedValue([
[
{ ":node/title": "Local", ":block/uid": "l1" },
{ ":block/uid": "l1" },
],
]),
},
},
backend: {
q: vi
.fn()
.mockResolvedValue([
[
{ ":node/title": "Remote", ":block/uid": "r1" },
{ ":block/uid": "r1" },
],
]),
},
fast: {
q: vi
.fn()
.mockReturnValue([
[{ ":node/title": "Sync", ":block/uid": "s1" }],
]),
},
},
},
};
});
it("uses backend queries by default and maps output", async () => {
const results = await fireQuery({ conditions: [], selections: [] });
expect(results[0]).toMatchObject({ text: "Remote", uid: "r1" });
});
it("uses async fast query when local=true", async () => {
const results = await fireQuery({
conditions: [],
selections: [],
local: true,
});
expect(results[0]).toMatchObject({ text: "Local", uid: "l1" });
});
it("returns sync mapped results", () => {
const results = fireQuerySync({ conditions: [], selections: [] });
expect(results).toEqual([{ text: "Sync", uid: "s1" }]);
});
});