import { $, Fragment, Slot, _fnSignal, _jsxSorted, component$, useSignal, useStore, useTask$, type JSXChildren, type JSXOutput, } from '@qwik.dev/core'; import { vnode_fromJSX } from '@qwik.dev/core/testing'; import { describe, expect, it } from 'vitest'; import type { SignalImpl } from '../reactive-primitives/impl/signal-impl'; import { _hasStoreEffects, getOrCreateStore, getStoreHandler, } from '../reactive-primitives/impl/store'; import type { WrappedSignalImpl } from '../reactive-primitives/impl/wrapped-signal-impl'; import { createSignal } from '../reactive-primitives/signal-api'; import { StoreFlags } from '../reactive-primitives/types'; import { QError, qError } from '../shared/error/error'; import type { QElement } from '../shared/types'; import { DeleteOperation, InsertOrMoveOperation } from '../shared/vnode/types/dom-vnode-operation'; import { VNodeFlags } from './types'; import { vnode_getFirstChild, vnode_getNode, vnode_setProp, type VNodeJournal, } from './vnode-utils'; import { _flushJournal } from '../shared/cursor/cursor-flush'; import { markVNodeDirty } from '../shared/vnode/vnode-dirty'; import { ChoreBits } from '../shared/vnode/enums/chore-bits.enum'; import { NODE_DIFF_DATA_KEY } from '../shared/cursor/cursor-props'; import type { ElementVNode } from '../shared/vnode/element-vnode'; import type { VNode } from '../shared/vnode/vnode'; import { vnode_diff } from './vnode-diff'; describe('vNode-diff', () => { it('should find no difference', () => { const { vNode, vParent, container } = vnode_fromJSX(
Hello
); expect(vNode).toMatchVDOM(
Hello
); expect(vnode_getNode(vNode!)!.ownerDocument!.body.innerHTML).toEqual('
Hello
'); const journal: VNodeJournal = []; vnode_diff(container, journal,
Hello
, vParent, vParent, null); expect(journal.length).toEqual(0); }); describe('text', () => { it('should update text', () => { const { vNode, vParent, container } = vnode_fromJSX(
Hello
); const journal: VNodeJournal = []; vnode_diff(container, journal,
World
, vParent, vParent, null); expect(vNode).toMatchVDOM(
World
); expect(journal).not.toEqual([]); expect((vnode_getNode(vNode) as Element).outerHTML).toEqual('
Hello
'); _flushJournal(journal); expect((vnode_getNode(vNode) as Element).outerHTML).toEqual('
World
'); }); it('should add missing text node', () => { const { vNode, vParent, container } = vnode_fromJSX(
); const journal: VNodeJournal = []; vnode_diff(container, journal,
Hello
, vParent, vParent, null); expect(vNode).toMatchVDOM(
Hello
); expect((vnode_getNode(vNode) as Element).outerHTML).toEqual('
'); _flushJournal(journal); expect((vnode_getNode(vNode) as Element).outerHTML).toEqual('
Hello
'); }); it('should update and add missing text node', () => { const { vNode, vParent, container } = vnode_fromJSX(
text
); const journal: VNodeJournal = []; vnode_diff(container, journal,
Hello {'World'}
, vParent, vParent, null); _flushJournal(journal); expect(vNode).toMatchVDOM(
Hello {'World'}
); }); it('should remove extra text nodes', () => { const { vNode, vParent, container } = vnode_fromJSX(
text{'removeMe'}
); const journal: VNodeJournal = []; vnode_diff(container, journal,
Hello
, vParent, vParent, null); _flushJournal(journal); expect(vNode).toMatchVDOM(
Hello
); }); it('should remove all text nodes', () => { const { vNode, vParent, container } = vnode_fromJSX(
text{'removeMe'}
); const journal: VNodeJournal = []; vnode_diff(container, journal,
, vParent, vParent, null); _flushJournal(journal); expect(vNode).toMatchVDOM(
); }); it('should treat undefined as no children', () => { const { vNode, vParent, container } = vnode_fromJSX(
text{'removeMe'}
); const journal: VNodeJournal = []; vnode_diff(container, journal,
{undefined}
, vParent, vParent, null); _flushJournal(journal); expect(vNode).toMatchVDOM(
); }); describe('2 empty texts', () => { it('should correctly diff 2 empty texts with element node', () => { const { vNode, vParent, container } = vnode_fromJSX( _jsxSorted('div', null, null, ['', ''], 0, 'KA_0') ); const emptyFragment = (vNode as ElementVNode).firstChild?.nextSibling; expect(emptyFragment).toBeDefined(); const journal: VNodeJournal = []; vnode_diff( container, journal, _jsxSorted( 'div', null, null, [_jsxSorted('div', null, null, 'Child 1', 0, null), ''], 0, 'KA_0' ), vParent, vParent, null ); _flushJournal(journal); expect((vNode as ElementVNode).firstChild?.nextSibling === emptyFragment).toBeTruthy(); }); it('should correctly diff 2 empty texts with fragment node', () => { const { vNode, vParent, container } = vnode_fromJSX( _jsxSorted('div', null, null, ['', ''], 0, 'KA_0') ); const emptyFragment = (vNode as ElementVNode).firstChild?.nextSibling; expect(emptyFragment).toBeDefined(); const journal: VNodeJournal = []; vnode_diff( container, journal, _jsxSorted( 'div', null, null, [_jsxSorted(Fragment, null, null, 'Child 1', 0, null), ''], 0, 'KA_0' ), vParent, vParent, null ); _flushJournal(journal); expect((vNode as ElementVNode).firstChild?.nextSibling === emptyFragment).toBeTruthy(); }); it('should correctly diff 2 empty texts with slot node', () => { const { vNode, vParent, container } = vnode_fromJSX( _jsxSorted('div', null, null, ['', ''], 0, 'KA_0') ); const emptyFragment = (vNode as ElementVNode).firstChild?.nextSibling; expect(emptyFragment).toBeDefined(); const journal: VNodeJournal = []; vnode_diff( container, journal, _jsxSorted( 'div', null, null, [_jsxSorted(Slot, null, null, null, 0, null), ''], 0, 'KA_0' ), vParent, vParent, null ); _flushJournal(journal); expect((vNode as ElementVNode).firstChild?.nextSibling === emptyFragment).toBeTruthy(); }); it('should correctly diff 2 empty texts with component node', () => { const { vNode, vParent, container } = vnode_fromJSX( _jsxSorted('div', null, null, ['', ''], 0, 'KA_0') ); const emptyFragment = (vNode as ElementVNode).firstChild?.nextSibling; expect(emptyFragment).toBeDefined(); const journal: VNodeJournal = []; const Cmp = component$(() =>
Hello
); vnode_diff( container, journal, _jsxSorted( 'div', null, null, [_jsxSorted(Cmp as JSXChildren, null, null, [], 0, null), ''], 0, 'KA_0' ), vParent, vParent, null ); _flushJournal(journal); expect((vNode as ElementVNode).firstChild?.nextSibling === emptyFragment).toBeTruthy(); }); }); }); describe('element', () => { it('should do nothing on same', () => { const { vNode, vParent, container } = vnode_fromJSX( ); const test = ( ); const journal: VNodeJournal = []; vnode_diff(container, journal, test, vParent, vParent, null); expect(vNode).toMatchVDOM(test); expect(journal.length).toEqual(0); }); it('should add missing element', () => { const { vNode, vParent, container } = vnode_fromJSX(); const test = ( ); const journal: VNodeJournal = []; vnode_diff(container, journal, test, vParent, vParent, null); _flushJournal(journal); expect(vNode).toMatchVDOM(test); }); it('should remove extra text node', async () => { const { vNode, vParent, container } = vnode_fromJSX( {'before'} {'after'} ); const test = ( ); const journal: VNodeJournal = []; vnode_diff(container, journal, test, vParent, vParent, null); _flushJournal(journal); expect(vNode).toMatchVDOM(test); await expect(container.document.querySelector('test')).toMatchDOM(test); }); describe('keeping siblings in place', () => { const touchedNodes = (journal: VNodeJournal) => journal.flatMap((op) => op instanceof InsertOrMoveOperation || op instanceof DeleteOperation ? [op.target] : [] ); it('should not move later siblings when an earlier element becomes an empty text', async () => { const { vNode, vParent, container } = vnode_fromJSX(

Message