You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're currently working on creating unit tests and as a starter, we're trying to snapshot the components using renderToString with shallow set to true.
The problem is that a object passed as parameter to a child component will be rendered as string "[object Object]", simplified example:
As far as I can trace it, it's because the default JS serializing is used for a simple JS object (done in src/util.js -> encodeEntities). We're using typescript and got the parameter typed, but that doesn't help us because I don't think you can override the toString method for that one. Changing everything to classes / functions and override the toString is really a no-go because it adds tons of extra code. Any way provide a callback or something that objects are serialized with (for example) JSON.stringify so we get something like:
Note: I really want to test using the renderToString function and not enzyme or any other jest snapshot serializer, because that just doesn't render the components in exactly the same way this module does.
The text was updated successfully, but these errors were encountered:
It would be nice if we have a bit more control over that, so maybe we could pass in a function to the options which will be used instead of the default toString function for the object.
We could change those lines into something like:
if (opts.shallow
&& typeof v === 'object'
&& typeof opts.shallowObjectsToString === 'function'
) {
s = s + ` ${name}="${encodeEntities(opts.shallowObjectsToString(name, v) +'')}"`;
} else {
s = s + ` ${name}="${encodeEntities(v + '')}"`;
}
And we can pass in an option 'shallowObjectsToString' to do whatever we want with it, for example:
We're currently working on creating unit tests and as a starter, we're trying to snapshot the components using renderToString with shallow set to true.
The problem is that a object passed as parameter to a child component will be rendered as string "[object Object]", simplified example:
export default function MyComponent { return (<WrapperComponent param1={{ test: '1234' }} />); }
generates ==>
<WrapperComponent param1="[object Object]" />
As far as I can trace it, it's because the default JS serializing is used for a simple JS object (done in src/util.js -> encodeEntities). We're using typescript and got the parameter typed, but that doesn't help us because I don't think you can override the toString method for that one. Changing everything to classes / functions and override the toString is really a no-go because it adds tons of extra code. Any way provide a callback or something that objects are serialized with (for example)
JSON.stringify
so we get something like:<WrapperComponent param1="{"test":"1234"}" />
Note: I really want to test using the renderToString function and not enzyme or any other jest snapshot serializer, because that just doesn't render the components in exactly the same way this module does.
The text was updated successfully, but these errors were encountered: