-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial documentation with example of Output mapping feature
- Loading branch information
Showing
1 changed file
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,38 @@ | ||
# Output mapping feature | ||
|
||
You can pass a function as the `map` option to have full control over your snapshot output. | ||
You can pass a function as the `map` option to have full control over your snapshot output. The function will traverse each node of your _wrapper_ tree recursively. | ||
|
||
* Documentation coming soon * | ||
## Example | ||
|
||
Use `map` to remove `containerInfo` prop from `Portal` components to avoid brittle snapshots. | ||
|
||
```javascript | ||
const removePortalContainerInfo = json => { | ||
if (json.type === "Portal") { | ||
return { | ||
...json, | ||
props: { | ||
...json.props, | ||
containerInfo: undefined | ||
} | ||
}; | ||
} | ||
return json; | ||
}; | ||
|
||
// Mount a component that might render a portal | ||
const wrapper = mount(MyComponent); | ||
|
||
// Then in your matcher | ||
|
||
expect( | ||
toJson(wrapper, { | ||
mode: "deep", | ||
map: wrapper.exists("Portal") && removePortalContainerInfo | ||
}) | ||
).toMatchSpecificSnapshot(snapshotFilename); | ||
``` | ||
|
||
Now these snapshots are solid. | ||
|
||
This example was extracted from a project using Material UI and the snapshot is actually a StoryShot. Before using the `map` function `containerInfo` was randomly set with body and spans apparently coming from Storybook markup. |