Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
ADD: coverage for VueWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyer committed Oct 21, 2019
1 parent 631537d commit 81c974d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
24 changes: 24 additions & 0 deletions tests/fixtures/VueSingleFileComponentChanged.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<div>
<span>{{displayname}}</span>
</div>
</template>

<script>
export default {
name: 'what2',
data(){
return {
displayname: 'VueSingleFileComponentChanged'
}
},
mounted(){
console.log(this.message);
},
methods: {
mayLog(e) {
console.log(this.displayname);
}
}
}
</script>
57 changes: 37 additions & 20 deletions tests/wrappers/VueWrapper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import { VueWrapper } from '../../src'
import VueComponent from '../fixtures/VueComponent'
import VueRegisteredComponent from '../fixtures/VueRegisteredComponent'
import VueSingleFileComponent from '../fixtures/VueSingleFileComponent.vue'
import VueSingleFileComponentChanged from '../fixtures/VueSingleFileComponentChanged.vue'

const mockReset = () => { return jest.fn() }
const mockReset = () => {
return jest.fn()
}
const makeReactInstanceWithVueComponent = (passedComponent, events) => {
class ReactApp extends React.Component {
constructor (props) {
super(props)
this.state = {
message: props.message,
component: passedComponent,
}
this.mockReset = mockReset()
}
Expand All @@ -23,14 +27,10 @@ const makeReactInstanceWithVueComponent = (passedComponent, events) => {
render () {
return (
<div>
<input
type='text'
value={this.state.message}
onChange={this.onChange}
/>
<input type='text' value={this.state.message} onChange={this.onChange} />
<VueWrapper
ref={ref => (this.vueWrapperRef = ref)}
component={passedComponent}
component={this.state.component}
on={events}
message={this.state.message}
reset={this.mockReset}
Expand Down Expand Up @@ -109,6 +109,32 @@ describe('VueInReact', () => {
)
})

/**
* NOTE: this case only occurs when using a wrapper component
* because the component filed is passed in curly braces of JSX,
* and it can be changed in `setSate` operation.
* However it won't occur in HOC mode or babel plugin mode.
* this test will fail. I'm trying to fixed it.
*/
it('dynamic change component while using a wrapper component', () => {
const reactAppInstance = makeReactInstanceWithVueComponent(VueSingleFileComponent)
expect(document.body.innerHTML).toBe(
normalizeHTMLString(
`<div id="root">
<div>
<input type="text" value="Message for Vue">
<div>
<span>Message for Vue</span> <button></button>
</div>
</div>
</div>`
)
)
reactAppInstance.setState({ component: VueSingleFileComponentChanged }, () => {
expect(document.body.innerHTML).toContain('<span>VueSingleFileComponentChanged</span>')
})
})

it('wires up events correctly', () => {
let eventRaised = false
const hndlr = () => (eventRaised = true)
Expand Down Expand Up @@ -157,30 +183,21 @@ describe('VueInReact', () => {
document.querySelectorAll('[data-reactroot]').forEach(el => {
el.removeAttribute('data-reactroot')
})
document.body.innerHTML = document.body.innerHTML.replace(
/<!--[\s\S]*?-->/g,
''
)
document.body.innerHTML = document.body.innerHTML.replace(/<!--[\s\S]*?-->/g, '')
}

it('works with a string', () => {
render('Hello')
expect(document.querySelector('#root div div').innerHTML).toBe(
'<div>Hello</div>'
)
expect(document.querySelector('#root div div').innerHTML).toBe('<div>Hello</div>')
})

it('works with a React component', () => {
render(<div>Hello</div>)
expect(document.querySelector('#root div div').innerHTML).toBe(
'<div><div>Hello</div></div>'
)
expect(document.querySelector('#root div div').innerHTML).toBe('<div><div>Hello</div></div>')
})

it('works with a React component', () => {
render(
<VueWrapper component={componentWithChildren}>wow so nested</VueWrapper>
)
render(<VueWrapper component={componentWithChildren}>wow so nested</VueWrapper>)
expect(document.querySelector('#root div div').innerHTML).toBe(
'<div><div><div><div>wow so nested</div></div></div></div>'
)
Expand Down

0 comments on commit 81c974d

Please sign in to comment.