Skip to content

Commit

Permalink
feat: serialize Wrapper Instances (#4)
Browse files Browse the repository at this point in the history
breaking change
  • Loading branch information
dvdzkwsk authored and eddyerburgh committed May 24, 2018
1 parent a7813cc commit 3e8dec0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
15 changes: 11 additions & 4 deletions index.js
@@ -1,11 +1,18 @@
const beautify = require('pretty')

const isHtmlString = received => typeof received === 'string' && received[0] === '<'
const isVueWrapper = received => (
typeof received === 'object' &&
typeof received.isVueInstance === 'function'
)

module.exports = {
test (object) {
return typeof object === 'string' && object[0] === '<'
test (received) {
return isHtmlString(received) || isVueWrapper(received)
},
print (val) {
const removedServerRenderedText = val.replace(/ data-server-rendered="true"/, '')
print (received) {
const html = isVueWrapper(received) ? received.html() : received
const removedServerRenderedText = html.replace(/ data-server-rendered="true"/, '')
return beautify(removedServerRenderedText, { indent_size: 2 })
}
}
5 changes: 5 additions & 0 deletions jest.config.json
@@ -0,0 +1,5 @@
{
"snapshotSerializers": [
"<rootDir>/index.js"
]
}
10 changes: 10 additions & 0 deletions test/Parent.spec.js
Expand Up @@ -12,4 +12,14 @@ describe('Parent.vue', () => {
const wrapper = shallow(Parent)
expect(wrapper.html()).toMatchSnapshot()
})

it('properly serializes a shallowly-rendered wrapper', () => {
const wrapper = shallow(Parent)
expect(wrapper).toMatchSnapshot()
})

it('properly serializes a fully-mounted wrapper', () => {
const wrapper = mount(Parent)
expect(wrapper).toMatchSnapshot()
})
})
22 changes: 22 additions & 0 deletions test/__snapshots__/Parent.spec.js.snap
Expand Up @@ -33,6 +33,28 @@ exports[`Parent.vue mount snapshot 2`] = `
</div>
`;

exports[`Parent.vue properly serializes a fully-mounted wrapper 1`] = `
<div>
<h1>parent</h1>
<main>
<p>this is a child component</p>
<p>this is a child component</p>
<p>this is a child component</p>
</main>
</div>
`;

exports[`Parent.vue properly serializes a shallowly-rendered wrapper 1`] = `
<div>
<h1>parent</h1>
<main>
<!---->
<!---->
<!---->
</main>
</div>
`;

exports[`Parent.vue shallow snapshot 1`] = `
<div>
<h1>parent</h1>
Expand Down

0 comments on commit 3e8dec0

Please sign in to comment.