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

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 (153) hide show
  1. package/dist/actions.d.ts +7 -0
  2. package/dist/actions.js +23 -8
  3. package/dist/actions.test.js +8 -410
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/after.test.js +58 -0
  7. package/dist/assets-static.d.ts +10 -1
  8. package/dist/assets-static.js +10 -2
  9. package/dist/base-path.d.ts +7 -0
  10. package/dist/base-path.js +20 -0
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/builtin-tags.test.js +12 -0
  14. package/dist/cache-adapter.test.js +4 -203
  15. package/dist/cache-scope.d.ts +2 -0
  16. package/dist/cache.test.js +1 -197
  17. package/dist/cloudflare-workers.d.ts +2 -0
  18. package/dist/cloudflare-workers.js +27 -0
  19. package/dist/dev-overlay.d.ts +54 -3
  20. package/dist/dev-overlay.js +469 -76
  21. package/dist/dom.js +17 -2
  22. package/dist/events.test.js +2 -407
  23. package/dist/handler-html.test.js +1 -202
  24. package/dist/handler-routing.js +3 -0
  25. package/dist/handler-segments.d.ts +6 -0
  26. package/dist/handler-segments.js +16 -2
  27. package/dist/handler-segments.test.js +8 -200
  28. package/dist/head-tags.d.ts +9 -0
  29. package/dist/head-tags.js +85 -1
  30. package/dist/head-tags.test.js +18 -125
  31. package/dist/index.d.ts +1 -0
  32. package/dist/index.js +3 -0
  33. package/dist/inline-action-token.test.js +31 -0
  34. package/dist/jsx-svg-types.test.js +58 -0
  35. package/dist/jsx-types.d.ts +92 -1
  36. package/dist/krate-core.js +1 -1
  37. package/dist/krate-dev.js +384 -65
  38. package/dist/krate-f-actions.js +1 -1
  39. package/dist/krate-f-navigation.js +1 -1
  40. package/dist/krate-f-router-lite.js +1 -1
  41. package/dist/krate-f-router.js +1 -1
  42. package/dist/krate-features.json +2 -0
  43. package/dist/krate-hydrate-lite.js +5 -5
  44. package/dist/krate-hydrate.js +5 -5
  45. package/dist/krate-runtime.js +5 -5
  46. package/dist/link-base-path.test.js +28 -0
  47. package/dist/metadata.js +3 -0
  48. package/dist/node-server.test.js +1 -355
  49. package/dist/platform.d.ts +52 -0
  50. package/dist/platform.js +57 -0
  51. package/dist/platform.test.js +44 -0
  52. package/dist/router-lite.js +3 -9
  53. package/dist/router-nav.test.js +15 -444
  54. package/dist/router.js +51 -18
  55. package/dist/server-handler.d.ts +49 -0
  56. package/dist/server-handler.js +311 -21
  57. package/dist/server-handler.test.js +56 -344
  58. package/dist/server-renderer.test.js +0 -104
  59. package/dist/server.d.ts +15 -5
  60. package/dist/server.js +150 -22
  61. package/dist/server.test.js +52 -60
  62. package/dist/site-config.d.ts +13 -0
  63. package/dist/site-config.js +28 -0
  64. package/dist/site-config.test.js +30 -0
  65. package/dist/ssr-core-behavior.test.js +3 -515
  66. package/dist/testing.d.ts +59 -0
  67. package/dist/testing.js +88 -0
  68. package/dist/testing.test.js +73 -0
  69. package/package.json +9 -1
  70. package/dist/action-context.test.js +0 -72
  71. package/dist/class-component.test.js +0 -462
  72. package/dist/class-helpers.test.js +0 -169
  73. package/dist/context.test.js +0 -57
  74. package/dist/derived.test.js +0 -140
  75. package/dist/dev-overlay.test.js +0 -165
  76. package/dist/discrete.test.js +0 -100
  77. package/dist/dom.test.js +0 -39
  78. package/dist/effect-errors.test.d.ts +0 -1
  79. package/dist/effect-errors.test.js +0 -199
  80. package/dist/effects.test.d.ts +0 -1
  81. package/dist/effects.test.js +0 -485
  82. package/dist/handler-routing.test.d.ts +0 -1
  83. package/dist/handler-routing.test.js +0 -99
  84. package/dist/highlight-bind.test.d.ts +0 -1
  85. package/dist/highlight-bind.test.js +0 -124
  86. package/dist/highlight.test.d.ts +0 -1
  87. package/dist/highlight.test.js +0 -56
  88. package/dist/lanes.test.d.ts +0 -1
  89. package/dist/lanes.test.js +0 -59
  90. package/dist/list.test.d.ts +0 -1
  91. package/dist/list.test.js +0 -435
  92. package/dist/metadata.test.d.ts +0 -1
  93. package/dist/metadata.test.js +0 -167
  94. package/dist/navigation.test.d.ts +0 -1
  95. package/dist/navigation.test.js +0 -130
  96. package/dist/optimistic-lanes.test.d.ts +0 -1
  97. package/dist/optimistic-lanes.test.js +0 -45
  98. package/dist/patterns.test.d.ts +0 -1
  99. package/dist/patterns.test.js +0 -125
  100. package/dist/portal.test.d.ts +0 -1
  101. package/dist/portal.test.js +0 -99
  102. package/dist/react-effects.test.d.ts +0 -1
  103. package/dist/react-effects.test.js +0 -133
  104. package/dist/react-suspense.test.d.ts +0 -1
  105. package/dist/react-suspense.test.js +0 -194
  106. package/dist/react.test.d.ts +0 -1
  107. package/dist/react.test.js +0 -60
  108. package/dist/react19.test.d.ts +0 -1
  109. package/dist/react19.test.js +0 -176
  110. package/dist/reactivity-core.test.d.ts +0 -1
  111. package/dist/reactivity-core.test.js +0 -152
  112. package/dist/reconcile.test.d.ts +0 -1
  113. package/dist/reconcile.test.js +0 -110
  114. package/dist/refs.test.d.ts +0 -1
  115. package/dist/refs.test.js +0 -257
  116. package/dist/router-layout.test.d.ts +0 -1
  117. package/dist/router-layout.test.js +0 -249
  118. package/dist/router-slots.test.d.ts +0 -1
  119. package/dist/router-slots.test.js +0 -144
  120. package/dist/router-speculation.test.d.ts +0 -1
  121. package/dist/router-speculation.test.js +0 -25
  122. package/dist/router-styles.test.d.ts +0 -1
  123. package/dist/router-styles.test.js +0 -98
  124. package/dist/router.test.d.ts +0 -1
  125. package/dist/router.test.js +0 -36
  126. package/dist/server-async.test.d.ts +0 -1
  127. package/dist/server-async.test.js +0 -216
  128. package/dist/server-highlight.test.d.ts +0 -1
  129. package/dist/server-highlight.test.js +0 -32
  130. package/dist/signal-lanes.test.d.ts +0 -1
  131. package/dist/signal-lanes.test.js +0 -32
  132. package/dist/slots.test.d.ts +0 -1
  133. package/dist/slots.test.js +0 -187
  134. package/dist/snapshots.test.d.ts +0 -1
  135. package/dist/snapshots.test.js +0 -208
  136. package/dist/ssr-core.test.d.ts +0 -1
  137. package/dist/ssr-core.test.js +0 -324
  138. package/dist/static-memory.test.d.ts +0 -1
  139. package/dist/static-memory.test.js +0 -26
  140. package/dist/storage.test.d.ts +0 -1
  141. package/dist/storage.test.js +0 -46
  142. package/dist/suspense-actions.test.d.ts +0 -1
  143. package/dist/suspense-actions.test.js +0 -72
  144. package/dist/suspense.test.d.ts +0 -1
  145. package/dist/suspense.test.js +0 -556
  146. /package/dist/{action-context.test.d.ts → after.test.d.ts} +0 -0
  147. /package/dist/{class-component.test.d.ts → builtin-tags.test.d.ts} +0 -0
  148. /package/dist/{class-helpers.test.d.ts → inline-action-token.test.d.ts} +0 -0
  149. /package/dist/{context.test.d.ts → jsx-svg-types.test.d.ts} +0 -0
  150. /package/dist/{derived.test.d.ts → link-base-path.test.d.ts} +0 -0
  151. /package/dist/{dev-overlay.test.d.ts → platform.test.d.ts} +0 -0
  152. /package/dist/{discrete.test.d.ts → site-config.test.d.ts} +0 -0
  153. /package/dist/{dom.test.d.ts → testing.test.d.ts} +0 -0
