@solidjs/router 0.16.0 → 0.16.1

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.
Files changed (65) hide show
  1. package/dist/data/action.js +6 -5
  2. package/dist/data/createAsync.js +3 -0
  3. package/dist/data/events.d.ts +8 -1
  4. package/dist/data/events.js +2 -2
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.jsx +1 -1
  7. package/dist/routers/HashRouter.js +1 -1
  8. package/dist/routers/MemoryRouter.js +1 -1
  9. package/dist/routers/Router.js +1 -1
  10. package/dist/routing.d.ts +2 -1
  11. package/dist/routing.js +1 -0
  12. package/dist/types.d.ts +1 -1
  13. package/dist/utils.d.ts +1 -0
  14. package/dist/utils.js +8 -0
  15. package/package.json +2 -2
  16. package/dist/src/components.d.ts +0 -31
  17. package/dist/src/components.jsx +0 -39
  18. package/dist/src/data/action.d.ts +0 -17
  19. package/dist/src/data/action.js +0 -163
  20. package/dist/src/data/action.spec.d.ts +0 -1
  21. package/dist/src/data/action.spec.js +0 -297
  22. package/dist/src/data/createAsync.d.ts +0 -32
  23. package/dist/src/data/createAsync.js +0 -96
  24. package/dist/src/data/createAsync.spec.d.ts +0 -1
  25. package/dist/src/data/createAsync.spec.js +0 -196
  26. package/dist/src/data/events.d.ts +0 -9
  27. package/dist/src/data/events.js +0 -123
  28. package/dist/src/data/events.spec.d.ts +0 -1
  29. package/dist/src/data/events.spec.js +0 -567
  30. package/dist/src/data/index.d.ts +0 -4
  31. package/dist/src/data/index.js +0 -4
  32. package/dist/src/data/query.d.ts +0 -23
  33. package/dist/src/data/query.js +0 -232
  34. package/dist/src/data/query.spec.d.ts +0 -1
  35. package/dist/src/data/query.spec.js +0 -354
  36. package/dist/src/data/response.d.ts +0 -4
  37. package/dist/src/data/response.js +0 -42
  38. package/dist/src/data/response.spec.d.ts +0 -1
  39. package/dist/src/data/response.spec.js +0 -165
  40. package/dist/src/index.d.ts +0 -7
  41. package/dist/src/index.jsx +0 -6
  42. package/dist/src/lifecycle.d.ts +0 -5
  43. package/dist/src/lifecycle.js +0 -69
  44. package/dist/src/routers/HashRouter.d.ts +0 -9
  45. package/dist/src/routers/HashRouter.js +0 -41
  46. package/dist/src/routers/MemoryRouter.d.ts +0 -24
  47. package/dist/src/routers/MemoryRouter.js +0 -57
  48. package/dist/src/routers/Router.d.ts +0 -9
  49. package/dist/src/routers/Router.js +0 -45
  50. package/dist/src/routers/StaticRouter.d.ts +0 -6
  51. package/dist/src/routers/StaticRouter.js +0 -15
  52. package/dist/src/routers/components.d.ts +0 -27
  53. package/dist/src/routers/components.jsx +0 -118
  54. package/dist/src/routers/createRouter.d.ts +0 -10
  55. package/dist/src/routers/createRouter.js +0 -41
  56. package/dist/src/routers/index.d.ts +0 -11
  57. package/dist/src/routers/index.js +0 -6
  58. package/dist/src/routing.d.ts +0 -175
  59. package/dist/src/routing.js +0 -560
  60. package/dist/src/types.d.ts +0 -200
  61. package/dist/src/types.js +0 -1
  62. package/dist/src/utils.d.ts +0 -13
  63. package/dist/src/utils.js +0 -185
  64. package/dist/test/helpers.d.ts +0 -6
  65. package/dist/test/helpers.js +0 -50
