@krate/runtime 0.5.0-rc.1 → 0.5.0-rc.11

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 (214) hide show
  1. package/dist/action-context.js +1 -1
  2. package/dist/actions.d.ts +7 -0
  3. package/dist/actions.js +34 -10
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/assets-static.d.ts +10 -1
  7. package/dist/assets-static.js +10 -2
  8. package/dist/base-path.d.ts +7 -0
  9. package/dist/base-path.js +20 -0
  10. package/dist/boundary.js +1 -1
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/cache-adapter.js +1 -1
  14. package/dist/cache-scope.d.ts +8 -0
  15. package/dist/cache-scope.js +14 -1
  16. package/dist/cloudflare-workers.d.ts +2 -0
  17. package/dist/cloudflare-workers.js +27 -0
  18. package/dist/context.d.ts +5 -0
  19. package/dist/context.js +28 -17
  20. package/dist/dev-overlay.d.ts +54 -3
  21. package/dist/dev-overlay.js +470 -77
  22. package/dist/dom.js +18 -3
  23. package/dist/effects.js +2 -2
  24. package/dist/flight-props.d.ts +7 -0
  25. package/dist/flight-props.js +105 -0
  26. package/dist/handler-html.js +4 -4
  27. package/dist/handler-routing.d.ts +2 -23
  28. package/dist/handler-routing.js +9 -190
  29. package/dist/handler-segments.d.ts +6 -0
  30. package/dist/handler-segments.js +18 -4
  31. package/dist/head-tags.d.ts +9 -0
  32. package/dist/head-tags.js +93 -6
  33. package/dist/highlight-bind.js +1 -1
  34. package/dist/highlight.d.ts +3 -2
  35. package/dist/highlight.js +17 -12
  36. package/dist/hints.d.ts +25 -2
  37. package/dist/hints.js +147 -74
  38. package/dist/index.d.ts +2 -0
  39. package/dist/index.js +3 -0
  40. package/dist/island-swap.d.ts +47 -0
  41. package/dist/island-swap.js +635 -0
  42. package/dist/jsx-dev-runtime.d.ts +1 -1
  43. package/dist/jsx-types.d.ts +99 -3
  44. package/dist/krate-core.js +1 -1
  45. package/dist/krate-dev.js +385 -66
  46. package/dist/krate-f-actions.js +1 -1
  47. package/dist/krate-f-classes.js +1 -1
  48. package/dist/krate-f-highlight.js +1 -1
  49. package/dist/krate-f-list.js +1 -1
  50. package/dist/krate-f-navigation.js +1 -1
  51. package/dist/krate-f-router-lite.js +1 -1
  52. package/dist/krate-f-router.js +1 -1
  53. package/dist/krate-features.json +454 -0
  54. package/dist/krate-hydrate-lite.js +5 -5
  55. package/dist/krate-hydrate.js +5 -5
  56. package/dist/krate-island-swap.js +1 -0
  57. package/dist/krate-runtime.js +5 -5
  58. package/dist/list.d.ts +14 -4
  59. package/dist/list.js +4 -5
  60. package/dist/metadata.js +5 -2
  61. package/dist/navigation-react.d.ts +13 -0
  62. package/dist/navigation-react.js +86 -0
  63. package/dist/navigation.d.ts +19 -0
  64. package/dist/navigation.js +35 -0
  65. package/dist/node-server.js +3 -3
  66. package/dist/og.js +1 -1
  67. package/dist/platform.d.ts +52 -0
  68. package/dist/platform.js +57 -0
  69. package/dist/react-compat.d.ts +39 -0
  70. package/dist/react-compat.js +359 -0
  71. package/dist/react-server.d.ts +63 -0
  72. package/dist/react-server.js +125 -0
  73. package/dist/region-wire.d.ts +10 -0
  74. package/dist/region-wire.js +154 -0
  75. package/dist/request-context.js +1 -1
  76. package/dist/request-extras.d.ts +4 -0
  77. package/dist/request-extras.js +51 -0
  78. package/dist/resp.js +1 -1
  79. package/dist/route-source.d.ts +39 -0
  80. package/dist/route-source.js +532 -0
  81. package/dist/router-lite.js +3 -9
  82. package/dist/router.d.ts +1 -1
  83. package/dist/router.js +628 -98
  84. package/dist/server-handler.d.ts +69 -1
  85. package/dist/server-handler.js +515 -96
  86. package/dist/server-renderer.js +16 -7
  87. package/dist/server.d.ts +73 -16
  88. package/dist/server.js +619 -102
  89. package/dist/sha256.js +9 -9
  90. package/dist/signal.js +0 -6
  91. package/dist/site-config.d.ts +15 -0
  92. package/dist/site-config.js +28 -0
  93. package/dist/slot.d.ts +12 -0
  94. package/dist/slot.js +23 -0
  95. package/dist/ssr-core.d.ts +2 -0
  96. package/dist/ssr-core.js +12 -4
  97. package/dist/testing.d.ts +62 -0
  98. package/dist/testing.js +90 -0
  99. package/dist/use-id.js +1 -1
  100. package/package.json +28 -3
  101. package/dist/action-context.test.d.ts +0 -1
  102. package/dist/action-context.test.js +0 -72
  103. package/dist/actions.test.d.ts +0 -1
  104. package/dist/actions.test.js +0 -477
  105. package/dist/cache-adapter.test.d.ts +0 -1
  106. package/dist/cache-adapter.test.js +0 -244
  107. package/dist/cache.test.d.ts +0 -1
  108. package/dist/cache.test.js +0 -233
  109. package/dist/class-component.test.d.ts +0 -1
  110. package/dist/class-component.test.js +0 -462
  111. package/dist/class-helpers.test.d.ts +0 -1
  112. package/dist/class-helpers.test.js +0 -169
  113. package/dist/context.test.d.ts +0 -1
  114. package/dist/context.test.js +0 -57
  115. package/dist/derived.test.d.ts +0 -1
  116. package/dist/derived.test.js +0 -140
  117. package/dist/dev-overlay.test.d.ts +0 -1
  118. package/dist/dev-overlay.test.js +0 -165
  119. package/dist/discrete.test.d.ts +0 -1
  120. package/dist/discrete.test.js +0 -100
  121. package/dist/dom.test.d.ts +0 -1
  122. package/dist/dom.test.js +0 -39
  123. package/dist/effect-errors.test.d.ts +0 -1
  124. package/dist/effect-errors.test.js +0 -199
  125. package/dist/effects.test.d.ts +0 -1
  126. package/dist/effects.test.js +0 -485
  127. package/dist/events.test.d.ts +0 -1
  128. package/dist/events.test.js +0 -447
  129. package/dist/handler-html.test.d.ts +0 -1
  130. package/dist/handler-html.test.js +0 -256
  131. package/dist/handler-routing.test.d.ts +0 -1
  132. package/dist/handler-routing.test.js +0 -99
  133. package/dist/handler-segments.test.d.ts +0 -1
  134. package/dist/handler-segments.test.js +0 -217
  135. package/dist/head-tags.test.d.ts +0 -1
  136. package/dist/head-tags.test.js +0 -131
  137. package/dist/highlight-bind.test.d.ts +0 -1
  138. package/dist/highlight-bind.test.js +0 -124
  139. package/dist/highlight.test.d.ts +0 -1
  140. package/dist/highlight.test.js +0 -56
  141. package/dist/lanes.test.d.ts +0 -1
  142. package/dist/lanes.test.js +0 -59
  143. package/dist/list.test.d.ts +0 -1
  144. package/dist/list.test.js +0 -435
  145. package/dist/metadata.test.d.ts +0 -1
  146. package/dist/metadata.test.js +0 -167
  147. package/dist/navigation.test.d.ts +0 -1
  148. package/dist/navigation.test.js +0 -130
  149. package/dist/node-server.test.d.ts +0 -1
  150. package/dist/node-server.test.js +0 -492
  151. package/dist/optimistic-lanes.test.d.ts +0 -1
  152. package/dist/optimistic-lanes.test.js +0 -45
  153. package/dist/patterns.test.d.ts +0 -1
  154. package/dist/patterns.test.js +0 -125
  155. package/dist/portal.test.d.ts +0 -1
  156. package/dist/portal.test.js +0 -99
  157. package/dist/react-effects.test.d.ts +0 -1
  158. package/dist/react-effects.test.js +0 -133
  159. package/dist/react-suspense.test.d.ts +0 -1
  160. package/dist/react-suspense.test.js +0 -194
  161. package/dist/react.test.d.ts +0 -1
  162. package/dist/react.test.js +0 -60
  163. package/dist/react19.test.d.ts +0 -1
  164. package/dist/react19.test.js +0 -176
  165. package/dist/reactivity-core.test.d.ts +0 -1
  166. package/dist/reactivity-core.test.js +0 -152
  167. package/dist/reconcile.test.d.ts +0 -1
  168. package/dist/reconcile.test.js +0 -110
  169. package/dist/refs.test.d.ts +0 -1
  170. package/dist/refs.test.js +0 -257
  171. package/dist/router-layout.test.d.ts +0 -1
  172. package/dist/router-layout.test.js +0 -249
  173. package/dist/router-nav.test.d.ts +0 -1
  174. package/dist/router-nav.test.js +0 -559
  175. package/dist/router-slots.test.d.ts +0 -1
  176. package/dist/router-slots.test.js +0 -144
  177. package/dist/router-speculation.test.d.ts +0 -1
  178. package/dist/router-speculation.test.js +0 -25
  179. package/dist/router-styles.test.d.ts +0 -1
  180. package/dist/router-styles.test.js +0 -98
  181. package/dist/router.test.d.ts +0 -1
  182. package/dist/router.test.js +0 -36
  183. package/dist/server-async.test.d.ts +0 -1
  184. package/dist/server-async.test.js +0 -216
  185. package/dist/server-handler.test.d.ts +0 -1
  186. package/dist/server-handler.test.js +0 -535
  187. package/dist/server-highlight.test.d.ts +0 -1
  188. package/dist/server-highlight.test.js +0 -32
  189. package/dist/server-renderer.test.d.ts +0 -1
  190. package/dist/server-renderer.test.js +0 -266
  191. package/dist/server.test.d.ts +0 -1
  192. package/dist/server.test.js +0 -88
  193. package/dist/sha256.test.d.ts +0 -1
  194. package/dist/sha256.test.js +0 -13
  195. package/dist/signal-lanes.test.d.ts +0 -1
  196. package/dist/signal-lanes.test.js +0 -32
  197. package/dist/signal.test.d.ts +0 -1
  198. package/dist/signal.test.js +0 -68
  199. package/dist/slots.test.d.ts +0 -1
  200. package/dist/slots.test.js +0 -187
  201. package/dist/snapshots.test.d.ts +0 -1
  202. package/dist/snapshots.test.js +0 -208
  203. package/dist/ssr-core-behavior.test.d.ts +0 -1
  204. package/dist/ssr-core-behavior.test.js +0 -620
  205. package/dist/ssr-core.test.d.ts +0 -1
  206. package/dist/ssr-core.test.js +0 -324
  207. package/dist/static-memory.test.d.ts +0 -1
  208. package/dist/static-memory.test.js +0 -26
  209. package/dist/storage.test.d.ts +0 -1
  210. package/dist/storage.test.js +0 -46
  211. package/dist/suspense-actions.test.d.ts +0 -1
  212. package/dist/suspense-actions.test.js +0 -72
  213. package/dist/suspense.test.d.ts +0 -1
  214. package/dist/suspense.test.js +0 -556
