@gradio/core 0.0.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.
- package/CHANGELOG.md +32 -0
- package/LICENSE +201 -0
- package/blocks.ts +1 -0
- package/index.ts +4 -0
- package/login.ts +1 -0
- package/package.json +83 -0
- package/public/favicon.png +0 -0
- package/public/static/img/Bunny.obj +7474 -0
- package/public/static/img/Duck.glb +0 -0
- package/public/static/img/api-logo.svg +4 -0
- package/public/static/img/camera.svg +1 -0
- package/public/static/img/clear.svg +67 -0
- package/public/static/img/edit.svg +39 -0
- package/public/static/img/javascript.svg +16 -0
- package/public/static/img/logo.svg +19 -0
- package/public/static/img/logo_error.svg +134 -0
- package/public/static/img/python.svg +20 -0
- package/public/static/img/undo-solid.svg +1 -0
- package/src/Blocks.svelte +792 -0
- package/src/Embed.svelte +197 -0
- package/src/Login.stories.svelte +33 -0
- package/src/Login.svelte +111 -0
- package/src/MountComponents.svelte +30 -0
- package/src/Render.svelte +103 -0
- package/src/RenderComponent.svelte +67 -0
- package/src/api_docs/ApiBanner.svelte +118 -0
- package/src/api_docs/ApiDocs.svelte +418 -0
- package/src/api_docs/ApiRecorder.svelte +75 -0
- package/src/api_docs/CodeSnippet.svelte +198 -0
- package/src/api_docs/CopyButton.svelte +17 -0
- package/src/api_docs/EndpointDetail.svelte +37 -0
- package/src/api_docs/InputPayload.svelte +155 -0
- package/src/api_docs/InstallSnippet.svelte +59 -0
- package/src/api_docs/NoApi.svelte +74 -0
- package/src/api_docs/ParametersSnippet.svelte +106 -0
- package/src/api_docs/RecordingSnippet.svelte +224 -0
- package/src/api_docs/ResponseSnippet.svelte +97 -0
- package/src/api_docs/TryButton.svelte +19 -0
- package/src/api_docs/img/api-logo.svg +4 -0
- package/src/api_docs/img/bash.svg +8 -0
- package/src/api_docs/img/clear.svelte +19 -0
- package/src/api_docs/img/javascript.svg +16 -0
- package/src/api_docs/img/python.svg +20 -0
- package/src/api_docs/index.ts +2 -0
- package/src/api_docs/utils.ts +143 -0
- package/src/css.ts +116 -0
- package/src/gradio_helper.ts +7 -0
- package/src/i18n.test.ts +27 -0
- package/src/i18n.ts +36 -0
- package/src/images/lightning.svg +2 -0
- package/src/images/logo.svg +19 -0
- package/src/images/play.svg +2 -0
- package/src/images/spaces.svg +7 -0
- package/src/init.test.ts +521 -0
- package/src/init.ts +590 -0
- package/src/lang/BCP47_codes.js +58 -0
- package/src/lang/README.md +11 -0
- package/src/lang/ar.json +16 -0
- package/src/lang/ca.json +19 -0
- package/src/lang/ckb.json +108 -0
- package/src/lang/de.json +16 -0
- package/src/lang/en.json +118 -0
- package/src/lang/es.json +17 -0
- package/src/lang/eu.json +16 -0
- package/src/lang/fa.json +16 -0
- package/src/lang/fr.json +30 -0
- package/src/lang/he.json +16 -0
- package/src/lang/hi.json +16 -0
- package/src/lang/ja.json +16 -0
- package/src/lang/ko.json +16 -0
- package/src/lang/lt.json +16 -0
- package/src/lang/nl.json +16 -0
- package/src/lang/pl.json +16 -0
- package/src/lang/pt-BR.json +19 -0
- package/src/lang/ru.json +118 -0
- package/src/lang/ta.json +16 -0
- package/src/lang/tr.json +16 -0
- package/src/lang/uk.json +16 -0
- package/src/lang/ur.json +16 -0
- package/src/lang/uz.json +15 -0
- package/src/lang/zh-CN.json +115 -0
- package/src/lang/zh-TW.json +16 -0
- package/src/s-blocks.ts +1 -0
- package/src/s-login.ts +1 -0
- package/src/stores.ts +165 -0
- package/src/types.ts +106 -0
- package/src/vite-env-override.d.ts +20 -0
package/src/init.test.ts
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import { describe, test, expect, vi } from "vitest";
|
|
2
|
+
import { spy } from "tinyspy";
|
|
3
|
+
import { setupServer } from "msw/node";
|
|
4
|
+
import { http, HttpResponse } from "msw";
|
|
5
|
+
import type { client_return } from "@gradio/client";
|
|
6
|
+
import { Dependency, TargetMap } from "./types";
|
|
7
|
+
import {
|
|
8
|
+
process_frontend_fn,
|
|
9
|
+
create_target_meta,
|
|
10
|
+
determine_interactivity,
|
|
11
|
+
process_server_fn,
|
|
12
|
+
get_component
|
|
13
|
+
} from "./init";
|
|
14
|
+
|
|
15
|
+
describe("process_frontend_fn", () => {
|
|
16
|
+
test("empty source code returns null", () => {
|
|
17
|
+
const source = "";
|
|
18
|
+
|
|
19
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
20
|
+
expect(fn).toBe(null);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("falsey source code returns null: false", () => {
|
|
24
|
+
const source = false;
|
|
25
|
+
|
|
26
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
27
|
+
expect(fn).toBe(null);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("falsey source code returns null: undefined", () => {
|
|
31
|
+
const source = undefined;
|
|
32
|
+
|
|
33
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
34
|
+
expect(fn).toBe(null);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("falsey source code returns null: null", () => {
|
|
38
|
+
const source = null;
|
|
39
|
+
|
|
40
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
41
|
+
expect(fn).toBe(null);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("source code returns a function", () => {
|
|
45
|
+
const source = "(arg) => arg";
|
|
46
|
+
|
|
47
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
48
|
+
expect(typeof fn).toBe("function");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("arrays of values can be passed to the generated function", async () => {
|
|
52
|
+
const source = "(arg) => arg";
|
|
53
|
+
|
|
54
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
55
|
+
if (fn) {
|
|
56
|
+
await expect(fn([1])).resolves.toEqual([1]);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("arrays of many values can be passed", async () => {
|
|
61
|
+
const source = "(...args) => args";
|
|
62
|
+
|
|
63
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
64
|
+
if (fn) {
|
|
65
|
+
await expect(fn([1, 2, 3, 4, 5, 6])).resolves.toEqual([1, 2, 3, 4, 5, 6]);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("The generated function returns a promise", () => {
|
|
70
|
+
const source = "(arg) => arg";
|
|
71
|
+
|
|
72
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
73
|
+
if (fn) {
|
|
74
|
+
expect(fn([1])).toBeInstanceOf(Promise);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("The generated function is callable and returns the expected value", async () => {
|
|
79
|
+
const source = "(arg) => arg";
|
|
80
|
+
|
|
81
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
82
|
+
if (fn) {
|
|
83
|
+
await expect(fn([1])).resolves.toEqual([1]);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("The return value of the function is wrapped in an array if there is no backend function and the input length is 1", async () => {
|
|
88
|
+
const source = "(arg) => arg";
|
|
89
|
+
|
|
90
|
+
const fn = process_frontend_fn(source, false, 1, 1);
|
|
91
|
+
if (fn) {
|
|
92
|
+
await expect(fn([1])).resolves.toEqual([1]);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("The return value of the function is not wrapped in an array if there is no backend function and the input length is greater than 1", async () => {
|
|
97
|
+
const source = "(arg) => arg";
|
|
98
|
+
|
|
99
|
+
const fn = process_frontend_fn(source, false, 2, 2);
|
|
100
|
+
if (fn) {
|
|
101
|
+
await expect(fn([1])).resolves.toEqual(1);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("The return value of the function is wrapped in an array if there is a backend function and the input length is 1", async () => {
|
|
106
|
+
const source = "(arg) => arg";
|
|
107
|
+
|
|
108
|
+
const fn = process_frontend_fn(source, true, 1, 1);
|
|
109
|
+
if (fn) {
|
|
110
|
+
await expect(fn([1])).resolves.toEqual([1]);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("The return value of the function is not wrapped in an array if there is a backend function and the input length is greater than 1", async () => {
|
|
115
|
+
const source = "(arg) => arg";
|
|
116
|
+
|
|
117
|
+
const fn = process_frontend_fn(source, true, 2, 2);
|
|
118
|
+
if (fn) {
|
|
119
|
+
await expect(fn([1])).resolves.toEqual(1);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe("create_target_meta", () => {
|
|
125
|
+
test("creates a target map", () => {
|
|
126
|
+
const targets: Dependency["targets"] = [
|
|
127
|
+
[1, "change"],
|
|
128
|
+
[2, "input"],
|
|
129
|
+
[3, "load"]
|
|
130
|
+
];
|
|
131
|
+
const fn_index = 0;
|
|
132
|
+
const target_map = {};
|
|
133
|
+
|
|
134
|
+
const result = create_target_meta(targets, fn_index, target_map);
|
|
135
|
+
expect(result).toEqual({
|
|
136
|
+
1: { change: [0] },
|
|
137
|
+
2: { input: [0] },
|
|
138
|
+
3: { load: [0] }
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("if the target already exists, it adds the new trigger to the list", () => {
|
|
143
|
+
const targets: Dependency["targets"] = [
|
|
144
|
+
[1, "change"],
|
|
145
|
+
[1, "input"],
|
|
146
|
+
[1, "load"]
|
|
147
|
+
];
|
|
148
|
+
const fn_index = 1;
|
|
149
|
+
const target_map: TargetMap = {
|
|
150
|
+
1: { change: [0] }
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const result = create_target_meta(targets, fn_index, target_map);
|
|
154
|
+
expect(result).toEqual({
|
|
155
|
+
1: { change: [0, 1], input: [1], load: [1] }
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("if the trigger already exists, it adds the new function to the list", () => {
|
|
160
|
+
const targets: Dependency["targets"] = [
|
|
161
|
+
[1, "change"],
|
|
162
|
+
[2, "change"],
|
|
163
|
+
[3, "change"]
|
|
164
|
+
];
|
|
165
|
+
const fn_index = 1;
|
|
166
|
+
const target_map: TargetMap = {
|
|
167
|
+
1: { change: [0] },
|
|
168
|
+
2: { change: [0] },
|
|
169
|
+
3: { change: [0] }
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const result = create_target_meta(targets, fn_index, target_map);
|
|
173
|
+
expect(result).toEqual({
|
|
174
|
+
1: { change: [0, 1] },
|
|
175
|
+
2: { change: [0, 1] },
|
|
176
|
+
3: { change: [0, 1] }
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("if the target and trigger already exist, it adds the new function to the list", () => {
|
|
181
|
+
const targets: Dependency["targets"] = [[1, "change"]];
|
|
182
|
+
const fn_index = 1;
|
|
183
|
+
const target_map: TargetMap = {
|
|
184
|
+
1: { change: [0] }
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const result = create_target_meta(targets, fn_index, target_map);
|
|
188
|
+
expect(result).toEqual({
|
|
189
|
+
1: { change: [0, 1] }
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("if the target, trigger and function id already exist, it does not add duplicates", () => {
|
|
194
|
+
const targets: Dependency["targets"] = [[1, "change"]];
|
|
195
|
+
const fn_index = 0;
|
|
196
|
+
const target_map: TargetMap = {
|
|
197
|
+
1: { change: [0] }
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const result = create_target_meta(targets, fn_index, target_map);
|
|
201
|
+
expect(result).toEqual({
|
|
202
|
+
1: { change: [0] }
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe("determine_interactivity", () => {
|
|
208
|
+
test("returns true if the prop is interactive = true", () => {
|
|
209
|
+
const result = determine_interactivity(
|
|
210
|
+
0,
|
|
211
|
+
true,
|
|
212
|
+
"hi",
|
|
213
|
+
new Set([0]),
|
|
214
|
+
new Set([2])
|
|
215
|
+
);
|
|
216
|
+
expect(result).toBe(true);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("returns false if the prop is interactive = false", () => {
|
|
220
|
+
const result = determine_interactivity(
|
|
221
|
+
0,
|
|
222
|
+
false,
|
|
223
|
+
"hi",
|
|
224
|
+
new Set([0]),
|
|
225
|
+
new Set([2])
|
|
226
|
+
);
|
|
227
|
+
expect(result).toBe(false);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("returns true if the component is an input", () => {
|
|
231
|
+
const result = determine_interactivity(
|
|
232
|
+
0,
|
|
233
|
+
undefined,
|
|
234
|
+
"hi",
|
|
235
|
+
new Set([0]),
|
|
236
|
+
new Set([2])
|
|
237
|
+
);
|
|
238
|
+
expect(result).toBe(true);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("returns true if the component is not an input or output and the component has no default value: empty string", () => {
|
|
242
|
+
const result = determine_interactivity(
|
|
243
|
+
2,
|
|
244
|
+
undefined,
|
|
245
|
+
"",
|
|
246
|
+
new Set([0]),
|
|
247
|
+
new Set([1])
|
|
248
|
+
);
|
|
249
|
+
expect(result).toBe(true);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test("returns true if the component is not an input or output and the component has no default value: empty array", () => {
|
|
253
|
+
const result = determine_interactivity(
|
|
254
|
+
2,
|
|
255
|
+
undefined,
|
|
256
|
+
[],
|
|
257
|
+
new Set([0]),
|
|
258
|
+
new Set([1])
|
|
259
|
+
);
|
|
260
|
+
expect(result).toBe(true);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("returns true if the component is not an input or output and the component has no default value: boolean", () => {
|
|
264
|
+
const result = determine_interactivity(
|
|
265
|
+
2,
|
|
266
|
+
undefined,
|
|
267
|
+
false,
|
|
268
|
+
new Set([0]),
|
|
269
|
+
new Set([1])
|
|
270
|
+
);
|
|
271
|
+
expect(result).toBe(true);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("returns true if the component is not an input or output and the component has no default value: undefined", () => {
|
|
275
|
+
const result = determine_interactivity(
|
|
276
|
+
2,
|
|
277
|
+
undefined,
|
|
278
|
+
undefined,
|
|
279
|
+
new Set([0]),
|
|
280
|
+
new Set([1])
|
|
281
|
+
);
|
|
282
|
+
expect(result).toBe(true);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test("returns true if the component is not an input or output and the component has no default value: null", () => {
|
|
286
|
+
const result = determine_interactivity(
|
|
287
|
+
2,
|
|
288
|
+
undefined,
|
|
289
|
+
null,
|
|
290
|
+
new Set([0]),
|
|
291
|
+
new Set([1])
|
|
292
|
+
);
|
|
293
|
+
expect(result).toBe(true);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test("returns true if the component is not an input or output and the component has no default value: 0", () => {
|
|
297
|
+
const result = determine_interactivity(
|
|
298
|
+
2,
|
|
299
|
+
undefined,
|
|
300
|
+
0,
|
|
301
|
+
new Set([0]),
|
|
302
|
+
new Set([1])
|
|
303
|
+
);
|
|
304
|
+
expect(result).toBe(true);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test("returns false if the component is not an input or output and the component has a default value", () => {
|
|
308
|
+
const result = determine_interactivity(
|
|
309
|
+
2,
|
|
310
|
+
undefined,
|
|
311
|
+
"hello",
|
|
312
|
+
new Set([0]),
|
|
313
|
+
new Set([1])
|
|
314
|
+
);
|
|
315
|
+
expect(result).toBe(false);
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
describe("process_server_fn", () => {
|
|
320
|
+
test("returns an object", () => {
|
|
321
|
+
const result = process_server_fn(1, ["fn1", "fn2"], {} as any);
|
|
322
|
+
expect(result).toBeTypeOf("object");
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
test("returns an object with the correct keys", () => {
|
|
326
|
+
const result = process_server_fn(1, ["fn1", "fn2"], {} as any);
|
|
327
|
+
expect(Object.keys(result)).toEqual(["fn1", "fn2"]);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
test("returns an object with the correct keys and values", () => {
|
|
331
|
+
const app = {
|
|
332
|
+
component_server: async (id: number, fn: string, args: any) => {
|
|
333
|
+
return args;
|
|
334
|
+
}
|
|
335
|
+
} as client_return;
|
|
336
|
+
|
|
337
|
+
const result = process_server_fn(1, ["fn1", "fn2"], app);
|
|
338
|
+
expect(Object.keys(result)).toEqual(["fn1", "fn2"]);
|
|
339
|
+
|
|
340
|
+
expect(result.fn1).toBeInstanceOf(Function);
|
|
341
|
+
expect(result.fn2).toBeInstanceOf(Function);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test("returned server functions should resolve to a promise", async () => {
|
|
345
|
+
const app = {
|
|
346
|
+
component_server: async (id: number, fn: string, args: any) => {
|
|
347
|
+
return args;
|
|
348
|
+
}
|
|
349
|
+
} as client_return;
|
|
350
|
+
|
|
351
|
+
const result = process_server_fn(1, ["fn1", "fn2"], app);
|
|
352
|
+
const response = result.fn1("hello");
|
|
353
|
+
expect(response).toBeInstanceOf(Promise);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test("the functions call the clients component_server function with the correct arguments ", async () => {
|
|
357
|
+
const mock = spy(async (id: number, fn: string, args: any) => {
|
|
358
|
+
return args;
|
|
359
|
+
});
|
|
360
|
+
const app = {
|
|
361
|
+
component_server: mock as any
|
|
362
|
+
} as client_return;
|
|
363
|
+
|
|
364
|
+
const result = process_server_fn(1, ["fn1", "fn2"], app as client_return);
|
|
365
|
+
const response = await result.fn1("hello");
|
|
366
|
+
expect(response).toBe("hello");
|
|
367
|
+
expect(mock.calls).toEqual([[1, "fn1", "hello"]]);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
test("if there are no server functions, it returns an empty object", () => {
|
|
371
|
+
const result = process_server_fn(1, undefined, {} as any);
|
|
372
|
+
expect(result).toEqual({});
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
describe("get_component", () => {
|
|
377
|
+
test("returns an object", () => {
|
|
378
|
+
const result = get_component("test-component-one", "class_id", "root", []);
|
|
379
|
+
expect(result.component).toBeTypeOf("object");
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test("returns an object with the correct keys", () => {
|
|
383
|
+
const result = get_component("test-component-one", "class_id", "root", []);
|
|
384
|
+
expect(Object.keys(result)).toEqual([
|
|
385
|
+
"component",
|
|
386
|
+
"name",
|
|
387
|
+
"example_components"
|
|
388
|
+
]);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
test("the component key is a promise", () => {
|
|
392
|
+
const result = get_component("test-component-one", "class_id", "root", []);
|
|
393
|
+
expect(result.component).toBeInstanceOf(Promise);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
test("the resolved component key is an object", async () => {
|
|
397
|
+
const result = get_component("test-component-one", "class_id", "root", []);
|
|
398
|
+
const o = await result.component;
|
|
399
|
+
|
|
400
|
+
expect(o).toBeTypeOf("object");
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
test("getting the same component twice should return the same promise", () => {
|
|
404
|
+
const result = get_component("test-component-one", "class_id", "root", []);
|
|
405
|
+
const result_two = get_component(
|
|
406
|
+
"test-component-one",
|
|
407
|
+
"class_id",
|
|
408
|
+
"root",
|
|
409
|
+
[]
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
expect(result.component).toBe(result_two.component);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
test("if example components are not provided, the example_components key is undefined", async () => {
|
|
416
|
+
const result = get_component("dataset", "class_id", "root", []);
|
|
417
|
+
expect(result.example_components).toBe(undefined);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test("if the type is not a dataset, the example_components key is undefined", async () => {
|
|
421
|
+
const result = get_component("test-component-one", "class_id", "root", []);
|
|
422
|
+
expect(result.example_components).toBe(undefined);
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
test("when the type is a dataset, returns an object with the correct keys and values and example components", () => {
|
|
426
|
+
const result = get_component(
|
|
427
|
+
"dataset",
|
|
428
|
+
"class_id",
|
|
429
|
+
"root",
|
|
430
|
+
[
|
|
431
|
+
{
|
|
432
|
+
type: "test-component-one",
|
|
433
|
+
component_class_id: "example_class_id",
|
|
434
|
+
id: 1,
|
|
435
|
+
props: {
|
|
436
|
+
value: "hi",
|
|
437
|
+
interactive: false
|
|
438
|
+
},
|
|
439
|
+
has_modes: false,
|
|
440
|
+
instance: {} as any,
|
|
441
|
+
component: {} as any
|
|
442
|
+
}
|
|
443
|
+
],
|
|
444
|
+
["test-component-one"]
|
|
445
|
+
);
|
|
446
|
+
expect(result.component).toBeTypeOf("object");
|
|
447
|
+
expect(result.example_components).toBeInstanceOf(Map);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
test("when example components are returned, returns an object with the correct keys and values and example components", () => {
|
|
451
|
+
const result = get_component(
|
|
452
|
+
"dataset",
|
|
453
|
+
"class_id",
|
|
454
|
+
"root",
|
|
455
|
+
[
|
|
456
|
+
{
|
|
457
|
+
type: "test-component-one",
|
|
458
|
+
component_class_id: "example_class_id",
|
|
459
|
+
id: 1,
|
|
460
|
+
props: {
|
|
461
|
+
value: "hi",
|
|
462
|
+
interactive: false
|
|
463
|
+
},
|
|
464
|
+
has_modes: false,
|
|
465
|
+
instance: {} as any,
|
|
466
|
+
component: {} as any
|
|
467
|
+
}
|
|
468
|
+
],
|
|
469
|
+
["test-component-one"]
|
|
470
|
+
);
|
|
471
|
+
expect(result.example_components?.get("test-component-one")).toBeTypeOf(
|
|
472
|
+
"object"
|
|
473
|
+
);
|
|
474
|
+
expect(result.example_components?.get("test-component-one")).toBeInstanceOf(
|
|
475
|
+
Promise
|
|
476
|
+
);
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
test("if the component is not found then it should request the component from the server", async () => {
|
|
480
|
+
const api_url = "example.com";
|
|
481
|
+
const id = "test-random";
|
|
482
|
+
const variant = "component";
|
|
483
|
+
const handlers = [
|
|
484
|
+
http.get(`${api_url}/custom_component/${id}/${variant}/style.css`, () => {
|
|
485
|
+
return new HttpResponse('console.log("boo")', {
|
|
486
|
+
status: 200,
|
|
487
|
+
headers: {
|
|
488
|
+
"Content-Type": "text/css"
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
})
|
|
492
|
+
];
|
|
493
|
+
|
|
494
|
+
// vi.mock calls are always hoisted out of the test function to the top of the file
|
|
495
|
+
// so we need to use vi.hoisted to hoist the mock function above the vi.mock call
|
|
496
|
+
const { mock } = vi.hoisted(() => {
|
|
497
|
+
return { mock: vi.fn() };
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
vi.mock(
|
|
501
|
+
`example.com/custom_component/test-random/component/index.js`,
|
|
502
|
+
async () => {
|
|
503
|
+
mock();
|
|
504
|
+
return {
|
|
505
|
+
default: {
|
|
506
|
+
default: "HELLO"
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
);
|
|
511
|
+
|
|
512
|
+
const server = setupServer(...handlers);
|
|
513
|
+
server.listen();
|
|
514
|
+
|
|
515
|
+
await get_component("test-random", id, api_url, []).component;
|
|
516
|
+
|
|
517
|
+
expect(mock).toHaveBeenCalled();
|
|
518
|
+
|
|
519
|
+
server.close();
|
|
520
|
+
});
|
|
521
|
+
});
|