@@ -1,297 +0,0 @@
1
- import { createRoot } from "solid-js";
2
- import { vi } from "vitest";
3
- import { action, useAction, useSubmission, useSubmissions, actions } from "./action.js";
4
- import { createMockRouter } from "../../test/helpers.js";
5
- vi.mock("../src/utils.js", () => ({
6
- mockBase: "https://action"
7
- }));
8
- let mockRouterContext;
9
- vi.mock("../routing.js", () => ({
10
- useRouter: () => mockRouterContext,
11
- createRouterContext: () => createMockRouter(),
12
- RouterContextObj: {},
13
- RouteContextObj: {},
14
- useRoute: () => mockRouterContext.base,
15
- useResolvedPath: () => "/",
16
- useHref: () => "/",
17
- useNavigate: () => vi.fn(),
18
- useLocation: () => mockRouterContext.location,
19
- useRouteData: () => undefined,
20
- useMatch: () => null,
21
- useParams: () => ({}),
22
- useSearchParams: () => [{}, vi.fn()],
23
- useIsRouting: () => false,
24
- usePreloadRoute: () => vi.fn(),
25
- useBeforeLeave: () => vi.fn()
26
- }));
27
- describe("action", () => {
28
- beforeEach(() => {
29
- actions.clear();
30
- mockRouterContext = createMockRouter();
31
- });
32
- test("should create an action function with `url` property", () => {
33
- const testAction = action(async (data) => {
34
- return `processed: ${data}`;
35
- }, "test-action");
36
- expect(typeof testAction).toBe("function");
37
- expect(testAction.url).toBe("https://action/test-action");
38
- });
39
- test("should create action with auto-generated hash when no `name` provided", () => {
40
- const testFn = async (data) => `result: ${data}`;
41
- const testAction = action(testFn);
42
- expect(testAction.url).toMatch(/^https:\/\/action\/-?\d+$/);
43
- expect(testAction.name).toMatch(/^-?\d+$/);
44
- });
45
- test("should use it as `name` when `options` are provided as a string", () => {
46
- const testFn = async (data) => `result: ${data}`;
47
- const testAction = action(testFn, "test-action");
48
- expect(testAction.url).toMatch("https://action/test-action");
49
- expect(testAction.name).toBe("test-action");
50
- });
51
- test("should use `name` when provided in object options", () => {
52
- const testFn = async (data) => `result: ${data}`;
53
- const testAction = action(testFn, { name: "test-action" });
54
- expect(testAction.url).toMatch("https://action/test-action");
55
- expect(testAction.name).toBe("test-action");
56
- });
57
- test("should register action in actions map", () => {
58
- const testAction = action(async () => "result", "register-test");
59
- expect(actions.has(testAction.url)).toBe(true);
60
- expect(actions.get(testAction.url)).toBe(testAction);
61
- });
62
- test("should support `.with` method for currying arguments", () => {
63
- const baseAction = action(async (prefix, data) => {
64
- return `${prefix}: ${data}`;
65
- }, "with-test");
66
- const curriedAction = baseAction.with("PREFIX");
67
- expect(typeof curriedAction).toBe("function");
68
- expect(curriedAction.url).toMatch(/with-test\?args=/);
69
- });
70
- test("should execute action and create submission", async () => {
71
- return createRoot(async () => {
72
- const testAction = action(async (data) => {
73
- return `processed: ${data}`;
74
- }, "execute-test");
75
- const boundAction = useAction(testAction);
76
- const promise = boundAction("test-data");
77
- const submissions = mockRouterContext.submissions[0]();
78
- expect(submissions).toHaveLength(1);
79
- expect(submissions[0].input).toEqual(["test-data"]);
80
- expect(submissions[0].pending).toBe(true);
81
- const result = await promise;
82
- expect(result).toBe("processed: test-data");
83
- });
84
- });
85
- test("should handle action errors", async () => {
86
- return createRoot(async () => {
87
- const errorAction = action(async () => {
88
- throw new Error("Test error");
89
- }, "error-test");
90
- const boundAction = useAction(errorAction);
91
- try {
92
- await boundAction();
93
- }
94
- catch (error) {
95
- expect(error.message).toBe("Test error");
96
- }
97
- const submissions = mockRouterContext.submissions[0]();
98
- expect(submissions[0].error.message).toBe("Test error");
99
- });
100
- });
101
- test("should support `onComplete` callback", async () => {
102
- return createRoot(async () => {
103
- const onComplete = vi.fn();
104
- const testAction = action(async (data) => `result: ${data}`, {
105
- name: "callback-test",
106
- onComplete
107
- });
108
- const boundAction = useAction(testAction);
109
- await boundAction("test");
110
- expect(onComplete).toHaveBeenCalledWith(expect.objectContaining({
111
- result: "result: test",
112
- error: undefined,
113
- pending: false
114
- }));
115
- });
116
- });
117
- });
118
- describe("useSubmissions", () => {
119
- beforeEach(() => {
120
- mockRouterContext = createMockRouter();
121
- });
122
- test("should return submissions for specific action", () => {
123
- return createRoot(() => {
124
- const testAction = action(async () => "result", "submissions-test");
125
- mockRouterContext.submissions[1](submissions => [
126
- ...submissions,
127
- {
128
- input: ["data1"],
129
- url: testAction.url,
130
- result: "result1",
131
- error: undefined,
132
- pending: false,
133
- clear: vi.fn(),
134
- retry: vi.fn()
135
- },
136
- {
137
- input: ["data2"],
138
- url: testAction.url,
139
- result: undefined,
140
- error: undefined,
141
- pending: true,
142
- clear: vi.fn(),
143
- retry: vi.fn()
144
- }
145
- ]);
146
- const submissions = useSubmissions(testAction);
147
- expect(submissions).toHaveLength(2);
148
- expect(submissions[0].input).toEqual(["data1"]);
149
- expect(submissions[1].input).toEqual(["data2"]);
150
- expect(submissions.pending).toBe(true);
151
- });
152
- });
153
- test("should filter submissions when filter function provided", () => {
154
- return createRoot(() => {
155
- const testAction = action(async (data) => data, "filter-test");
156
- mockRouterContext.submissions[1](submissions => [
157
- ...submissions,
158
- {
159
- input: ["keep"],
160
- url: testAction.url,
161
- result: "result1",
162
- error: undefined,
163
- pending: false,
164
- clear: vi.fn(),
165
- retry: vi.fn()
166
- },
167
- {
168
- input: ["skip"],
169
- url: testAction.url,
170
- result: "result2",
171
- error: undefined,
172
- pending: false,
173
- clear: vi.fn(),
174
- retry: vi.fn()
175
- }
176
- ]);
177
- const submissions = useSubmissions(testAction, input => input[0] === "keep");
178
- expect(submissions).toHaveLength(1);
179
- expect(submissions[0].input).toEqual(["keep"]);
180
- });
181
- });
182
- test("should return pending false when no pending submissions", () => {
183
- return createRoot(() => {
184
- const testAction = action(async () => "result", "no-pending-test");
185
- mockRouterContext.submissions[1](submissions => [
186
- ...submissions,
187
- {
188
- input: ["data"],
189
- url: testAction.url,
190
- result: "result",
191
- error: undefined,
192
- pending: false,
193
- clear: vi.fn(),
194
- retry: vi.fn()
195
- }
196
- ]);
197
- const submissions = useSubmissions(testAction);
198
- expect(submissions.pending).toBe(false);
199
- });
200
- });
201
- });
202
- describe("useSubmission", () => {
203
- beforeEach(() => {
204
- mockRouterContext = createMockRouter();
205
- });
206
- test("should return latest submission for action", () => {
207
- return createRoot(() => {
208
- const testAction = action(async () => "result", "latest-test");
209
- mockRouterContext.submissions[1](submissions => [
210
- ...submissions,
211
- {
212
- input: ["data1"],
213
- url: testAction.url,
214
- result: "result1",
215
- error: undefined,
216
- pending: false,
217
- clear: vi.fn(),
218
- retry: vi.fn()
219
- },
220
- {
221
- input: ["data2"],
222
- url: testAction.url,
223
- result: "result2",
224
- error: undefined,
225
- pending: false,
226
- clear: vi.fn(),
227
- retry: vi.fn()
228
- }
229
- ]);
230
- const submission = useSubmission(testAction);
231
- expect(submission.input).toEqual(["data2"]);
232
- expect(submission.result).toBe("result2");
233
- });
234
- });
235
- test("should return stub when no submissions exist", () => {
236
- return createRoot(() => {
237
- const testAction = action(async () => "result", "stub-test");
238
- const submission = useSubmission(testAction);
239
- expect(submission.clear).toBeDefined();
240
- expect(submission.retry).toBeDefined();
241
- expect(typeof submission.clear).toBe("function");
242
- expect(typeof submission.retry).toBe("function");
243
- });
244
- });
245
- test("should filter submissions when filter function provided", () => {
246
- return createRoot(() => {
247
- const testAction = action(async (data) => data, "filter-submission-test");
248
- mockRouterContext.submissions[1](submissions => [
249
- ...submissions,
250
- {
251
- input: ["skip"],
252
- url: testAction.url,
253
- result: "result1",
254
- error: undefined,
255
- pending: false,
256
- clear: vi.fn(),
257
- retry: vi.fn()
258
- },
259
- {
260
- input: ["keep"],
261
- url: testAction.url,
262
- result: "result2",
263
- error: undefined,
264
- pending: false,
265
- clear: vi.fn(),
266
- retry: vi.fn()
267
- }
268
- ]);
269
- const submission = useSubmission(testAction, input => input[0] === "keep");
270
- expect(submission.input).toEqual(["keep"]);
271
- expect(submission.result).toBe("result2");
272
- });
273
- });
274
- });
275
- describe("useAction", () => {
276
- beforeEach(() => {
277
- mockRouterContext = createMockRouter();
278
- });
279
- test("should return bound action function", () => {
280
- return createRoot(() => {
281
- const testAction = action(async (data) => `result: ${data}`, "bound-test");
282
- const boundAction = useAction(testAction);
283
- expect(typeof boundAction).toBe("function");
284
- });
285
- });
286
- test("should execute action through useAction", async () => {
287
- return createRoot(async () => {
288
- const testAction = action(async (data) => {
289
- await new Promise(resolve => setTimeout(resolve, 1));
290
- return `result: ${data}`;
291
- }, "context-test");
292
- const boundAction = useAction(testAction);
293
- const result = await boundAction("test-data");
294
- expect(result).toBe("result: test-data");
295
- });
296
- });
297
- });
@@ -1,32 +0,0 @@
1
- import { type ReconcileOptions } from "solid-js/store";
2
- /**
3
- * As `createAsync` and `createAsyncStore` are wrappers for `createResource`,
4
- * this type allows to support `latest` field for these primitives.
5
- * It will be removed in the future.
6
- */
7
- export type AccessorWithLatest<T> = {
8
- (): T;
9
- latest: T;
10
- };
11
- export declare function createAsync<T>(fn: (prev: T) => Promise<T>, options: {
12
- name?: string;
13
- initialValue: T;
14
- deferStream?: boolean;
15
- }): AccessorWithLatest<T>;
16
- export declare function createAsync<T>(fn: (prev: T | undefined) => Promise<T>, options?: {
17
- name?: string;
18
- initialValue?: T;
19
- deferStream?: boolean;
20
- }): AccessorWithLatest<T | undefined>;
21
- export declare function createAsyncStore<T>(fn: (prev: T) => Promise<T>, options: {
22
- name?: string;
23
- initialValue: T;
24
- deferStream?: boolean;
25
- reconcile?: ReconcileOptions;
26
- }): AccessorWithLatest<T>;
27
- export declare function createAsyncStore<T>(fn: (prev: T | undefined) => Promise<T>, options?: {
28
- name?: string;
29
- initialValue?: T;
30
- deferStream?: boolean;
31
- reconcile?: ReconcileOptions;
32
- }): AccessorWithLatest<T | undefined>;
@@ -1,96 +0,0 @@
1
- /**
2
- * This is mock of the eventual Solid 2.0 primitive. It is not fully featured.
3
- */
4
- import { createResource, sharedConfig, untrack, catchError } from "solid-js";
5
- import { createStore, reconcile, unwrap } from "solid-js/store";
6
- import { isServer } from "solid-js/web";
7
- import { setFunctionName } from "../utils.js";
8
- export function createAsync(fn, options) {
9
- let resource;
10
- let prev = () => !resource || resource.state === "unresolved" ? undefined : resource.latest;
11
- [resource] = createResource(() => subFetch(fn, catchError(() => untrack(prev), () => undefined)), v => v, options);
12
- const resultAccessor = (() => resource());
13
- if (options?.name)
14
- setFunctionName(resultAccessor, options.name);
15
- Object.defineProperty(resultAccessor, "latest", {
16
- get() {
17
- return resource.latest;
18
- }
19
- });
20
- return resultAccessor;
21
- }
22
- export function createAsyncStore(fn, options = {}) {
23
- let resource;
24
- let prev = () => !resource || resource.state === "unresolved"
25
- ? undefined
26
- : unwrap(resource.latest);
27
- [resource] = createResource(() => subFetch(fn, catchError(() => untrack(prev), () => undefined)), v => v, {
28
- ...options,
29
- storage: (init) => createDeepSignal(init, options.reconcile)
30
- });
31
- const resultAccessor = (() => resource());
32
- Object.defineProperty(resultAccessor, "latest", {
33
- get() {
34
- return resource.latest;
35
- }
36
- });
37
- return resultAccessor;
38
- }
39
- function createDeepSignal(value, options) {
40
- const [store, setStore] = createStore({
41
- value: structuredClone(value)
42
- });
43
- return [
44
- () => store.value,
45
- (v) => {
46
- typeof v === "function" && (v = v());
47
- setStore("value", reconcile(structuredClone(v), options));
48
- return store.value;
49
- }
50
- ];
51
- }
52
- // mock promise while hydrating to prevent fetching
53
- class MockPromise {
54
- static all() {
55
- return new MockPromise();
56
- }
57
- static allSettled() {
58
- return new MockPromise();
59
- }
60
- static any() {
61
- return new MockPromise();
62
- }
63
- static race() {
64
- return new MockPromise();
65
- }
66
- static reject() {
67
- return new MockPromise();
68
- }
69
- static resolve() {
70
- return new MockPromise();
71
- }
72
- catch() {
73
- return new MockPromise();
74
- }
75
- then() {
76
- return new MockPromise();
77
- }
78
- finally() {
79
- return new MockPromise();
80
- }
81
- }
82
- function subFetch(fn, prev) {
83
- if (isServer || !sharedConfig.context)
84
- return fn(prev);
85
- const ogFetch = fetch;
86
- const ogPromise = Promise;
87
- try {
88
- window.fetch = () => new MockPromise();
89
- Promise = MockPromise;
90
- return fn(prev);
91
- }
92
- finally {
93
- window.fetch = ogFetch;
94
- Promise = ogPromise;
95
- }
96
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,196 +0,0 @@
1
- import { createRoot } from "solid-js";
2
- import { vi } from "vitest";
3
- import { createAsync, createAsyncStore } from "./createAsync.js";
4
- vi.mock("solid-js", async () => {
5
- const actual = await vi.importActual("solid-js");
6
- return {
7
- ...actual,
8
- sharedConfig: { context: null }
9
- };
10
- });
11
- let mockSharedConfig;
12
- describe("createAsync", () => {
13
- beforeAll(async () => {
14
- const { sharedConfig } = await import("solid-js");
15
- mockSharedConfig = sharedConfig;
16
- });
17
- test("should create async resource with `initialValue`", async () => {
18
- return createRoot(async () => {
19
- const resource = createAsync(async (prev) => {
20
- await new Promise(resolve => setTimeout(resolve, 10));
21
- return prev ? prev + 1 : 1;
22
- }, { initialValue: 0 });
23
- expect(resource()).toBe(0);
24
- expect(resource.latest).toBe(0);
25
- });
26
- });
27
- test("should create async resource without `initialValue`", async () => {
28
- return createRoot(async () => {
29
- const resource = createAsync(async () => {
30
- await new Promise(resolve => setTimeout(resolve, 10));
31
- return "loaded data";
32
- });
33
- expect(resource()).toBeUndefined();
34
- expect(resource.latest).toBeUndefined();
35
- await new Promise(resolve => setTimeout(resolve, 20));
36
- expect(resource()).toBe("loaded data");
37
- expect(resource.latest).toBe("loaded data");
38
- });
39
- });
40
- test("should update resource with new data", async () => {
41
- return createRoot(async () => {
42
- let counter = 0;
43
- const resource = createAsync(async () => {
44
- await new Promise(resolve => setTimeout(resolve, 10));
45
- return ++counter;
46
- });
47
- await new Promise(resolve => setTimeout(resolve, 20));
48
- expect(resource()).toBe(1);
49
- // Trigger re-fetch - this would typically happen through some reactive source
50
- // Since we can't easily trigger refetch in this test environment,
51
- // we verify the structure is correct
52
- expect(typeof resource).toBe("function");
53
- expect(resource.latest).toBe(1);
54
- });
55
- });
56
- test("should handle async errors", async () => {
57
- return createRoot(async () => {
58
- const resource = createAsync(async () => {
59
- await new Promise(resolve => setTimeout(resolve, 10));
60
- throw new Error("Async error");
61
- });
62
- await new Promise(resolve => setTimeout(resolve, 20));
63
- /*
64
- * @note Resource should handle the error gracefully
65
- * The exact error handling depends on `createResource` implementation
66
- */
67
- expect(typeof resource).toBe("function");
68
- });
69
- });
70
- test("should support `deferStream` option", () => {
71
- return createRoot(() => {
72
- const resource = createAsync(async () => "deferred data", { deferStream: true });
73
- expect(typeof resource).toBe("function");
74
- expect(resource.latest).toBeUndefined();
75
- });
76
- });
77
- test("should support `name` option for debugging", () => {
78
- return createRoot(() => {
79
- const resource = createAsync(async () => "named resource", { name: "test-resource" });
80
- expect(typeof resource).toBe("function");
81
- expect(resource.name).toBe("test-resource");
82
- });
83
- });
84
- test("should pass previous value to fetch function", async () => {
85
- return createRoot(async () => {
86
- let callCount = 0;
87
- let lastPrev;
88
- const resource = createAsync(async (prev) => {
89
- lastPrev = prev;
90
- return `call-${++callCount}-prev-${prev}`;
91
- }, { initialValue: "initial" });
92
- expect(resource()).toBe("initial");
93
- await new Promise(resolve => setTimeout(resolve, 20));
94
- expect(lastPrev).toBeUndefined();
95
- });
96
- });
97
- });
98
- describe("createAsyncStore", () => {
99
- test("should create async store with `initialValue`", async () => {
100
- return createRoot(async () => {
101
- const store = createAsyncStore(async (prev) => {
102
- await new Promise(resolve => setTimeout(resolve, 10));
103
- return { count: prev?.count ? prev.count + 1 : 1, data: "test" };
104
- }, { initialValue: { count: 0, data: "initial" } });
105
- expect(store()).toEqual({ count: 0, data: "initial" });
106
- expect(store.latest).toEqual({ count: 0, data: "initial" });
107
- });
108
- });
109
- test("should create async store without `initialValue`", async () => {
110
- return createRoot(async () => {
111
- const store = createAsyncStore(async () => {
112
- await new Promise(resolve => setTimeout(resolve, 10));
113
- return { loaded: true, message: "success" };
114
- });
115
- expect(store()).toBeUndefined();
116
- expect(store.latest).toBeUndefined();
117
- await new Promise(resolve => setTimeout(resolve, 20));
118
- expect(store()).toEqual({ loaded: true, message: "success" });
119
- expect(store.latest).toEqual({ loaded: true, message: "success" });
120
- });
121
- });
122
- test("should support `reconcile` options", () => {
123
- return createRoot(() => {
124
- const store = createAsyncStore(async () => ({ items: [1, 2, 3] }), {
125
- reconcile: { key: "id" }
126
- });
127
- expect(typeof store).toBe("function");
128
- });
129
- });
130
- test("should handle complex object updates", async () => {
131
- return createRoot(async () => {
132
- let updateCount = 0;
133
- const store = createAsyncStore(async (prev) => {
134
- await new Promise(resolve => setTimeout(resolve, 10));
135
- return {
136
- ...prev,
137
- updateCount: ++updateCount,
138
- timestamp: Date.now(),
139
- nested: { value: `update-${updateCount}` }
140
- };
141
- }, { initialValue: { updateCount: 0, timestamp: 0, nested: { value: "initial" } } });
142
- const initial = store();
143
- expect(initial.updateCount).toBe(0);
144
- expect(initial.nested.value).toBe("initial");
145
- });
146
- });
147
- test("should support all `createAsync` options", () => {
148
- return createRoot(() => {
149
- const store = createAsyncStore(async () => ({ data: "test" }), {
150
- name: "test-store",
151
- deferStream: true,
152
- reconcile: { merge: true }
153
- });
154
- expect(typeof store).toBe("function");
155
- });
156
- });
157
- });
158
- describe("MockPromise", () => {
159
- test("should mock fetch during hydration", async () => {
160
- mockSharedConfig.context = {};
161
- return createRoot(async () => {
162
- const originalFetch = window.fetch;
163
- // Set up a fetch that should be mocked
164
- window.fetch = () => {
165
- return Promise.resolve(new Response("real fetch"));
166
- };
167
- const resource = createAsync(async () => {
168
- const response = await fetch("/api/data");
169
- return await response.text();
170
- });
171
- // During hydration, fetch should be mocked
172
- expect(resource()).toBeUndefined();
173
- window.fetch = originalFetch;
174
- mockSharedConfig.context = null;
175
- });
176
- });
177
- test("should allow real fetch outside hydration", async () => {
178
- // Ensure we're not in hydration context
179
- mockSharedConfig.context = null;
180
- return createRoot(async () => {
181
- let fetchCalled = false;
182
- const originalFetch = window.fetch;
183
- window.fetch = vi.fn().mockImplementation(() => {
184
- fetchCalled = true;
185
- return Promise.resolve(new Response("real data"));
186
- });
187
- createAsync(async () => {
188
- const response = await fetch("/api/data");
189
- return await response.text();
190
- });
191
- await new Promise(resolve => setTimeout(resolve, 20));
192
- expect(fetchCalled).toBe(true);
193
- window.fetch = originalFetch;
194
- });
195
- });
196
- });
@@ -1,9 +0,0 @@
1
- import type { RouterContext } from "../types.js";
2
- type NativeEventConfig = {
3
- preload?: boolean;
4
- explicitLinks?: boolean;
5
- actionBase?: string;
6
- transformUrl?: (url: string) => string;
7
- };
8
- export declare function setupNativeEvents({ preload, explicitLinks, actionBase, transformUrl }?: NativeEventConfig): (router: RouterContext) => void;
9
- export {};