|
| 1 | +import { Component, h, Prop } from '@stencil/core'; |
| 2 | +import { newSpecPage } from '@stencil/core/testing'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Regression tests for: |
| 6 | + * - #6368: input/textarea values containing JSON should not be coerced to objects during change/assignment |
| 7 | + * - #6380: prop typed as a union (e.g. string | number) must not parse a valid JSON string into an object |
| 8 | + */ |
| 9 | + |
| 10 | +describe('regression: do not parse JSON strings into objects', () => { |
| 11 | + it('does not parse JSON when assigning to a union prop (string | number)', async () => { |
| 12 | + @Component({ tag: 'cmp-union' }) |
| 13 | + class CmpUnion { |
| 14 | + @Prop() value!: string | number; |
| 15 | + render() { |
| 16 | + return <div>{typeof this.value}:{String(this.value)}</div>; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + const json = '{"text":"Hello"}'; |
| 21 | + |
| 22 | + const page = await newSpecPage({ |
| 23 | + components: [CmpUnion], |
| 24 | + html: `<cmp-union value='${json}'></cmp-union>`, |
| 25 | + }); |
| 26 | + |
| 27 | + // Expect the prop to remain a string and not be parsed to an object |
| 28 | + expect(page.root?.textContent).toBe(`string:${json}`); |
| 29 | + }); |
| 30 | + |
| 31 | + it('does not parse JSON when assigning to a union prop (string | boolean)', async () => { |
| 32 | + @Component({ tag: 'cmp-union-bool' }) |
| 33 | + class CmpUnionBool { |
| 34 | + @Prop() value!: string | boolean; |
| 35 | + render() { |
| 36 | + return <div>{typeof this.value}:{String(this.value)}</div>; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + const json = '{"active":true}'; |
| 41 | + |
| 42 | + const page = await newSpecPage({ |
| 43 | + components: [CmpUnionBool], |
| 44 | + html: `<cmp-union-bool value='${json}'></cmp-union-bool>`, |
| 45 | + }); |
| 46 | + |
| 47 | + expect(page.root?.textContent).toBe(`string:${json}`); |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not parse JSON from an <input> value propagated to a mutable string prop', async () => { |
| 51 | + @Component({ tag: 'cmp-input-bind' }) |
| 52 | + class CmpInputBind { |
| 53 | + // emulates how frameworks pass raw input values to components |
| 54 | + @Prop({ mutable: true, reflect: true }) value: string = ''; |
| 55 | + |
| 56 | + private onInput = (ev: Event) => { |
| 57 | + const target = ev.target as HTMLInputElement; |
| 58 | + this.value = target.value; // assigning raw value must not parse JSON |
| 59 | + }; |
| 60 | + |
| 61 | + render() { |
| 62 | + return ( |
| 63 | + <div> |
| 64 | + <input value={this.value} onInput={this.onInput} /> |
| 65 | + <span id="out">{typeof this.value}:{this.value}</span> |
| 66 | + </div> |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + const page = await newSpecPage({ |
| 72 | + components: [CmpInputBind], |
| 73 | + html: `<cmp-input-bind></cmp-input-bind>`, |
| 74 | + }); |
| 75 | + |
| 76 | + const input = page.root!.querySelector('input')! as HTMLInputElement; |
| 77 | + const json = '{"a":1}'; |
| 78 | + |
| 79 | + // simulate user typing JSON into the input |
| 80 | + input.value = json; |
| 81 | + // Use a standard 'input' Event to mirror how other hydration tests trigger input handlers |
| 82 | + input.dispatchEvent(new Event('input', { bubbles: true })); |
| 83 | + await page.waitForChanges(); |
| 84 | + |
| 85 | + const out = page.root!.querySelector('#out')! as HTMLSpanElement; |
| 86 | + expect(out.textContent).toBe(`string:${json}`); |
| 87 | + }); |
| 88 | +}); |
0 commit comments