Skip to content

Wraps rendered component in a div #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -50,7 +50,15 @@ function render (TestComponent, {

mountedWrappers.add(wrapper)

if (wrapper.element.parentNode === document.body) {
const div = document.createElement('div')
wrapper.element.parentNode.insertBefore(div, wrapper.element)
div.appendChild(wrapper.element)
}

return {
container: wrapper.element.parentNode,
baseElement: document.body,
debug: () => console.log(prettyDOM(wrapper.element)),
unmount: () => wrapper.destroy(),
isUnmounted: () => wrapper.vm._isDestroyed,
@@ -61,7 +69,7 @@ function render (TestComponent, {
return wait()
},
updateState: _ => wrapper.setData(_),
...getQueriesForElement(wrapper.element)
...getQueriesForElement(wrapper.element.parentNode)
}
}

@@ -70,8 +78,8 @@ function cleanup () {
}

function cleanupAtWrapper (wrapper) {
if (wrapper.parentNode === document.body) {
document.body.removeChild(wrapper)
if (wrapper.element.parentNode && wrapper.element.parentNode.parentNode === document.body) {
document.body.removeChild(wrapper.element.parentNode)
}
wrapper.destroy()
mountedWrappers.delete(wrapper)
31 changes: 31 additions & 0 deletions tests/__tests__/components/Button.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<template>
<button :class="typeClass" @click="clicked">{{ text }}</button>
</template>

<script>
export default {
props: {
text: {
type: String,
default: '',
},
clicked: {
type: Function,
default: () => true
},
type: {
validator: (value) => ['primary', 'secondary'].includes(value),
},
},
computed: {
typeClass: function() {
if (this.type) {
return `button button--${this.type}`;
}
return 'button';
},
},
};
</script>
23 changes: 23 additions & 0 deletions tests/__tests__/simple-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { render, cleanup, fireEvent } from '../../src';
import SimpleButton from './components/Button';

afterEach(cleanup)

test('renders button with text', () => {
const buttonText = "Click me; I'm sick"
const { getByText } = render(SimpleButton, {
props: { text: buttonText, clicked: () => true }
})

getByText(buttonText)
})

test('clicked prop is called when button is clicked', () => {
const clicked = jest.fn()
const text = 'Click me'
const { getByText } = render(SimpleButton, {
props: { text, clicked }
})
fireEvent.click(getByText(text))
expect(clicked).toBeCalled()
})