mzgoddard / preact-render-spy

Render preact components with access to the produced virtual dom for testing.

Home Page:https://www.npmjs.com/package/preact-render-spy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strange output for toString method while using preact-i18n

ingro opened this issue · comments

Hello! I was trying the latest rc.7 to port my snapshot tests from manually use preact-render-to-string to this library built-in solution.

One glitch I found is when I try to snapsnot an element which uses preact-i18n.

This is my component method:

export const Select = ({ name, value, options, htmlFor, onBlur, onChange }) => {
    return (
        <select
            name={name}
            value={value}
            onChange={onChange}
            onBlur={onBlur}
            id={htmlFor}
        >
            <option value=""><Text id="select">Select</Text></option>
            {options.map(({ value, label }, i) => <option key={i} value={value}>{label}</option>)}
        </select>
    );
}

This is the output of preact-render-to-string/jsx:

<select
    name="foo"
    id="foo"
>
    <option value="">Select</option>
    <option value={1}>bar</option>
    <option value={2}>baz</option>
</select>

While this is the output of preact-render-spy snapshot:

preact-render-spy (1 nodes)
    -------
    <select
      name="foo"
      id="foo"
    >
      <option value="">[object Object]</option>
      <option value={1}>bar</option>
      <option value={2}>baz</option>
    </select>

Notice how the <Text id="select">Select</Text> is render as [object Object] in the snapshot.

Not sure if that's a problem only of preact-i18n though.

Hrm... Can you maybe setup a reduced test case repo for me to start debugging from here? I'll have some time to dig in deeper later today, but it would be nice to not have to set up this scenario myself 😄

I have some thoughts about why... does preact-i18n use context/providers?

Also, was this with render spy's shallow or deep? I bet you it looks more like what you want on shallow

Can you try with import toString from 'preact-render-to-string/jsx' and toString(jsx, { shallow: true }) (we use the shallow option from render-to-string on our toString output)

aha, I found the specific issue! basically i assumed any truthy output from components was vnodes, but it can also be a string - which was then made into an invalid vnode

Sorry didn't had time to create a test repo! Glad you already found the problem :), thanks for the fast fix!

I noticed that preact-i18n doesn't have display names for it's components either... so added synacor/preact-i18n#4

That text nodes fix is published in 1.0.0-rc.8

Also, just as a suggestion - you might want to just start using shallow as your default for testing (unless you're really depending on children components to do something) .

Let me know for sure that this patch fixed your string node issue tho :) (I'm pretty confident it did)