package/dist/dom.js CHANGED
@@ -3,6 +3,7 @@ import { krateWithOwner, krateUnmount } from './effects.js';
3
3
  import { krateSanitizeAttr } from './slots.js';
4
4
  import { krateOn, krateControl, krateSetControl } from './events.js';
5
5
  import { krateAttachRef } from './refs.js';
6
+ import { withBase } from './base-path.js';
6
7
  export { krateOn, krateControl, krateSetControl, krateSynthetic, krateEventRoot, krateEventName, krateMountFields, } from './events.js';
7
8
  export { krateAttachRef, krateSetRef, krateRefsEnter, krateRefsLeave, krateRefsFlush, krateRefsHold } from './refs.js';
8
9
  // per-node effect tracking
@@ -288,9 +289,22 @@ function setAttr(el, key, value) {
288
289
  }
289
290
  }
290
291
  }
292
+ /** A built-in <Link> lowers to these props; plain anchors are left as written. */
293
+ function isLinkProps(props) {
294
+ if ('data-krate-no-base' in props)
295
+ return false;
296
+ return 'data-krate-link' in props || 'data-krate-external' in props;
297
+ }
298
+ function linkHref(href) {
299
+ if (typeof href === 'string')
300
+ return withBase(href);
301
+ if (typeof href === 'function')
302
+ return () => linkHref(href());
303
+ return href;
304
+ }
291
305
  function applyProps(el, props) {
292
306
  for (const key in props) {
293
- const value = props[key];
307
+ const value = key === 'href' && isLinkProps(props) ? linkHref(props[key]) : props[key];
294
308
  if (typeof value === 'function' && (key === 'value' || key === 'checked')) {
295
309
  trackEffect(el, krateControl(el, key, value));
296
310
  }
@@ -323,7 +337,8 @@ export function krateSpread(el, get) {
323
337
  if (key === 'children' || key === 'key' || key === 'dangerouslySetInnerHTML')
324
338
  continue;
325
339
  next.add(key);
326
- setAttr(el, key, props[key]);
340
+ const isLink = key === 'href' && (isLinkProps(props) || (el.hasAttribute('data-krate-link') && !('data-krate-no-base' in props)));
341
+ setAttr(el, key, isLink ? linkHref(props[key]) : props[key]);
327
342
  }
328
343
  const prev = spreadKeys.get(el);
329
344
  if (prev) {
@@ -1,8 +1,6 @@
1
- import { describe, it, expect, beforeEach, vi } from 'vitest';
2
- import { krateOn, krateControl, krateEventName, krateEventRoot } from './events';
1
+ import { describe, it, expect, beforeEach } from 'vitest';
2
+ import { krateOn, krateControl } from './events';
3
3
  import { createSignal } from './signal';
4
- import { h } from './dom';
5
- const tick = () => new Promise((r) => setTimeout(r, 0));
6
4
  function setup(html) {
7
5
  document.body.innerHTML = '<div id="root">' + html + '</div>';
8
6
  return document.getElementById('root');
@@ -15,14 +13,6 @@ function fire(el, type, init = { bubbles: true, cancelable: true }) {
15
13
  el.dispatchEvent(e);
16
14
  return e;
17
15
  }
18
- describe('krateEventName', () => {
19
- it('splits capture variants but keeps pointer-capture event names', () => {
20
- expect(krateEventName('onClick')).toEqual(['click', false]);
21
- expect(krateEventName('onClickCapture')).toEqual(['click', true]);
22
- expect(krateEventName('onGotPointerCapture')).toEqual(['gotpointercapture', false]);
23
- expect(krateEventName('onLostPointerCaptureCapture')).toEqual(['lostpointercapture', true]);
24
- });
25
- });
26
16
  describe('delegated events', () => {
27
17
  beforeEach(() => {
28
18
  document.body.innerHTML = '';
@@ -37,287 +27,6 @@ describe('delegated events', () => {
37
27
  $('#c').click();
38
28
  expect(log).toEqual(['a:capture', 'b:capture', 'c:capture', 'c:bubble', 'b:bubble', 'a:bubble']);
39
29
  });
40
- it('sets currentTarget to the element whose handler runs, and clears it after', () => {
41
- setup('<div id="a"><button id="c">x</button></div>');
42
- const seen = [];
43
- let saved;
44
- krateOn($('#a'), 'onClick', (e) => {
45
- seen.push([e.currentTarget.id, e.target.id, e.type]);
46
- saved = e;
47
- });
48
- krateOn($('#c'), 'onClick', (e) => seen.push([e.currentTarget.id, e.target.id, e.type]));
49
- $('#c').click();
50
- expect(seen).toEqual([
51
- ['c', 'c', 'click'],
52
- ['a', 'c', 'click'],
53
- ]);
54
- expect(saved.currentTarget).toBe(null);
55
- });
56
- it('exposes the synthetic event surface', () => {
57
- setup('<input id="i">');
58
- let got;
59
- krateOn($('#i'), 'onKeyDown', (e) => {
60
- got = e;
61
- e.persist();
62
- });
63
- const native = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', shiftKey: true, bubbles: true, cancelable: true });
64
- $('#i').dispatchEvent(native);
65
- expect(got.nativeEvent).toBe(native);
66
- expect(got.key).toBe('Enter');
67
- expect(got.code).toBe('Enter');
68
- expect(got.shiftKey).toBe(true);
69
- expect(got.bubbles).toBe(true);
70
- expect(got.cancelable).toBe(true);
71
- expect(typeof got.timeStamp).toBe('number');
72
- expect(got.isDefaultPrevented()).toBe(false);
73
- expect(got.isPropagationStopped()).toBe(false);
74
- expect(got.isPersistent()).toBe(true);
75
- expect(got.getModifierState('Shift')).toBe(native.getModifierState('Shift'));
76
- });
77
- it('preventDefault reaches the native event', () => {
78
- setup('<a id="l" href="#x">x</a>');
79
- let synthetic;
80
- krateOn($('#l'), 'onClick', (e) => {
81
- e.preventDefault();
82
- synthetic = e;
83
- });
84
- const native = fire($('#l'), 'click');
85
- expect(native.defaultPrevented).toBe(true);
86
- expect(synthetic.defaultPrevented).toBe(true);
87
- expect(synthetic.isDefaultPrevented()).toBe(true);
88
- });
89
- it('stopPropagation stops React ancestors and native listeners above the root', () => {
90
- setup('<div id="a"><button id="c">x</button></div>');
91
- const log = [];
92
- krateOn($('#a'), 'onClick', () => log.push('a'));
93
- krateOn($('#c'), 'onClick', (e) => {
94
- log.push('c');
95
- e.stopPropagation();
96
- expect(e.isPropagationStopped()).toBe(true);
97
- });
98
- const onDoc = () => log.push('document');
99
- document.addEventListener('click', onDoc);
100
- $('#c').click();
101
- document.removeEventListener('click', onDoc);
102
- expect(log).toEqual(['c']);
103
- });
104
- it('stopPropagation in a capture handler prevents bubble handlers', () => {
105
- setup('<div id="a"><button id="c">x</button></div>');
106
- const log = [];
107
- krateOn($('#a'), 'onClickCapture', (e) => {
108
- log.push('a:capture');
109
- e.stopPropagation();
110
- });
111
- krateOn($('#c'), 'onClick', () => log.push('c'));
112
- $('#c').click();
113
- expect(log).toEqual(['a:capture']);
114
- });
115
- it('interleaves with native listeners like React 17+ root delegation', () => {
116
- setup('<div id="a"><button id="c">x</button></div>');
117
- const log = [];
118
- krateOn($('#a'), 'onClick', () => log.push('react a'));
119
- krateOn($('#c'), 'onClick', () => log.push('react c'));
120
- krateOn($('#c'), 'onClickCapture', () => log.push('react c capture'));
121
- const c = $('#c');
122
- const nativeC = () => log.push('native c');
123
- const nativeCap = () => log.push('native c capture');
124
- const nativeDoc = () => log.push('native document');
125
- const nativeDocCap = () => log.push('native document capture');
126
- c.addEventListener('click', nativeC);
127
- c.addEventListener('click', nativeCap, true);
128
- document.addEventListener('click', nativeDoc);
129
- document.addEventListener('click', nativeDocCap, true);
130
- c.click();
131
- document.removeEventListener('click', nativeDoc);
132
- document.removeEventListener('click', nativeDocCap, true);
133
- expect(log).toEqual([
134
- 'native document capture',
135
- 'react c capture',
136
- 'native c capture',
137
- 'native c',
138
- 'react c',
139
- 'react a',
140
- 'native document',
141
- ]);
142
- });
143
- it('a native stopPropagation inside the root hides the event from React', () => {
144
- setup('<div id="a"><button id="c">x</button></div>');
145
- const log = [];
146
- krateOn($('#a'), 'onClick', () => log.push('react a'));
147
- $('#c').addEventListener('click', (e) => e.stopPropagation());
148
- $('#c').click();
149
- expect(log).toEqual([]);
150
- });
151
- it('maps onDoubleClick, onFocus and onBlur', () => {
152
- setup('<div id="w"><input id="i"></div>');
153
- const log = [];
154
- krateOn($('#w'), 'onDoubleClick', (e) => log.push('dbl:' + e.type));
155
- krateOn($('#w'), 'onFocus', (e) => log.push('focus:' + e.type));
156
- krateOn($('#w'), 'onBlur', (e) => log.push('blur:' + e.type));
157
- fire($('#i'), 'dblclick');
158
- fire($('#i'), 'focusin');
159
- fire($('#i'), 'focusout');
160
- expect(log).toEqual(['dbl:dblclick', 'focus:focus', 'blur:blur']);
161
- });
162
- it('attaches non-bubbling events to the element', () => {
163
- setup('<div id="p"><div id="c"></div></div>');
164
- const log = [];
165
- krateOn($('#p'), 'onScroll', () => log.push('p scroll'));
166
- krateOn($('#c'), 'onScroll', (e) => log.push('c scroll ' + e.currentTarget.id));
167
- krateOn($('#p'), 'onMouseEnter', (e) => log.push('p enter ' + e.type));
168
- krateOn($('#c'), 'onMouseLeave', () => log.push('c leave'));
169
- fire($('#c'), 'scroll', { bubbles: false });
170
- fire($('#p'), 'mouseenter', { bubbles: false });
171
- fire($('#c'), 'mouseleave', { bubbles: false });
172
- fire($('#c'), 'mouseenter', { bubbles: false });
173
- expect(log).toEqual(['c scroll c', 'p enter mouseenter', 'c leave']);
174
- });
175
- it('dispatches media and composition events', () => {
176
- setup('<div id="w"><video id="v"></video><input id="i"></div>');
177
- const log = [];
178
- krateOn($('#v'), 'onPlay', () => log.push('play'));
179
- krateOn($('#w'), 'onPlay', () => log.push('wrapper play'));
180
- krateOn($('#w'), 'onCompositionStart', (e) => log.push('start ' + e.data));
181
- krateOn($('#w'), 'onCompositionEnd', (e) => log.push('end ' + e.data));
182
- krateOn($('#w'), 'onBeforeInput', (e) => log.push('before ' + e.data));
183
- fire($('#v'), 'play', { bubbles: false });
184
- // happy-dom's CompositionEvent has no data; use plain events carrying it.
185
- const comp = (type, data) => Object.assign(new Event(type, { bubbles: true }), { data });
186
- $('#i').dispatchEvent(comp('compositionstart', ''));
187
- $('#i').dispatchEvent(comp('compositionend', 'ka'));
188
- $('#i').dispatchEvent(new InputEvent('beforeinput', { bubbles: true, data: 'x', inputType: 'insertText' }));
189
- $('#i').dispatchEvent(new InputEvent('beforeinput', { bubbles: true, inputType: 'deleteContentBackward' }));
190
- expect(log).toEqual(['play', 'start ', 'end ka', 'before ka', 'before x']);
191
- });
192
- it('dispatches onSubmit and onReset', () => {
193
- setup('<form id="f"><button id="b">go</button></form>');
194
- const log = [];
195
- krateOn($('#f'), 'onSubmit', (e) => {
196
- e.preventDefault();
197
- log.push('submit');
198
- });
199
- krateOn($('#f'), 'onReset', () => log.push('reset'));
200
- const s = fire($('#f'), 'submit');
201
- fire($('#f'), 'reset');
202
- expect(s.defaultPrevented).toBe(true);
203
- expect(log).toEqual(['submit', 'reset']);
204
- });
205
- it('replaces and removes handlers', () => {
206
- setup('<button id="c">x</button>');
207
- const log = [];
208
- krateOn($('#c'), 'onClick', () => log.push('one'));
209
- krateOn($('#c'), 'onClick', () => log.push('two'));
210
- $('#c').click();
211
- krateOn($('#c'), 'onClick', null);
212
- $('#c').click();
213
- expect(log).toEqual(['two']);
214
- });
215
- it('runs every handler and rethrows the first error afterwards', () => {
216
- setup('<div id="a"><button id="c">x</button></div>');
217
- const log = [];
218
- krateOn($('#c'), 'onClick', () => {
219
- throw new Error('boom');
220
- });
221
- krateOn($('#a'), 'onClick', () => log.push('a'));
222
- const errors = [];
223
- const onErr = (e) => {
224
- errors.push(e.error ?? e.message);
225
- e.preventDefault();
226
- };
227
- window.addEventListener('error', onErr);
228
- try {
229
- $('#c').dispatchEvent(new Event('click', { bubbles: true }));
230
- }
231
- catch (e) {
232
- errors.push(e);
233
- }
234
- window.removeEventListener('error', onErr);
235
- expect(log).toEqual(['a']);
236
- expect(errors.length).toBe(1);
237
- });
238
- it('handles events in portals outside the root', () => {
239
- setup('');
240
- const portal = document.createElement('div');
241
- portal.innerHTML = '<button id="p">x</button>';
242
- document.body.appendChild(portal);
243
- const log = [];
244
- krateOn($('#p'), 'onClick', () => log.push('portal'));
245
- krateEventRoot(portal);
246
- $('#p').click();
247
- expect(log).toEqual(['portal']);
248
- });
249
- it('wires handlers set through h()', () => {
250
- const root = setup('');
251
- const log = [];
252
- const el = h('button', { onClick: (e) => log.push('h ' + e.type), onDoubleClick: () => log.push('dbl') });
253
- root.appendChild(el);
254
- el.click();
255
- fire(el, 'dblclick');
256
- expect(log).toEqual(['h click', 'dbl']);
257
- });
258
- });
259
- describe('onChange', () => {
260
- it('fires on every input event for text fields, after onInput', () => {
261
- setup('<div id="w"><input id="i"><textarea id="t"></textarea></div>');
262
- const log = [];
263
- krateOn($('#i'), 'onInput', () => log.push('input'));
264
- krateOn($('#i'), 'onChange', (e) => log.push('change ' + e.type + ' ' + e.target.value));
265
- krateOn($('#w'), 'onChange', () => log.push('wrapper change'));
266
- krateOn($('#t'), 'onChange', (e) => log.push('textarea ' + e.target.value));
267
- const i = $('#i');
268
- i.value = 'a';
269
- fire(i, 'input');
270
- i.value = 'ab';
271
- fire(i, 'input');
272
- // The native change on blur carries no new value: no second onChange.
273
- fire(i, 'change');
274
- const t = $('#t');
275
- t.value = 'x';
276
- fire(t, 'input');
277
- expect(log).toEqual([
278
- 'input',
279
- 'change change a',
280
- 'wrapper change',
281
- 'input',
282
- 'change change ab',
283
- 'wrapper change',
284
- 'textarea x',
285
- 'wrapper change',
286
- ]);
287
- });
288
- it('runs onChangeCapture before onChange', () => {
289
- setup('<div id="w"><input id="i"></div>');
290
- const log = [];
291
- krateOn($('#w'), 'onChangeCapture', () => log.push('w capture'));
292
- krateOn($('#i'), 'onChange', () => log.push('i'));
293
- krateOn($('#w'), 'onChange', () => log.push('w'));
294
- $('#i').value = 'z';
295
- fire($('#i'), 'input');
296
- expect(log).toEqual(['w capture', 'i', 'w']);
297
- });
298
- it('fires on click for checkboxes and only for a newly checked radio', () => {
299
- setup('<input id="c" type="checkbox"><input id="r1" type="radio" name="g" checked><input id="r2" type="radio" name="g">');
300
- const log = [];
301
- krateOn($('#c'), 'onClick', () => log.push('click'));
302
- krateOn($('#c'), 'onChange', (e) => log.push('checkbox ' + e.target.checked));
303
- krateOn($('#r1'), 'onChange', () => log.push('r1'));
304
- krateOn($('#r2'), 'onChange', () => log.push('r2'));
305
- $('#c').click();
306
- $('#c').click();
307
- $('#r1').click();
308
- $('#r2').click();
309
- $('#r1').click();
310
- expect(log).toEqual(['click', 'checkbox true', 'click', 'checkbox false', 'r2', 'r1']);
311
- });
312
- it('fires on change for selects', () => {
313
- setup('<select id="s"><option value="a">A</option><option value="b">B</option></select>');
314
- const log = [];
315
- krateOn($('#s'), 'onChange', (e) => log.push(e.target.value));
316
- $('#s').value = 'b';
317
- fire($('#s'), 'input');
318
- fire($('#s'), 'change');
319
- expect(log).toEqual(['b']);
320
- });
321
30
  });
322
31
  describe('controlled form controls', () => {
323
32
  it('restores a controlled text input when the handler does not update state', async () => {
@@ -330,118 +39,4 @@ describe('controlled form controls', () => {
330
39
  fire(i, 'input');
331
40
  expect(i.value).toBe('a');
332
41
  });
333
- it('restores even without an onChange handler (read-only controlled field)', () => {
334
- setup('<input id="i" value="a">');
335
- const i = $('#i');
336
- krateControl(i, 'value', () => 'a');
337
- i.value = 'typed';
338
- fire(i, 'input');
339
- expect(i.value).toBe('a');
340
- });
341
- it('keeps the typed value when state follows it, including transforms', async () => {
342
- setup('<input id="i" value="">');
343
- const i = $('#i');
344
- const [v, setV] = createSignal('');
345
- krateControl(i, 'value', () => v());
346
- krateOn(i, 'onChange', (e) => setV(e.target.value.toUpperCase()));
347
- i.value = 'ab';
348
- fire(i, 'input');
349
- expect(i.value).toBe('AB');
350
- await tick();
351
- expect(i.value).toBe('AB');
352
- expect(i.defaultValue).toBe('AB');
353
- });
354
- it('restores a controlled checkbox and radio group', () => {
355
- setup('<input id="c" type="checkbox"><input id="r1" type="radio" name="g" checked><input id="r2" type="radio" name="g">');
356
- const c = $('#c');
357
- krateControl(c, 'checked', () => false);
358
- krateOn(c, 'onChange', () => { });
359
- c.click();
360
- expect(c.checked).toBe(false);
361
- const [sel, setSel] = createSignal('r1');
362
- const r1 = $('#r1');
363
- const r2 = $('#r2');
364
- krateControl(r1, 'checked', () => sel() === 'r1');
365
- krateControl(r2, 'checked', () => sel() === 'r2');
366
- krateOn(r2, 'onChange', () => { });
367
- r2.click();
368
- expect([r1.checked, r2.checked]).toEqual([true, false]);
369
- krateOn(r2, 'onChange', () => setSel('r2'));
370
- r2.click();
371
- expect([r1.checked, r2.checked]).toEqual([false, true]);
372
- });
373
- it('controls single and multiple selects', async () => {
374
- setup('<select id="s"><option value="a">A</option><option value="b">B</option></select>' +
375
- '<select id="m" multiple><option value="a">A</option><option value="b">B</option><option value="c">C</option></select>');
376
- const s = $('#s');
377
- const m = $('#m');
378
- const [v, setV] = createSignal('b');
379
- const [mv, setMv] = createSignal(['a', 'c']);
380
- krateControl(s, 'value', () => v());
381
- krateControl(m, 'value', () => mv());
382
- expect(s.value).toBe('b');
383
- expect(Array.from(m.options).filter((o) => o.selected).map((o) => o.value)).toEqual(['a', 'c']);
384
- setMv(['b']);
385
- setV('a');
386
- await tick();
387
- expect(s.value).toBe('a');
388
- expect(Array.from(m.options).filter((o) => o.selected).map((o) => o.value)).toEqual(['b']);
389
- // A handler that ignores the change: the select snaps back.
390
- krateOn(s, 'onChange', () => { });
391
- s.value = 'b';
392
- fire(s, 'change');
393
- expect(s.value).toBe('a');
394
- });
395
- it('treats value={undefined} as uncontrolled and warns on switching', async () => {
396
- setup('<input id="i">');
397
- const i = $('#i');
398
- const err = vi.spyOn(console, 'error').mockImplementation(() => { });
399
- const [v, setV] = createSignal(undefined);
400
- krateControl(i, 'value', () => v());
401
- i.value = 'typed';
402
- fire(i, 'input');
403
- expect(i.value).toBe('typed');
404
- setV('now');
405
- await tick();
406
- expect(i.value).toBe('now');
407
- expect(err).toHaveBeenCalledTimes(1);
408
- expect(String(err.mock.calls[0][0])).toContain('changing an uncontrolled input to be controlled');
409
- err.mockRestore();
410
- });
411
- it('applies defaultValue/defaultChecked only as defaults; form reset restores them', async () => {
412
- setup('<form id="f"><input id="i" value="d"><input id="c" type="checkbox"></form>');
413
- const i = $('#i');
414
- const c = $('#c');
415
- const [d, setD] = createSignal('d');
416
- krateControl(i, 'defaultValue', () => d());
417
- krateControl(c, 'defaultChecked', () => true);
418
- i.value = 'user';
419
- setD('d2');
420
- await tick();
421
- expect(i.value).toBe('user');
422
- expect(i.defaultValue).toBe('d2');
423
- $('#f').reset();
424
- expect(i.value).toBe('d2');
425
- expect(c.checked).toBe(true);
426
- });
427
- it('keeps number and range inputs controlled', () => {
428
- setup('<input id="n" type="number" value="1"><input id="r" type="range" min="0" max="10" value="5">');
429
- const n = $('#n');
430
- const r = $('#r');
431
- const [num, setNum] = createSignal(1);
432
- krateControl(n, 'value', () => num());
433
- krateControl(r, 'value', () => 5);
434
- const log = [];
435
- krateOn(n, 'onChange', (e) => {
436
- log.push(e.target.value);
437
- setNum(Number(e.target.value));
438
- });
439
- n.value = '7';
440
- fire(n, 'input');
441
- r.value = '8';
442
- fire(r, 'input');
443
- expect(log).toEqual(['7']);
444
- expect(n.value).toBe('7');
445
- expect(r.value).toBe('5');
446
- });
447
42
  });