@@ -1,462 +0,0 @@
1
- import { afterEach, describe, expect, it, vi } from 'vitest';
2
- import { createEffect, createSignal, disposeAll, krateDisposeExcept, krateEnterScope, krateLeaveScope } from './signal.js';
3
- import { __krateOwn, __krateOwner, __krateCondPre, __krateCondPost, __krateOnError, handleError } from './boundary.js';
4
- import { __krateClass } from './class-component.js';
5
- // Lets the effect flush, the render phase and the commit phase run.
6
- const settle = () => new Promise((r) => setTimeout(r, 0));
7
- afterEach(() => {
8
- disposeAll();
9
- });
10
- // Simulates the hydration scope the compiler emits for a lowered class: a
11
- // committed-state signal, the instance, and the render bindings.
12
- function classScope(props, initial, spec, render) {
13
- const [state, setState] = createSignal(initial);
14
- const inst = __krateClass(props, state, setState, spec);
15
- if (render)
16
- render(state, props);
17
- return { inst, state };
18
- }
19
- describe('class lifecycles', () => {
20
- it('mounts children before parents and updates child before parent', async () => {
21
- const log = [];
22
- let parent;
23
- const text = { p: '', c: '' };
24
- const childProps = {};
25
- __krateOwn('P', { n: 'Parent' }, () => {
26
- parent = classScope({}, { n: 1 }, {
27
- componentDidMount: () => log.push('P didMount'),
28
- componentDidUpdate: (pp, ps) => log.push('P didUpdate ' + ps.n + '>' + parent.inst.state.n),
29
- }, (st) => {
30
- Object.defineProperty(childProps, 'n', { get: () => st().n, enumerable: true, configurable: true });
31
- createEffect(() => {
32
- text.p = String(st().n);
33
- });
34
- });
35
- });
36
- __krateOwn('C', { p: 'P', n: 'Child', k: 'P' }, () => {
37
- const c = classScope(childProps, {}, {
38
- componentDidMount: () => log.push('C didMount ' + c.inst.props.n),
39
- componentDidUpdate: (pp) => log.push('C didUpdate ' + pp.n + '>' + c.inst.props.n + ' dom=' + text.c),
40
- }, (_st, props) => {
41
- createEffect(() => {
42
- text.c = String(props.n);
43
- });
44
- });
45
- });
46
- await settle();
47
- expect(log).toEqual(['C didMount 1', 'P didMount']);
48
- log.length = 0;
49
- parent.inst.setState({ n: 2 });
50
- await settle();
51
- expect(log).toEqual(['C didUpdate 1>2 dom=2', 'P didUpdate 1>2']);
52
- expect(text).toEqual({ p: '2', c: '2' });
53
- });
54
- it('re-renders child classes when the creator re-renders with equal props', async () => {
55
- const log = [];
56
- let parent;
57
- __krateOwn('P', { n: 'Parent' }, () => {
58
- parent = classScope({}, { n: 1, other: 0 }, { componentDidUpdate: () => log.push('P') });
59
- });
60
- __krateOwn('C', { p: 'P', n: 'Child', k: 'P' }, () => {
61
- classScope({ label: 'x' }, {}, { componentDidUpdate: () => log.push('C') });
62
- });
63
- __krateOwn('Pure', { p: 'P', n: 'Pure', k: 'P' }, () => {
64
- classScope({ label: 'x' }, {}, { pure: true, componentDidUpdate: () => log.push('Pure') });
65
- });
66
- // Passed as children from outside: not re-rendered by P.
67
- __krateOwn('Passed', { p: 'P', n: 'Passed' }, () => {
68
- classScope({}, {}, { componentDidUpdate: () => log.push('Passed') });
69
- });
70
- await settle();
71
- parent.inst.setState({ other: 1 });
72
- await settle();
73
- expect(log).toEqual(['C', 'P']);
74
- });
75
- it('batches setState, applies updaters in order and runs callbacks after didUpdate', async () => {
76
- const log = [];
77
- let c;
78
- __krateOwn('A', { n: 'A' }, () => {
79
- c = classScope({}, { n: 0 }, {
80
- componentDidUpdate: (_pp, ps) => log.push('didUpdate ' + ps.n + '>' + c.inst.state.n),
81
- });
82
- });
83
- await settle();
84
- c.inst.setState((s) => ({ n: s.n + 1 }), () => log.push('cb1 ' + c.inst.state.n));
85
- c.inst.setState((s) => ({ n: s.n + 1 }), () => log.push('cb2'));
86
- c.inst.setState({ extra: true });
87
- // setState is asynchronous: this.state is unchanged until the update runs.
88
- expect(c.inst.state.n).toBe(0);
89
- await settle();
90
- expect(log).toEqual(['didUpdate 0>2', 'cb1 2', 'cb2']);
91
- expect(c.state()).toEqual({ n: 2, extra: true });
92
- });
93
- it('shouldComponentUpdate false skips the render and didUpdate but updates the instance', async () => {
94
- const log = [];
95
- let c;
96
- let dom = '';
97
- __krateOwn('A', { n: 'A' }, () => {
98
- c = classScope({}, { n: 0 }, {
99
- shouldComponentUpdate: (_np, ns) => ns.n % 2 === 0,
100
- componentDidUpdate: () => log.push('didUpdate ' + c.inst.state.n),
101
- }, (st) => {
102
- createEffect(() => {
103
- dom = String(st().n);
104
- });
105
- });
106
- });
107
- await settle();
108
- c.inst.setState({ n: 1 }, () => log.push('cb ' + c.inst.state.n));
109
- await settle();
110
- expect(dom).toBe('0');
111
- expect(c.inst.state.n).toBe(1);
112
- expect(log).toEqual(['cb 1']);
113
- c.inst.setState({ n: 2 });
114
- await settle();
115
- expect(dom).toBe('2');
116
- expect(log).toEqual(['cb 1', 'didUpdate 2']);
117
- // forceUpdate bypasses shouldComponentUpdate.
118
- c.inst.setState({ n: 3 });
119
- await settle();
120
- expect(dom).toBe('2');
121
- c.inst.forceUpdate(() => log.push('forced'));
122
- await settle();
123
- expect(dom).toBe('3');
124
- expect(log.slice(-2)).toEqual(['didUpdate 3', 'forced']);
125
- });
126
- it('PureComponent skips shallow-equal updates', async () => {
127
- const log = [];
128
- let c;
129
- __krateOwn('A', { n: 'A' }, () => {
130
- c = classScope({}, { n: 0 }, { pure: true, componentDidUpdate: () => log.push('u') });
131
- });
132
- await settle();
133
- c.inst.setState({ n: 0 });
134
- await settle();
135
- expect(log).toEqual([]);
136
- c.inst.setState({ n: 1 });
137
- await settle();
138
- expect(log).toEqual(['u']);
139
- });
140
- it('runs getDerivedStateFromProps on prop changes and passes the snapshot to didUpdate', async () => {
141
- const log = [];
142
- const [x, setX] = createSignal(1);
143
- const props = {};
144
- Object.defineProperty(props, 'x', { get: () => x(), enumerable: true, configurable: true });
145
- let dom = '';
146
- __krateOwn('A', { n: 'A' }, () => {
147
- classScope(props, { doubled: 2 }, {
148
- getDerivedStateFromProps: (p, s) => {
149
- log.push('gDSFP ' + p.x + ' ' + s.doubled);
150
- return { doubled: p.x * 2 };
151
- },
152
- getSnapshotBeforeUpdate: (pp) => {
153
- log.push('snapshot dom=' + dom + ' prev=' + pp.x);
154
- return 'snap';
155
- },
156
- componentDidUpdate: (pp, ps, snap) => log.push('didUpdate ' + pp.x + ' ' + ps.doubled + ' ' + snap + ' dom=' + dom),
157
- }, (st, p) => {
158
- createEffect(() => {
159
- dom = p.x + ':' + st().doubled;
160
- });
161
- });
162
- });
163
- await settle();
164
- expect(dom).toBe('1:2');
165
- setX(5);
166
- await settle();
167
- expect(log).toEqual(['gDSFP 5 2', 'snapshot dom=1:2 prev=1', 'didUpdate 1 2 snap dom=5:10']);
168
- });
169
- it('applies defaultProps to this.props and gated reads', async () => {
170
- let c;
171
- const props = { given: 'g' };
172
- __krateOwn('A', { n: 'A' }, () => {
173
- c = classScope(props, {}, { defaultProps: { color: 'blue', given: 'no' } });
174
- });
175
- await settle();
176
- expect(c.inst.props).toEqual({ given: 'g', color: 'blue' });
177
- expect(props.color).toBe('blue');
178
- });
179
- it('runs componentWillUnmount parent-first when a branch is hidden and remounts fresh state', async () => {
180
- const log = [];
181
- let setups = 0;
182
- __krateOwn('Page', { n: 'Page' }, () => {
183
- __krateCondPost('5', true);
184
- });
185
- __krateOwn('P', { p: 'Page', n: 'P', c: '5', b: 'c' }, () => {
186
- setups++;
187
- classScope({}, {}, {
188
- componentDidMount: () => log.push('P mount'),
189
- componentWillUnmount: () => log.push('P unmount'),
190
- });
191
- });
192
- __krateOwn('C', { p: 'P', n: 'C' }, () => {
193
- classScope({}, {}, {
194
- componentDidMount: () => log.push('C mount'),
195
- componentWillUnmount: () => log.push('C unmount'),
196
- });
197
- });
198
- await settle();
199
- expect(log).toEqual(['C mount', 'P mount']);
200
- __krateCondPre('5', false);
201
- __krateCondPost('5', false);
202
- expect(log.slice(2)).toEqual(['P unmount', 'C unmount']);
203
- expect(__krateOwner('P').state).toBe('dormant');
204
- __krateCondPre('5', true);
205
- __krateCondPost('5', true);
206
- await settle();
207
- expect(setups).toBe(2);
208
- expect(log.slice(4)).toEqual(['C mount', 'P mount']);
209
- });
210
- it('attaches the instance to a ref after mount and detaches it before unmount', async () => {
211
- const log = [];
212
- const ref = { current: null };
213
- __krateOwn('Page', { n: 'Page' }, () => {
214
- __krateCondPost('9', true);
215
- });
216
- __krateOwn('Fancy', { p: 'Page', n: 'Fancy', c: '9', b: 'c' }, () => {
217
- const focus = () => log.push('focus');
218
- classScope({ ref, label: 'x' }, {}, {
219
- expose: () => ({ focus }),
220
- componentDidMount: () => log.push('mount ref=' + (ref.current === null ? 'null' : 'set')),
221
- componentWillUnmount: () => log.push('unmount ref=' + (ref.current === null ? 'null' : 'set')),
222
- });
223
- });
224
- await settle();
225
- ref.current.focus();
226
- expect(ref.current.props).toEqual({ label: 'x' });
227
- __krateCondPre('9', false);
228
- __krateCondPost('9', false);
229
- expect(ref.current).toBeNull();
230
- expect(log).toEqual(['mount ref=null', 'focus', 'unmount ref=null']);
231
- });
232
- it('keeps components in an initially hidden branch dormant', async () => {
233
- let setups = 0;
234
- __krateOwn('Page', { n: 'Page' }, () => {
235
- __krateCondPost('7', false);
236
- });
237
- __krateOwn('X', { p: 'Page', n: 'X', c: '7', b: 'c' }, () => {
238
- setups++;
239
- });
240
- expect(setups).toBe(0);
241
- __krateCondPost('7', true);
242
- expect(setups).toBe(1);
243
- });
244
- });
245
- describe('runtime class components (react.ts Component)', () => {
246
- it('re-renders on setState and runs lifecycles through the controller', async () => {
247
- const { Component, createElement } = await import('./react.js');
248
- const log = [];
249
- let counter;
250
- class Counter extends Component {
251
- constructor() {
252
- super(...arguments);
253
- this.state = { n: 0 };
254
- }
255
- componentDidMount() {
256
- log.push('mount');
257
- }
258
- componentDidUpdate(_pp, ps) {
259
- log.push('update ' + ps.n + '>' + this.state.n);
260
- }
261
- render() {
262
- counter = this;
263
- return createElement('b', null, 'n=' + String(this.state.n) + ' ' + String(this.props.label));
264
- }
265
- }
266
- const host = createElement(Counter, { label: 'x' });
267
- expect(host.textContent).toBe('n=0 x');
268
- await settle();
269
- expect(log).toEqual(['mount']);
270
- counter.setState({ n: 1 });
271
- // Batched: the DOM changes once the update commits.
272
- expect(host.textContent).toBe('n=0 x');
273
- await settle();
274
- expect(host.textContent).toBe('n=1 x');
275
- expect(log).toEqual(['mount', 'update 0>1']);
276
- });
277
- });
278
- describe('error boundaries', () => {
279
- function boundary(id, meta, log, name = 'Boundary') {
280
- let b;
281
- __krateOwn(id, { n: name, ...meta }, () => {
282
- b = classScope({}, { error: null }, {
283
- name,
284
- getDerivedStateFromError: (error) => {
285
- log.push(name + ' gDSFE ' + error.message);
286
- return { error: error.message };
287
- },
288
- componentDidCatch: (error, info) => log.push(name + ' didCatch ' + error.message + info.componentStack),
289
- });
290
- });
291
- return { get: () => b };
292
- }
293
- it('catches a descendant setup error, unmounts the subtree and recovers on reset', async () => {
294
- const log = [];
295
- const b = boundary('B', {}, log);
296
- let attempts = 0;
297
- let shouldThrow = true;
298
- __krateOwn('Ok', { p: 'B', n: 'Ok' }, () => {
299
- classScope({}, {}, {
300
- componentDidMount: () => log.push('Ok mount'),
301
- componentWillUnmount: () => log.push('Ok unmount'),
302
- });
303
- });
304
- __krateOwn('Buggy', { p: 'B', n: 'Buggy' }, () => {
305
- attempts++;
306
- if (shouldThrow)
307
- throw new Error('boom');
308
- });
309
- __krateOwn('Later', { p: 'B', n: 'Later' }, () => {
310
- log.push('Later setup');
311
- });
312
- await settle();
313
- expect(log).toEqual(['Boundary gDSFE boom', 'Boundary didCatch boom\n at Buggy\n at Boundary']);
314
- expect(b.get().state()).toEqual({ error: 'boom' });
315
- expect(__krateOwner('Ok').state).toBe('dormant');
316
- expect(__krateOwner('Later').state).toBe('dormant');
317
- log.length = 0;
318
- shouldThrow = false;
319
- b.get().inst.setState({ error: null });
320
- await settle();
321
- expect(attempts).toBe(2);
322
- expect(log).toEqual(['Later setup', 'Ok mount']);
323
- expect(__krateOwner('Buggy').state).toBe('mounted');
324
- });
325
- it('catches errors in reactive re-runs and lifecycles, not in event handlers', async () => {
326
- const log = [];
327
- boundary('B', {}, log);
328
- const [n, setN] = createSignal(0);
329
- let handler;
330
- __krateOwn('Child', { p: 'B', n: 'Child' }, () => {
331
- createEffect(() => {
332
- if (n() === 2)
333
- throw new Error('render ' + n());
334
- });
335
- handler = () => {
336
- throw new Error('handler');
337
- };
338
- });
339
- await settle();
340
- expect(() => handler()).toThrow('handler');
341
- expect(log).toEqual([]);
342
- setN(2);
343
- await settle();
344
- expect(log[0]).toBe('Boundary gDSFE render 2');
345
- expect(__krateOwner('Child').state).toBe('dormant');
346
- const log2 = [];
347
- disposeAll();
348
- boundary('B2', {}, log2);
349
- __krateOwn('Life', { p: 'B2', n: 'Life' }, () => {
350
- classScope({}, {}, {
351
- componentDidMount: () => {
352
- throw new Error('mount');
353
- },
354
- });
355
- });
356
- await settle();
357
- expect(log2[0]).toBe('Boundary gDSFE mount');
358
- });
359
- it('lets the innermost boundary catch and sends fallback errors to the next one', async () => {
360
- const log = [];
361
- boundary('Outer', {}, log, 'Outer');
362
- boundary('Inner', { p: 'Outer' }, log, 'Inner');
363
- __krateOwn('Leaf', { p: 'Inner', n: 'Leaf' }, () => {
364
- throw new Error('leaf');
365
- });
366
- await settle();
367
- expect(log.filter((l) => l.includes('gDSFE'))).toEqual(['Inner gDSFE leaf']);
368
- // An error thrown by the boundary's own render goes to the boundary above.
369
- const log2 = [];
370
- disposeAll();
371
- boundary('O', {}, log2, 'O');
372
- __krateOwn('I', { p: 'O', n: 'I' }, () => {
373
- const [st, setSt] = createSignal({ error: null });
374
- __krateClass({}, st, setSt, {
375
- getDerivedStateFromError: () => ({ error: 'x' }),
376
- });
377
- createEffect(() => {
378
- if (st().error)
379
- throw new Error('fallback broke');
380
- });
381
- });
382
- __krateOwn('Deep', { p: 'I', n: 'Deep' }, () => {
383
- throw new Error('deep');
384
- });
385
- await settle();
386
- expect(log2.filter((l) => l.includes('gDSFE'))).toEqual(['O gDSFE fallback broke']);
387
- });
388
- it('does not catch an error raised while the boundary already shows its fallback', async () => {
389
- const log = [];
390
- boundary('Outer', {}, log, 'Outer');
391
- boundary('Inner', { p: 'Outer' }, log, 'Inner');
392
- const [fail, setFail] = createSignal(false);
393
- __krateOwn('Page', { p: 'Inner', n: 'Page' }, () => {
394
- createEffect(() => {
395
- if (fail())
396
- throw new Error('page failed');
397
- });
398
- });
399
- await settle();
400
- setFail(true);
401
- await settle();
402
- // (An update-phase error runs getDerivedStateFromError twice, as in React 18.)
403
- expect(new Set(log.filter((l) => l.includes('gDSFE')))).toEqual(new Set(['Inner gDSFE page failed']));
404
- expect(__krateOwner('Inner').inError).toBe(true);
405
- // The fallback is the boundary's own render output: what it throws is not
406
- // the boundary's to catch.
407
- handleError(new Error('fallback failed'), __krateOwner('Page'));
408
- await settle();
409
- const caught = log.filter((l) => l.includes('gDSFE'));
410
- expect(caught).toContain('Outer gDSFE fallback failed');
411
- expect(caught).not.toContain('Inner gDSFE fallback failed');
412
- });
413
- it('reports uncaught errors and unmounts only the failing component', async () => {
414
- const spy = vi.spyOn(console, 'error').mockImplementation(() => { });
415
- let siblingRan = false;
416
- __krateOwn('Root', { n: 'Root' }, () => { });
417
- __krateOwn('Bad', { p: 'Root', n: 'Bad' }, () => {
418
- throw new Error('nobody catches');
419
- });
420
- __krateOwn('Fine', { p: 'Root', n: 'Fine' }, () => {
421
- siblingRan = true;
422
- });
423
- expect(siblingRan).toBe(true);
424
- expect(__krateOwner('Bad').state).toBe('dormant');
425
- expect(spy.mock.calls.some((c) => String(c[0]).includes('The above error occurred in the <Bad> component'))).toBe(true);
426
- spy.mockRestore();
427
- });
428
- it('ignores errors from bindings outside the document', () => {
429
- const detached = document.createElement('div');
430
- let routed = false;
431
- __krateOwn('Root', { n: 'Root' }, () => {
432
- routed = __krateOnError(new Error('hidden'), detached);
433
- });
434
- expect(routed).toBe(false);
435
- });
436
- });
437
- describe('owner teardown by route scope', () => {
438
- it('keeps the owners of a kept layout and drops the page owners', async () => {
439
- const log = [];
440
- krateEnterScope('_lroot_');
441
- __krateOwn('Layout', { n: 'Layout' }, () => {
442
- classScope({}, {}, { componentWillUnmount: () => log.push('layout unmount') });
443
- });
444
- krateLeaveScope();
445
- __krateOwn('Page', { p: 'Layout', n: 'Page' }, () => {
446
- classScope({}, {}, { componentWillUnmount: () => log.push('page unmount') });
447
- });
448
- await settle();
449
- expect(__krateOwner('Page').state).toBe('mounted');
450
- // A soft navigation keeps the layout: only page-scope owners go.
451
- krateDisposeExcept(['_lroot_']);
452
- expect(log).toEqual(['page unmount']);
453
- expect(__krateOwner('Page')).toBeNull();
454
- const layout = __krateOwner('Layout');
455
- expect(layout.state).toBe('mounted');
456
- expect(layout.children).toHaveLength(0);
457
- // A full teardown drops everything.
458
- disposeAll();
459
- expect(log).toEqual(['page unmount', 'layout unmount']);
460
- expect(__krateOwner('Layout')).toBeNull();
461
- });
462
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,169 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { readFileSync } from 'node:fs';
3
- import { cn, twMerge, clsx, cva } from './class-helpers';
4
- // Each case: input classes, expected merged output (later wins).
5
- const same = (a, b) => {
6
- expect(twMerge(a, b)).toBe(b);
7
- };
8
- const apart = (a, b) => {
9
- expect(twMerge(a, b).split(' ').sort()).toEqual([a, b].sort());
10
- };
11
- describe('twMerge numeric and arbitrary values', () => {
12
- it('collapses multi-digit values', () => {
13
- same('duration-700', 'duration-500');
14
- same('delay-150', 'delay-300');
15
- same('duration-75', 'duration-1000');
16
- expect(cn('duration-700', 'duration-500')).toBe('duration-500');
17
- });
18
- it('collapses arbitrary and variable values', () => {
19
- same('duration-500', 'duration-[2s]');
20
- same('duration-[2s]', 'duration-(--d)');
21
- same('p-4', 'p-[3px]');
22
- same('w-1/2', 'w-[200px]');
23
- same('mt-2', 'mt-(--gap)');
24
- });
25
- it('handles decimals, fractions, negatives, modifiers and important', () => {
26
- same('p-0.5', 'p-12');
27
- same('w-1/2', 'w-1/3');
28
- same('-mt-2', 'mt-4');
29
- same('mt-4', '-mt-2');
30
- same('bg-red-500/50', 'bg-blue-500');
31
- same('bg-red-500', 'bg-blue-500/20');
32
- same('!p-2', '!p-4');
33
- same('p-2!', 'p-4!');
34
- same('!p-2', 'p-4!');
35
- apart('!p-2', 'p-4');
36
- });
37
- it('scopes by variant chain', () => {
38
- apart('hover:duration-500', 'duration-700');
39
- same('hover:duration-500', 'hover:duration-700');
40
- same('md:hover:p-2', 'hover:md:p-4');
41
- apart('md:p-2', 'p-4');
42
- same('[&>*]:p-2', '[&>*]:p-4');
43
- apart('[&>*]:p-2', '[&_a]:p-4');
44
- });
45
- it('keeps unrelated and unknown classes', () => {
46
- expect(twMerge('px-2', 'py-2')).toBe('px-2 py-2');
47
- expect(twMerge('krate-card', 'krate-card-title', 'krate-card')).toBe('krate-card-title krate-card');
48
- expect(twMerge('foo-bar', 'foo-baz')).toBe('foo-bar foo-baz');
49
- expect(twMerge('a b', 'c a')).toBe('b c a');
50
- });
51
- });
52
- describe('twMerge groups', () => {
53
- it('spacing and sizing', () => {
54
- same('p-2', 'p-4');
55
- same('px-2', 'px-4');
56
- apart('pt-2', 'pb-2');
57
- expect(twMerge('px-2', 'py-2', 'p-4')).toBe('p-4');
58
- expect(twMerge('p-4', 'px-2')).toBe('p-4 px-2');
59
- same('m-2', 'm-8');
60
- same('gap-2', 'gap-10');
61
- apart('gap-x-2', 'gap-y-2');
62
- expect(twMerge('gap-x-2', 'gap-4')).toBe('gap-4');
63
- same('w-10', 'w-full');
64
- same('h-10', 'h-screen');
65
- apart('w-10', 'h-10');
66
- expect(twMerge('w-4', 'h-4', 'size-8')).toBe('size-8');
67
- same('min-w-0', 'min-w-[10px]');
68
- apart('min-w-0', 'max-w-0');
69
- same('z-10', 'z-50');
70
- same('opacity-50', 'opacity-100');
71
- });
72
- it('text size versus color', () => {
73
- apart('text-sm', 'text-red-500');
74
- same('text-sm', 'text-lg');
75
- same('text-sm', 'text-2xl');
76
- same('text-red-500', 'text-blue-500');
77
- same('text-red-500', 'text-[#fff]');
78
- same('text-sm', 'text-[14px]');
79
- apart('text-[14px]', 'text-[#fff]');
80
- apart('text-sm', 'text-center');
81
- same('text-left', 'text-center');
82
- expect(twMerge('leading-6', 'text-lg')).toBe('text-lg');
83
- same('leading-6', 'leading-tight');
84
- same('tracking-tight', 'tracking-wide');
85
- });
86
- it('background', () => {
87
- same('bg-red-500', 'bg-blue-500');
88
- apart('bg-red-500', 'bg-cover');
89
- apart('bg-red-500', 'bg-center');
90
- same('bg-cover', 'bg-contain');
91
- same('bg-center', 'bg-top');
92
- apart('bg-cover', 'bg-center');
93
- same('bg-red-500', 'bg-[#fff]');
94
- same('bg-cover', 'bg-[length:10px_10px]');
95
- apart('bg-no-repeat', 'bg-cover');
96
- });
97
- it('border', () => {
98
- apart('border-2', 'border-red-500');
99
- same('border-2', 'border-4');
100
- same('border', 'border-2');
101
- same('border-red-500', 'border-blue-500');
102
- apart('border-solid', 'border-2');
103
- same('border-solid', 'border-dashed');
104
- apart('border-t-2', 'border-b-2');
105
- same('border-t-2', 'border-t-4');
106
- apart('border-t-2', 'border-t-red-500');
107
- expect(twMerge('border-t-2', 'border-x-2', 'border-4')).toBe('border-4');
108
- same('border-[3px]', 'border-2');
109
- apart('border-[3px]', 'border-[#fff]');
110
- same('rounded', 'rounded-lg');
111
- same('rounded-t-md', 'rounded-t-lg');
112
- apart('rounded-t-md', 'rounded-b-md');
113
- expect(twMerge('rounded-tl-md', 'rounded-t-lg')).toBe('rounded-t-lg');
114
- });
115
- it('shadow, font, motion, layout', () => {
116
- same('shadow-sm', 'shadow-lg');
117
- same('shadow', 'shadow-md');
118
- apart('shadow-md', 'shadow-red-500');
119
- same('shadow-red-500', 'shadow-blue-500');
120
- same('font-bold', 'font-light');
121
- apart('font-bold', 'font-sans');
122
- same('font-sans', 'font-mono');
123
- same('transition', 'transition-colors');
124
- same('transition-all', 'transition-none');
125
- same('ease-in', 'ease-out');
126
- same('ease-in', 'ease-[cubic-bezier(0.4,0,0.2,1)]');
127
- same('animate-spin', 'animate-pulse');
128
- same('animate-spin', 'animate-none');
129
- same('grid-cols-2', 'grid-cols-12');
130
- same('grid-cols-2', 'grid-cols-[1fr_2fr]');
131
- same('flex', 'block');
132
- same('hidden', 'flex');
133
- apart('flex', 'flex-col');
134
- same('flex-row', 'flex-col');
135
- same('absolute', 'relative');
136
- same('ring-2', 'ring-4');
137
- apart('ring-2', 'ring-red-500');
138
- same('outline-2', 'outline-4');
139
- apart('outline-2', 'outline-red-500');
140
- });
141
- it('arbitrary properties', () => {
142
- same('[mask-type:alpha]', '[mask-type:luminance]');
143
- apart('[mask-type:alpha]', '[color:red]');
144
- });
145
- it('works through clsx and cva-style inputs', () => {
146
- expect(cn('p-2', false && 'p-3', ['px-4', { 'px-6': true }])).toBe('p-2 px-6');
147
- expect(clsx('a', null, 'b')).toBe('a b');
148
- });
149
- });
150
- // The compiler folds literal cn/twMerge calls with a Go port of the merge; the
151
- // same vectors run in packages/compiler/internal/irtree/classmerge_test.go.
152
- describe('shared class-merge vectors', () => {
153
- const vectors = JSON.parse(readFileSync('src/testdata/class-merge-vectors.json', 'utf8'));
154
- const fns = { twMerge, cn, clsx };
155
- it('has vectors', () => {
156
- expect(vectors.length).toBeGreaterThan(100);
157
- expect(vectors.some((v) => v.fn === 'cva')).toBe(true);
158
- });
159
- for (const v of vectors) {
160
- it(`${v.fn}: ${v.name}`, () => {
161
- if (v.fn === 'cva') {
162
- expect(cva(v.base ?? '', v.config)(v.selection)).toBe(v.expected);
163
- return;
164
- }
165
- const args = v.src ? new Function(`return [${v.src}]`)() : (v.args ?? []);
166
- expect(fns[v.fn](...args)).toBe(v.expected);
167
- });
168
- }
169
- });
@@ -1 +0,0 @@
1
- export {};