import { AttrDeserialize, Component, h, Prop } from '@stencil/core'; import { newSpecPage } from '@stencil/core/testing'; function Clamp(lowerBound: number, upperBound: number): any { const clamp = (value: number) => Math.max(lowerBound, Math.min(value, upperBound)); return () => { const key = Symbol(); return { get() { return this[key]; }, set(newValue: number) { this[key] = clamp(newValue); }, configurable: true, enumerable: true, }; }; } describe('prop', () => { it('"value" attribute', async () => { @Component({ tag: 'cmp-a' }) class CmpA { @Prop() value: string; render() { return {this.value.trim()}; } } const { root } = await newSpecPage({ components: [CmpA], html: ``, }); expect(root).toEqualHtml(` #005a00 `); }); it('override default values from attribute', async () => { @Component({ tag: 'cmp-a' }) class CmpA { @Prop() boolFalse = false; @Prop() boolTrue = true; @Prop() str = 'string'; @Prop() num = 88; private _accessor = 'accessor'; @Prop() get accessor() { return this._accessor; } set accessor(newVal) { this._accessor = newVal; } @Clamp(0, 10) @Prop() clamped = 5; render() { return `${this.boolFalse}-${this.boolTrue}-${this.str}-${this.num}-${this.accessor}-${this.clamped}`; } } const { root } = await newSpecPage({ components: [CmpA], html: ``, }); expect(root).toEqualHtml(` true-false-attr-99-accessed!-10 `); expect(root.textContent).toBe('true-false-attr-99-accessed!-10'); expect(root.boolFalse).toBe(true); expect(root.boolTrue).toBe(false); expect(root.str).toBe('attr'); expect(root.num).toBe(99); expect(root.clamped).toBe(10); expect(root.accessor).toBe('accessed!'); }); it('set default values', async () => { @Component({ tag: 'cmp-a' }) class CmpA { @Prop() boolFalse = false; @Prop() boolTrue = true; @Prop() str = 'string'; @Prop() num = 88; private _accessor = 'accessor'; @Prop() get accessor() { return this._accessor; } set accessor(newVal) { this._accessor = newVal; } @Clamp(0, 5) @Prop() clamped = 11; render() { return `${this.boolFalse}-${this.boolTrue}-${this.str}-${this.num}-${this.accessor}-${this.clamped}`; } } const { root } = await newSpecPage({ components: [CmpA], html: ``, }); expect(root).toEqualHtml(` false-true-string-88-accessor-5 `); expect(root.textContent).toBe('false-true-string-88-accessor-5'); expect(root.boolFalse).toBe(false); expect(root.boolTrue).toBe(true); expect(root.str).toBe('string'); expect(root.num).toBe(88); expect(root.accessor).toBe('accessor'); expect(root.clamped).toBe(5); }); it('should call componentShouldUpdate for each prop when multiple props change synchronously', async () => { const shouldUpdateCalls: Array<{ value: any; old: any; prop: string }> = []; @Component({ tag: 'cmp-a' }) class CmpA { @Prop({ mutable: true }) first = 'initial-first'; @Prop({ mutable: true }) second = 'initial-second'; @Prop({ mutable: true }) third = 'initial-third'; componentShouldUpdate(value: any, old: any, prop: string) { shouldUpdateCalls.push({ value, old, prop }); } render() { return `${this.first}-${this.second}-${this.third}`; } } const { root, waitForChanges } = await newSpecPage({ components: [CmpA], html: ``, }); expect(root).toEqualHtml(` initial-first-initial-second-initial-third `); expect(shouldUpdateCalls).toHaveLength(0); // Update all three props synchronously root.first = 'new-first'; root.second = 'new-second'; root.third = 'new-third'; await waitForChanges(); // componentShouldUpdate should have been called for each prop expect(shouldUpdateCalls).toHaveLength(3); expect(shouldUpdateCalls[0]).toEqual({ value: 'new-first', old: 'initial-first', prop: 'first' }); expect(shouldUpdateCalls[1]).toEqual({ value: 'new-second', old: 'initial-second', prop: 'second' }); expect(shouldUpdateCalls[2]).toEqual({ value: 'new-third', old: 'initial-third', prop: 'third' }); // All values should be rendered expect(root).toEqualHtml(` new-first-new-second-new-third `); }); it('only update on even numbers', async () => { @Component({ tag: 'cmp-a' }) class CmpA { @Prop() num = 1; componentShouldUpdate(newValue: number, _: number, propName: string) { if (propName === 'num') { return newValue % 2 === 0; } return true; } render() { return `${this.num}`; } } const { root, waitForChanges } = await newSpecPage({ components: [CmpA], html: ``, }); expect(root).toEqualHtml(` 1 `); root.num++; await waitForChanges(); expect(root).toEqualHtml(` 2 `); root.num++; await waitForChanges(); expect(root).toEqualHtml(` 2 `); root.num++; await waitForChanges(); expect(root).toEqualHtml(` 4 `); }); it('only updates on even numbers via a setter', async () => { @Component({ tag: 'cmp-a' }) class CmpA { private _num = 1; @Prop() get num() { return this._num; } set num(newValue: number) { if (newValue % 2 === 0) this._num = newValue; } render() { return `${this.num}`; } } const { root, waitForChanges } = await newSpecPage({ components: [CmpA], html: ``, }); expect(root).toEqualHtml(` 1 `); root.num = 2; await waitForChanges(); expect(root).toEqualHtml(` 2 `); root.num = 3; await waitForChanges(); expect(root).toEqualHtml(` 2 `); root.num = 4; await waitForChanges(); expect(root).toEqualHtml(` 4 `); }); it('should demonstrate JSON parsing for complex object props', async () => { @Component({ tag: 'simple-demo' }) class SimpleDemo { @Prop() message: { text: string } = { text: 'default' }; @Prop() messageAny: any = { text: 'default' }; @AttrDeserialize('message') @AttrDeserialize('messageAny') parseMessage(newValue: string) { return JSON.parse(newValue); } render() { return (
{this.message.text}
{this.messageAny.text}
); } } const { root } = await newSpecPage({ components: [SimpleDemo], html: ``, }); expect(root).toEqualHtml(`
Hello World
Hello World
`); }); it('updates the internal input when the prop value changes between distinct falsy states', async () => { @Component({ tag: 'cmp-input-wrapper' }) class CmpA { @Prop() value: any; render() { return ; } } const { root, waitForChanges } = await newSpecPage({ components: [CmpA], html: ``, }); root.value = 0; await waitForChanges(); expect(root.querySelector('#internal-input').value).toBe('0'); root.value = ''; await waitForChanges(); expect(root.querySelector('#internal-input').value).toBe(''); root.value = 0; await waitForChanges(); expect(root.querySelector('#internal-input').value).toBe('0'); }); it('should maintain reactivity when prop is set to undefined on element before initialization', async () => { @Component({ tag: 'cmp-lazy-prop' }) class CmpLazyProp { @Prop() first?: string; render() { return
{this.first ?? 'empty'}
; } } // Create page without flushing queue - element exists but component not yet initialized const page = await newSpecPage({ components: [CmpLazyProp], html: ``, flushQueue: false, }); // Set property to undefined on element BEFORE component initializes // This creates an "own property" that shadows the getter/setter (page.body.querySelector('cmp-lazy-prop') as any).first = undefined; // Now initialize the component await page.waitForChanges(); const root = page.root; expect(root).toEqualHtml(`
empty
`); // Update the prop - this should work because lazy property handling // correctly cleaned up the shadowing own property root.first = 'updated'; await page.waitForChanges(); expect(root).toEqualHtml(`
updated
`); expect(root.first).toBe('updated'); }); it('should maintain reactivity when prop is set to null on element before initialization', async () => { @Component({ tag: 'cmp-lazy-prop-null' }) class CmpLazyPropNull { @Prop() value?: string; render() { return
{this.value ?? 'empty'}
; } } const page = await newSpecPage({ components: [CmpLazyPropNull], html: ``, flushQueue: false, }); (page.body.querySelector('cmp-lazy-prop-null') as any).value = null; await page.waitForChanges(); const root = page.root; // null is a valid value, should render 'empty' due to nullish coalescing expect(root).toEqualHtml(`
empty
`); root.value = 'test'; await page.waitForChanges(); expect(root).toEqualHtml(`
test
`); }); });