| 
 | 1 | +import { describe, expect, it } from 'vitest';  | 
 | 2 | +import { NEEDS_COMPUTATION, EffectProperty } from '../../reactive-primitives/types';  | 
 | 3 | +import { WrappedSignalImpl } from '../../reactive-primitives/impl/wrapped-signal-impl';  | 
 | 4 | +import { VNodeFlags } from '../../client/types';  | 
 | 5 | +import { ElementVNode, VirtualVNode } from '../../client/vnode-impl';  | 
 | 6 | +import { inflateWrappedSignalValue } from './inflate';  | 
 | 7 | + | 
 | 8 | +describe('inflateWrappedSignalValue', () => {  | 
 | 9 | +  it('should read value from class attribute', () => {  | 
 | 10 | +    const signal = new WrappedSignalImpl(  | 
 | 11 | +      null,  | 
 | 12 | +      (val: boolean) => (val ? 'active' : 'inactive'),  | 
 | 13 | +      [true],  | 
 | 14 | +      null  | 
 | 15 | +    );  | 
 | 16 | +    signal.$untrackedValue$ = NEEDS_COMPUTATION;  | 
 | 17 | + | 
 | 18 | +    const element = {  | 
 | 19 | +      attributes: [{ name: 'class', value: 'active' }],  | 
 | 20 | +    } as any;  | 
 | 21 | + | 
 | 22 | +    const vnode = new ElementVNode(  | 
 | 23 | +      VNodeFlags.Element,  | 
 | 24 | +      null, // parent  | 
 | 25 | +      null, // previousSibling  | 
 | 26 | +      null, // nextSibling  | 
 | 27 | +      null, // firstChild  | 
 | 28 | +      null, // lastChild  | 
 | 29 | +      element,  | 
 | 30 | +      'div'  | 
 | 31 | +    );  | 
 | 32 | +    vnode.setAttr('class', 'active', null);  | 
 | 33 | + | 
 | 34 | +    signal.$hostElement$ = vnode;  | 
 | 35 | + | 
 | 36 | +    signal.$effects$ = new Set([  | 
 | 37 | +      [vnode, 'class', null, null], // EffectSubscription: [consumer, property, backRefs, data]  | 
 | 38 | +    ] as any);  | 
 | 39 | + | 
 | 40 | +    inflateWrappedSignalValue(signal);  | 
 | 41 | + | 
 | 42 | +    expect(signal.$untrackedValue$).toBe('active');  | 
 | 43 | +    expect(signal.$untrackedValue$).not.toBe(NEEDS_COMPUTATION);  | 
 | 44 | +  });  | 
 | 45 | + | 
 | 46 | +  it('should read value from data-* attribute', () => {  | 
 | 47 | +    const signal = new WrappedSignalImpl(null, (val: string) => val, ['initial'], null);  | 
 | 48 | +    signal.$untrackedValue$ = NEEDS_COMPUTATION;  | 
 | 49 | + | 
 | 50 | +    const element = {  | 
 | 51 | +      attributes: [{ name: 'data-state', value: 'initial' }],  | 
 | 52 | +    } as any;  | 
 | 53 | + | 
 | 54 | +    const vnode = new ElementVNode(  | 
 | 55 | +      VNodeFlags.Element,  | 
 | 56 | +      null,  | 
 | 57 | +      null,  | 
 | 58 | +      null,  | 
 | 59 | +      null,  | 
 | 60 | +      null,  | 
 | 61 | +      element,  | 
 | 62 | +      'div'  | 
 | 63 | +    );  | 
 | 64 | +    vnode.setAttr('data-state', 'initial', null);  | 
 | 65 | + | 
 | 66 | +    signal.$hostElement$ = vnode;  | 
 | 67 | +    signal.$effects$ = new Set([[vnode, 'data-state', null, null]] as any);  | 
 | 68 | + | 
 | 69 | +    inflateWrappedSignalValue(signal);  | 
 | 70 | + | 
 | 71 | +    expect(signal.$untrackedValue$).toBe('initial');  | 
 | 72 | +  });  | 
 | 73 | + | 
 | 74 | +  it('should skip non-string effect keys (EffectProperty enum)', () => {  | 
 | 75 | +    const signal = new WrappedSignalImpl(null, () => 'computed', [], null);  | 
 | 76 | +    signal.$untrackedValue$ = NEEDS_COMPUTATION;  | 
 | 77 | + | 
 | 78 | +    const element = {  | 
 | 79 | +      attributes: [],  | 
 | 80 | +    } as any;  | 
 | 81 | + | 
 | 82 | +    const vnode = new ElementVNode(  | 
 | 83 | +      VNodeFlags.Element,  | 
 | 84 | +      null,  | 
 | 85 | +      null,  | 
 | 86 | +      null,  | 
 | 87 | +      null,  | 
 | 88 | +      null,  | 
 | 89 | +      element,  | 
 | 90 | +      'div'  | 
 | 91 | +    );  | 
 | 92 | + | 
 | 93 | +    signal.$hostElement$ = vnode;  | 
 | 94 | +    signal.$effects$ = new Set([[vnode, EffectProperty.VNODE, null, null]] as any);  | 
 | 95 | + | 
 | 96 | +    inflateWrappedSignalValue(signal);  | 
 | 97 | + | 
 | 98 | +    expect(signal.$untrackedValue$).toBe(NEEDS_COMPUTATION);  | 
 | 99 | +  });  | 
 | 100 | + | 
 | 101 | +  it('should not set value when attribute is null', () => {  | 
 | 102 | +    const signal = new WrappedSignalImpl(null, () => 'computed', [], null);  | 
 | 103 | +    signal.$untrackedValue$ = NEEDS_COMPUTATION;  | 
 | 104 | + | 
 | 105 | +    const element = {  | 
 | 106 | +      attributes: [],  | 
 | 107 | +    } as any;  | 
 | 108 | + | 
 | 109 | +    const vnode = new ElementVNode(  | 
 | 110 | +      VNodeFlags.Element,  | 
 | 111 | +      null,  | 
 | 112 | +      null,  | 
 | 113 | +      null,  | 
 | 114 | +      null,  | 
 | 115 | +      null,  | 
 | 116 | +      element,  | 
 | 117 | +      'div'  | 
 | 118 | +    );  | 
 | 119 | + | 
 | 120 | +    signal.$hostElement$ = vnode;  | 
 | 121 | +    signal.$effects$ = new Set([[vnode, 'missing-attr', null, null]] as any);  | 
 | 122 | + | 
 | 123 | +    inflateWrappedSignalValue(signal);  | 
 | 124 | + | 
 | 125 | +    expect(signal.$untrackedValue$).toBe(NEEDS_COMPUTATION);  | 
 | 126 | +  });  | 
 | 127 | + | 
 | 128 | +  it('should take first non-null attribute when multiple effects exist', () => {  | 
 | 129 | +    const signal = new WrappedSignalImpl(null, () => 'computed', [], null);  | 
 | 130 | +    signal.$untrackedValue$ = NEEDS_COMPUTATION;  | 
 | 131 | + | 
 | 132 | +    const element = {  | 
 | 133 | +      attributes: [  | 
 | 134 | +        { name: 'first-attr', value: 'first-value' },  | 
 | 135 | +        { name: 'second-attr', value: 'second-value' },  | 
 | 136 | +      ],  | 
 | 137 | +    } as unknown as HTMLElement;  | 
 | 138 | + | 
 | 139 | +    const vnode = new ElementVNode(  | 
 | 140 | +      VNodeFlags.Element,  | 
 | 141 | +      null,  | 
 | 142 | +      null,  | 
 | 143 | +      null,  | 
 | 144 | +      null,  | 
 | 145 | +      null,  | 
 | 146 | +      element,  | 
 | 147 | +      'div'  | 
 | 148 | +    );  | 
 | 149 | +    vnode.setAttr('first-attr', 'first-value', null);  | 
 | 150 | +    vnode.setAttr('second-attr', 'second-value', null);  | 
 | 151 | + | 
 | 152 | +    signal.$hostElement$ = vnode;  | 
 | 153 | +    signal.$effects$ = new Set([  | 
 | 154 | +      [vnode, 'first-attr', null, null],  | 
 | 155 | +      [vnode, 'second-attr', null, null],  | 
 | 156 | +    ]);  | 
 | 157 | + | 
 | 158 | +    inflateWrappedSignalValue(signal);  | 
 | 159 | + | 
 | 160 | +    expect(signal.$untrackedValue$).toBe('first-value');  | 
 | 161 | +  });  | 
 | 162 | + | 
 | 163 | +  it('should read from text node when used in text content (not attributes)', () => {  | 
 | 164 | +    const signal = new WrappedSignalImpl(null, () => 'hello', [], null);  | 
 | 165 | +    signal.$untrackedValue$ = NEEDS_COMPUTATION;  | 
 | 166 | + | 
 | 167 | +    const textNode = { nodeValue: 'hello' } as any;  | 
 | 168 | +    const textVNode = { flags: VNodeFlags.Text, textNode } as any;  | 
 | 169 | + | 
 | 170 | +    const vnode = new VirtualVNode(VNodeFlags.Virtual, null, null, null, textVNode, textVNode);  | 
 | 171 | + | 
 | 172 | +    signal.$hostElement$ = vnode;  | 
 | 173 | +    // No attribute effects, only VNODE effect  | 
 | 174 | +    signal.$effects$ = new Set([[vnode, EffectProperty.VNODE, null, null]] as any);  | 
 | 175 | + | 
 | 176 | +    inflateWrappedSignalValue(signal);  | 
 | 177 | + | 
 | 178 | +    expect(signal.$untrackedValue$).toBe('hello');  | 
 | 179 | +  });  | 
 | 180 | + | 
 | 181 | +  it('should prefer attribute over text node when both exist', () => {  | 
 | 182 | +    const signal = new WrappedSignalImpl(null, () => 'value', [], null);  | 
 | 183 | +    signal.$untrackedValue$ = NEEDS_COMPUTATION;  | 
 | 184 | + | 
 | 185 | +    // Element with both attribute and text child  | 
 | 186 | +    const textNode = { nodeValue: 'text-value' } as any;  | 
 | 187 | +    const textVNode = { flags: VNodeFlags.Text, textNode } as any;  | 
 | 188 | +    const element = {  | 
 | 189 | +      attributes: [{ name: 'data-value', value: 'attr-value' }],  | 
 | 190 | +      childNodes: [textNode],  | 
 | 191 | +    } as any;  | 
 | 192 | + | 
 | 193 | +    const vnode = new ElementVNode(  | 
 | 194 | +      VNodeFlags.Element,  | 
 | 195 | +      null,  | 
 | 196 | +      null,  | 
 | 197 | +      null,  | 
 | 198 | +      textVNode,  | 
 | 199 | +      textVNode,  | 
 | 200 | +      element,  | 
 | 201 | +      'div'  | 
 | 202 | +    );  | 
 | 203 | +    vnode.setAttr('data-value', 'attr-value', null);  | 
 | 204 | + | 
 | 205 | +    signal.$hostElement$ = vnode;  | 
 | 206 | +    signal.$effects$ = new Set([[vnode, 'data-value', null, null]] as any);  | 
 | 207 | + | 
 | 208 | +    inflateWrappedSignalValue(signal);  | 
 | 209 | + | 
 | 210 | +    // Should prefer attribute  | 
 | 211 | +    expect(signal.$untrackedValue$).toBe('attr-value');  | 
 | 212 | +  });  | 
 | 213 | +});  | 
0 commit comments