scenarios.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /**
  2. * DOM-walk + forms-module scenarios for the Statum client.
  3. *
  4. * Each scenario runs in a FRESH QuickJS context (run.sh invokes this script
  5. * once per scenario) with a minimal DOM shim (shim.js) that supports the
  6. * render walk and the forms module end to end, including capture-phase
  7. * events, form reset defaults and a scriptable fetch stub.
  8. *
  9. * State enters through the real machinery: a `set` directive returned by the
  10. * fetch stub and applied by bridge.loadEntrypoint() (which re-renders through
  11. * applyDirectives -> renderAll). Signatures vary per step so the idempotency
  12. * guard never short-circuits a deliberate re-render.
  13. *
  14. * 1 walk: stm-if/stm-else chain, stm-text, stm-class.*
  15. * 2 stm-for keyed: node identity preserved per key, removal honored
  16. * 3 stm-for unkeyed: instances rebuilt (all identities change)
  17. * 4 stm-prefill: input + textarea property writes (no attribute)
  18. * 5 stm-prefill: select single + multiple (array)
  19. * 6 stm-prefill: checkbox truthy/falsy/null
  20. * 7 stm-prefill: radio group, same expression per member
  21. * 8 dirty skip: dirty input keeps user value, clean sibling re-fills
  22. * 9 dirty is per element within a form
  23. * 10 reset event un-dirties; prefill re-applies after reset
  24. * 11 stm-clear-on-success: success resets + un-dirties the form
  25. * 12 stm-clear-on-success: failure keeps values and dirty state
  26. * 13 keyed loop of forms: clearing instance 2 leaves 1 and 3 alone
  27. * 14 two sibling forms: clearing A leaves B alone
  28. * 15 statum.forms.reset: resets exactly the given form
  29. * 16 handler order: stm-prefill registered before stm-class.*
  30. *
  31. * Usage: qjs tests/dom/scenarios.js <1..16>
  32. */
  33. import * as std from 'std';
  34. import { installShim } from './shim.js';
  35. // Repo root as a CWD-relative path; run.sh cds to the repo root first (same
  36. // convention as tests/markdown/spec-test.js).
  37. var ROOT = '.';
  38. var scenario = scriptArgs.length > 1 ? scriptArgs[1] : '';
  39. function fail(msg) {
  40. std.err.puts('FAIL [' + scenario + '] ' + msg + '\n');
  41. std.exit(1);
  42. }
  43. function check(cond, msg) {
  44. if (!cond) fail(msg);
  45. }
  46. function load(name) {
  47. (0, eval)(std.loadFile(ROOT + '/js/' + name));
  48. }
  49. function loadAll() {
  50. load('statum-core.js');
  51. load('statum-document.js');
  52. load('statum-markdown.js');
  53. load('statum-forms.js');
  54. }
  55. /** Yield to the job queue n times so promise chains settle. */
  56. async function spin(n) {
  57. for (var i = 0; i < (n || 30); i++) await Promise.resolve();
  58. }
  59. var sigCounter = 0;
  60. /**
  61. * Put `public` data into a slot type through a set directive, then re-render.
  62. * Uses loadEntrypoint (public bridge machinery) so the exact production path
  63. * (fetch -> handleResponse -> applyDirectives -> renderAll) runs.
  64. */
  65. async function setState(typeName, publicData) {
  66. sigCounter++;
  67. var snapshot = { slot: { key: 'k-' + typeName, type: typeName, scope: 'page' }, public: publicData };
  68. globalThis.__shim.setFetch(function () {
  69. return globalThis.__shim.okResponse([{
  70. type: 'set',
  71. snapshot_frame: { content: JSON.stringify(snapshot), signature: 'sig-' + sigCounter }
  72. }]);
  73. });
  74. await globalThis.statum._bridge.loadEntrypoint();
  75. await spin();
  76. }
  77. /** Fire a real (bubbling, non-cancelable-by-default) event on an element. */
  78. function fire(el, type) {
  79. var evt = globalThis.__shim.makeEvent(type);
  80. el.dispatchEvent(evt);
  81. return evt;
  82. }
  83. function el(tag) { return globalThis.__shim.makeElement(tag); }
  84. function input(type, name, value) {
  85. var i = el('input');
  86. if (type) i.setAttribute('type', type);
  87. if (name) i.setAttribute('name', name);
  88. if (value !== undefined) i.setAttribute('value', value);
  89. return i;
  90. }
  91. async function main() {
  92. installShim();
  93. var log = globalThis.__shim.log;
  94. var body = globalThis.__shim.body;
  95. loadAll();
  96. var s = globalThis.statum;
  97. check(s && s._bridge, 'statum did not load');
  98. check(s.forms && typeof s.forms.reset === 'function', 'statum.forms.reset missing');
  99. check(typeof s.forms.markDirty === 'function' && typeof s.forms.isDirty === 'function',
  100. 'statum.forms.markDirty/isDirty missing');
  101. check(s._bridge.formsDirty instanceof WeakSet, 'bridge.formsDirty missing');
  102. check(typeof s._bridge.markDirty === 'function' && typeof s._bridge.isDirty === 'function',
  103. 'bridge.markDirty/isDirty missing');
  104. if (scenario === '1') {
  105. // if/else-if/else chain + stm-text + stm-class.*
  106. var w = el('div');
  107. var a = el('p'); a.setAttribute('stm-if', "m.mood === 'up'"); a.textContent = 'up';
  108. var b = el('p'); b.setAttribute('stm-else-if', "m.mood === 'mid'"); b.textContent = 'mid';
  109. var c = el('p'); c.setAttribute('stm-else', ''); c.textContent = 'down';
  110. w.appendChild(a); w.appendChild(b); w.appendChild(c);
  111. var t = el('span'); t.setAttribute('stm-text', 'm.mood');
  112. var cl = el('span'); cl.setAttribute('stm-class.hi', "m.mood === 'up'");
  113. w.appendChild(t); w.appendChild(cl);
  114. body.appendChild(w);
  115. await setState('m', { mood: 'up' });
  116. check(!a.hasAttribute('hidden') && b.hasAttribute('hidden') && c.hasAttribute('hidden'),
  117. 'branch up not exclusively shown');
  118. check(t.textContent === 'up', 'stm-text wrote ' + JSON.stringify(t.textContent));
  119. check(cl.classList.contains('hi'), 'stm-class.hi not applied');
  120. await setState('m', { mood: 'mid' });
  121. check(a.hasAttribute('hidden') && !b.hasAttribute('hidden') && c.hasAttribute('hidden'),
  122. 'branch mid not exclusively shown');
  123. check(!cl.classList.contains('hi'), 'stm-class.hi not removed');
  124. check(t.textContent === 'mid', 'stm-text not updated');
  125. await setState('m', { mood: 'zzz' });
  126. check(a.hasAttribute('hidden') && b.hasAttribute('hidden') && !c.hasAttribute('hidden'),
  127. 'else branch not shown');
  128. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  129. } else if (scenario === '2') {
  130. // Keyed loop: identity per key preserved; removal honored.
  131. var holder = el('div');
  132. holder.setAttribute('stm-for-item-in', 'list.items');
  133. holder.setAttribute('stm-key', 'item.id');
  134. var row = el('p');
  135. row.setAttribute('stm-text', 'item.name');
  136. holder.appendChild(row);
  137. body.appendChild(holder);
  138. await setState('list', { items: [{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }] });
  139. var insts = holder.parentNode.children.filter(function (n) { return n !== holder; });
  140. // holder is children[0]; instances follow it in order.
  141. var kids = body.children.filter(function (n) { return n !== holder; });
  142. check(kids.length === 3, 'expected 3 instances, got ' + kids.length);
  143. var k1 = kids[0], k2 = kids[1], k3 = kids[2];
  144. // The stm-text binding is on the template's child, so read the child.
  145. check(k1.children[0].textContent === 'a' && k2.children[0].textContent === 'b' &&
  146. k3.children[0].textContent === 'c',
  147. 'instance texts: ' + kids.map(function (n) { return n.children[0] && n.children[0].textContent; }).join(','));
  148. await setState('list', { items: [{ id: 1, name: 'a' }, { id: 3, name: 'c3' }] });
  149. var kids2 = body.children.filter(function (n) { return n !== holder; });
  150. check(kids2.length === 2, 'expected 2 instances after removal, got ' + kids2.length);
  151. check(kids2[0] === k1, 'key 1 node identity not preserved');
  152. check(kids2[1] === k3, 'key 3 node identity not preserved');
  153. check(kids2[1].children[0].textContent === 'c3', 'key 3 text not refreshed');
  154. check(k2.parentNode === null, 'removed instance still attached');
  155. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  156. } else if (scenario === '3') {
  157. // Unkeyed loop: rebuild wholesale.
  158. var holder = el('div');
  159. holder.setAttribute('stm-for-item-in', 'list.items');
  160. var row = el('p');
  161. row.setAttribute('stm-text', 'item.name');
  162. holder.appendChild(row);
  163. body.appendChild(holder);
  164. await setState('list', { items: [{ name: 'a' }, { name: 'b' }] });
  165. var kids = body.children.filter(function (n) { return n !== holder; });
  166. check(kids.length === 2 && kids[0].children[0].textContent === 'a' &&
  167. kids[1].children[0].textContent === 'b', 'initial render');
  168. await setState('list', { items: [{ name: 'a' }, { name: 'b' }] });
  169. var kids2 = body.children.filter(function (n) { return n !== holder; });
  170. check(kids2.length === 2, 'instance count changed');
  171. check(kids2[0] !== kids[0] && kids2[1] !== kids[1], 'unkeyed loop reused nodes');
  172. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  173. } else if (scenario === '4') {
  174. // Prefill writes the value PROPERTY, never the value attribute.
  175. var f = el('form');
  176. var ti = input('text', 'who'); ti.setAttribute('stm-prefill', 'p.who');
  177. var ta = el('textarea'); ta.setAttribute('name', 'bio'); ta.setAttribute('stm-prefill', 'p.bio');
  178. f.appendChild(ti); f.appendChild(ta);
  179. body.appendChild(f);
  180. await setState('p', { who: 'ada', bio: 'hi' });
  181. check(ti.value === 'ada', 'text input value = ' + JSON.stringify(ti.value));
  182. check(ta.value === 'hi', 'textarea value = ' + JSON.stringify(ta.value));
  183. check(ti.getAttribute('value') === null, 'text input value ATTRIBUTE was written');
  184. check(ta.getAttribute('stm-prefill') === 'p.bio', 'stm-prefill attribute consumed');
  185. await setState('p', { who: null, bio: undefined });
  186. check(ti.value === '', 'null should prefill as empty string, got ' + JSON.stringify(ti.value));
  187. check(ta.value === 'hi', 'undefined must leave the element untouched');
  188. await setState('p', { who: 42, bio: 'x' });
  189. check(ti.value === '42', 'number stringified: ' + JSON.stringify(ti.value));
  190. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  191. } else if (scenario === '5') {
  192. // Select single + multiple.
  193. var f = el('form');
  194. var sel = el('select'); sel.setAttribute('stm-prefill', 'p.pick');
  195. var o1 = el('option'); o1.setAttribute('value', 'a'); o1.textContent = 'A';
  196. var o2 = el('option'); o2.setAttribute('value', 'b'); o2.textContent = 'B';
  197. sel.appendChild(o1); sel.appendChild(o2);
  198. var multi = el('select'); multi.setAttribute('multiple', ''); multi.setAttribute('stm-prefill', 'p.set');
  199. var m1 = el('option'); m1.setAttribute('value', 'x');
  200. var m2 = el('option'); m2.setAttribute('value', 'y');
  201. var m3 = el('option'); m3.setAttribute('value', 'z');
  202. multi.appendChild(m1); multi.appendChild(m2); multi.appendChild(m3);
  203. f.appendChild(sel); f.appendChild(multi);
  204. body.appendChild(f);
  205. await setState('p', { pick: 'b', set: ['x', 'z'] });
  206. check(sel.value === 'b', 'single select value = ' + JSON.stringify(sel.value));
  207. check(o2.selected === true && o1.selected === false, 'single select option states wrong');
  208. check(m1.selected === true && m2.selected === false && m3.selected === true,
  209. 'multiple select option states wrong');
  210. await setState('p', { pick: 'nope', set: 'y' });
  211. check(sel.value === '', 'unmatched single select should yield empty value');
  212. check(m1.selected === false && m2.selected === true && m3.selected === false,
  213. 'non-array multiple value must behave like single');
  214. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  215. } else if (scenario === '6') {
  216. // Checkbox: truthy -> checked, falsy/null -> unchecked.
  217. var f = el('form');
  218. var cb = input('checkbox', 'ok'); cb.setAttribute('stm-prefill', 'p.ok');
  219. f.appendChild(cb);
  220. body.appendChild(f);
  221. await setState('p', { ok: true });
  222. check(cb.checked === true, 'truthy did not check');
  223. await setState('p', { ok: false });
  224. check(cb.checked === false, 'falsy did not uncheck');
  225. await setState('p', { ok: null });
  226. check(cb.checked === false, 'null did not uncheck');
  227. await setState('p', { ok: 'yes' });
  228. check(cb.checked === true, 'non-empty string should check');
  229. await setState('p', {}); // ok undefined -> untouched
  230. check(cb.checked === true, 'undefined must leave checked untouched');
  231. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  232. } else if (scenario === '7') {
  233. // Radio group: every member carries the same expression; the member whose
  234. // own value matches the resolved value gets checked.
  235. var f = el('form');
  236. var mk = function (v) {
  237. var r = input('radio', 'g', v);
  238. r.setAttribute('stm-prefill', 'p.choice');
  239. f.appendChild(r);
  240. return r;
  241. };
  242. var ra = mk('a'), rb = mk('b'), rc = mk('c');
  243. body.appendChild(f);
  244. await setState('p', { choice: 'b' });
  245. check(ra.checked === false && rb.checked === true && rc.checked === false,
  246. 'state b checked the wrong member(s)');
  247. await setState('p', { choice: 'd' });
  248. check(ra.checked === false && rb.checked === false && rc.checked === false,
  249. 'state d (no match) must leave all unchecked');
  250. await setState('p', { choice: null });
  251. check(rb.checked === false, 'null must uncheck (String(null) matches nothing)');
  252. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  253. } else if (scenario === '8') {
  254. // A dirty element is never overwritten; a clean sibling still fills.
  255. var f = el('form');
  256. var i1 = input('text', 'one'); i1.setAttribute('stm-prefill', 'p.one');
  257. var i2 = input('text', 'two'); i2.setAttribute('stm-prefill', 'p.two');
  258. f.appendChild(i1); f.appendChild(i2);
  259. body.appendChild(f);
  260. await setState('p', { one: 'x', two: 'x' });
  261. check(i1.value === 'x' && i2.value === 'x', 'initial fill failed');
  262. fire(i1, 'input'); // user edits input 1
  263. check(s.forms.isDirty(i1), 'input event did not mark the element dirty');
  264. await setState('p', { one: 'y', two: 'y' });
  265. check(i1.value === 'x', 'dirty element was overwritten: ' + JSON.stringify(i1.value));
  266. check(i2.value === 'y', 'clean sibling not re-filled');
  267. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  268. } else if (scenario === '9') {
  269. // Dirtiness is per element.
  270. var f = el('form');
  271. var i1 = input('text', 'one'); i1.setAttribute('stm-prefill', 'p.v');
  272. var i2 = input('text', 'two'); i2.setAttribute('stm-prefill', 'p.v');
  273. f.appendChild(i1); f.appendChild(i2);
  274. body.appendChild(f);
  275. await setState('p', { v: 'a' });
  276. fire(i1, 'change');
  277. await setState('p', { v: 'b' });
  278. check(i1.value === 'a', 'dirty element overwritten');
  279. check(i2.value === 'b', 'clean element not filled');
  280. check(s.forms.isDirty(i1) && !s.forms.isDirty(i2), 'dirty flags wrong');
  281. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  282. } else if (scenario === '10') {
  283. // Reset restores defaults, un-dirties, and prefill re-applies.
  284. var f = el('form');
  285. var i1 = input('text', 'one', 'init'); i1.setAttribute('stm-prefill', 'p.v');
  286. f.appendChild(i1);
  287. body.appendChild(f);
  288. await setState('p', { v: 'state' });
  289. check(i1.value === 'state', 'prefill did not apply');
  290. fire(i1, 'input');
  291. check(s.forms.isDirty(i1), 'not dirty after edit');
  292. f.reset();
  293. check(i1.value === 'init', 'reset did not restore the attribute default: ' + JSON.stringify(i1.value));
  294. check(!s.forms.isDirty(i1), 'reset did not un-dirty');
  295. await setState('p', { v: 'zzz' });
  296. check(i1.value === 'zzz', 'prefill did not re-apply after reset');
  297. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  298. } else if (scenario === '11') {
  299. // Clear-on-success: action resolves -> form resets and un-dirties.
  300. var f = el('form');
  301. f.setAttribute('stm-action', 'p.act');
  302. f.setAttribute('stm-clear-on-success', '');
  303. var i1 = input('text', 'one', 'dflt'); i1.setAttribute('stm-prefill', 'p.v');
  304. f.appendChild(i1);
  305. body.appendChild(f);
  306. await setState('p', { v: 'typed', act: { uri: '/save', method: 'POST' } });
  307. check(i1.value === 'typed', 'prefill did not apply');
  308. fire(i1, 'input'); // user edits -> dirty; reset must still win via clear
  309. globalThis.__shim.setFetch(function () { return globalThis.__shim.okResponse([]); });
  310. fire(f, 'submit');
  311. await spin(50);
  312. check(globalThis.__shim.fetchCalls.some(function (c) { return c.url === '/save'; }),
  313. 'the action request never fired');
  314. check(i1.value === 'dflt', 'form not reset after success: ' + JSON.stringify(i1.value));
  315. check(!s.forms.isDirty(i1), 'form not un-dirtied after success');
  316. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  317. } else if (scenario === '12') {
  318. // Clear-on-success: failure keeps values and dirty state.
  319. var f = el('form');
  320. f.setAttribute('stm-action', 'p.act');
  321. f.setAttribute('stm-clear-on-success', '');
  322. var i1 = input('text', 'one', 'dflt'); i1.setAttribute('stm-prefill', 'p.v');
  323. f.appendChild(i1);
  324. body.appendChild(f);
  325. await setState('p', { v: 'typed', act: { uri: '/save', method: 'POST' } });
  326. fire(i1, 'input');
  327. i1.value = 'edited';
  328. globalThis.__shim.setFetch(function () { return globalThis.__shim.failResponse(500); });
  329. fire(f, 'submit');
  330. await spin(50);
  331. check(i1.value === 'edited', 'form was reset despite failure: ' + JSON.stringify(i1.value));
  332. check(s.forms.isDirty(i1), 'dirty state lost on failure');
  333. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  334. // Error-directive variant (statum response carrying an error directive).
  335. globalThis.__shim.setFetch(function () {
  336. return globalThis.__shim.okResponse([{ type: 'error', message: 'nope' }]);
  337. });
  338. fire(f, 'submit');
  339. await spin(50);
  340. check(i1.value === 'edited', 'form was reset after an error directive');
  341. // Core logs error directives by design; nothing else may error.
  342. check(log.errors.length === 1 && log.errors[0].indexOf('error directive') !== -1,
  343. 'unexpected console.error: ' + JSON.stringify(log.errors));
  344. log.errors.length = 0;
  345. } else if (scenario === '13') {
  346. // Keyed loop of forms: clearing one instance leaves the others intact.
  347. var holder = el('div');
  348. holder.setAttribute('stm-for-item-in', 'p.rows');
  349. holder.setAttribute('stm-key', 'item.id');
  350. var row = el('form');
  351. row.setAttribute('stm-action', 'item.act');
  352. row.setAttribute('stm-clear-on-success', '');
  353. var ri = input('text', 'val', 'blank');
  354. ri.setAttribute('stm-prefill', 'item.val');
  355. row.appendChild(ri);
  356. holder.appendChild(row);
  357. body.appendChild(holder);
  358. var mkAct = function (uri) { return { uri: uri, method: 'POST' }; };
  359. await setState('p', {
  360. rows: [
  361. { id: 1, val: 'one', act: mkAct('/save1') },
  362. { id: 2, val: 'two', act: mkAct('/save2') },
  363. { id: 3, val: 'three', act: mkAct('/save3') }
  364. ]
  365. });
  366. var forms = body.querySelectorAll('form').filter(function (n) {
  367. // The loop template lives inside the (hidden) holder; instances sit in
  368. // their own DIVs outside it.
  369. var p = n.parentNode;
  370. return p !== holder && p.tagName === 'DIV';
  371. });
  372. check(forms.length === 3, 'expected 3 instance forms, got ' + forms.length);
  373. check(forms[0].children[0].value === 'one', 'instance 1 prefill failed');
  374. // Dirty instance 2's input by editing it.
  375. var i2 = forms[1].children[0];
  376. i2.value = 'user2';
  377. fire(i2, 'input');
  378. // Instance 1 and 3 inputs are clean (state-filled).
  379. globalThis.__shim.setFetch(function () { return globalThis.__shim.okResponse([]); });
  380. fire(forms[1], 'submit');
  381. await spin(50);
  382. check(forms[1].children[0].value === 'blank', 'instance 2 not reset: ' + JSON.stringify(forms[1].children[0].value));
  383. check(!s.forms.isDirty(forms[1].children[0]), 'instance 2 not un-dirtied');
  384. check(forms[0].children[0].value === 'one', 'instance 1 was touched');
  385. check(forms[2].children[0].value === 'three', 'instance 3 was touched');
  386. var saved = globalThis.__shim.fetchCalls.filter(function (c) { return c.method === 'POST'; })
  387. .map(function (c) { return c.url; });
  388. check(saved.indexOf('/save2') !== -1 && saved.indexOf('/save1') === -1 && saved.indexOf('/save3') === -1,
  389. 'wrong action fired: ' + JSON.stringify(saved));
  390. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  391. } else if (scenario === '14') {
  392. // Two sibling forms: clear-on-success on A only.
  393. var fa = el('form');
  394. fa.setAttribute('stm-action', 'p.act');
  395. fa.setAttribute('stm-clear-on-success', '');
  396. var ia = input('text', 'a', 'dflt-a');
  397. ia.setAttribute('stm-prefill', 'p.va');
  398. fa.appendChild(ia);
  399. var fb = el('form');
  400. fb.setAttribute('stm-action', 'p.act');
  401. var ib = input('text', 'b', 'dflt-b');
  402. ib.setAttribute('stm-prefill', 'p.vb');
  403. fb.appendChild(ib);
  404. body.appendChild(fa); body.appendChild(fb);
  405. await setState('p', { va: 'A', vb: 'B', act: { uri: '/save', method: 'POST' } });
  406. fire(ib, 'input'); // B becomes dirty
  407. globalThis.__shim.setFetch(function () { return globalThis.__shim.okResponse([]); });
  408. fire(fa, 'submit');
  409. await spin(50);
  410. check(ia.value === 'dflt-a', 'form A not reset');
  411. check(!s.forms.isDirty(ia), 'form A not un-dirtied');
  412. check(ib.value === 'B', 'form B value was touched');
  413. check(s.forms.isDirty(ib), 'form B dirty state was touched');
  414. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  415. } else if (scenario === '15') {
  416. // statum.forms.reset resets exactly the given form.
  417. var fa = el('form');
  418. var ia = input('text', 'a', 'dflt-a');
  419. fa.appendChild(ia);
  420. var fb = el('form');
  421. var ib = input('text', 'b', 'dflt-b');
  422. fb.appendChild(ib);
  423. body.appendChild(fa); body.appendChild(fb);
  424. ia.value = 'x'; fire(ia, 'input');
  425. ib.value = 'y'; fire(ib, 'input');
  426. s.forms.reset(fa);
  427. check(ia.value === 'dflt-a', 'form A not reset');
  428. check(!s.forms.isDirty(ia), 'form A not un-dirtied');
  429. check(ib.value === 'y', 'form B was touched');
  430. check(s.forms.isDirty(ib), 'form B dirty state was touched');
  431. // Passing a control inside a form resolves the owning form.
  432. ib.value = 'z';
  433. s.forms.reset(ib);
  434. check(ib.value === 'dflt-b', 'forms.reset(el) did not resolve the owning form');
  435. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  436. } else if (scenario === '16') {
  437. // Handler order: stm-prefill first (property writes feed later predicates).
  438. var names = s._bridge.attributeHandlers.map(function (h) { return h.name; });
  439. check(names.indexOf('stm-prefill') === 0, 'stm-prefill is not first: ' + JSON.stringify(names));
  440. check(names.indexOf('stm-class.') === 1, 'stm-class. is not second: ' + JSON.stringify(names));
  441. check(names.indexOf('stm-markdown') === names.indexOf('stm-html') + 1, 'markdown ordering broke');
  442. check(names[names.length - 1] === 'stm-h-demotion', 'last handler changed: ' + JSON.stringify(names));
  443. check(log.warns.length === 0, 'warnings: ' + JSON.stringify(log.warns));
  444. check(log.errors.length === 0, 'errors: ' + JSON.stringify(log.errors));
  445. } else {
  446. fail('unknown scenario: ' + scenario);
  447. }
  448. check(log.errors.length === 0, 'unexpected console.error: ' + JSON.stringify(log.errors));
  449. print('scenario ' + scenario + ': OK');
  450. }
  451. main().catch(function (e) {
  452. std.err.puts('FAIL [' + scenario + '] ' + (e && e.stack || e) + '\n');
  453. std.exit(1);
  454. });