Skip to content
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

Add renderToString method #435

Merged
merged 9 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [API](api/README.md)
* [mount](api/mount.md)
* [shallow](api/shallow.md)
* [renderToString](api/renderToString.md)
* [Mounting Options](api/options.md)
- [context](api/options.md#context)
- [slots](api/options.md#slots)
Expand Down
1 change: 1 addition & 0 deletions docs/en/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [API](api/README.md)
* [mount](api/mount.md)
* [shallow](api/shallow.md)
* [renderToString](api/renderToString.md)
* [Mounting Options](api/options.md)
- [context](api/options.md#context)
- [slots](api/options.md#slots)
Expand Down
1 change: 1 addition & 0 deletions docs/en/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [mount](./mount.md)
* [shallow](./shallow.md)
* [renderToString](./renderToString.md)
* [Mounting Options](./options.md)
- [context](./options.md#context)
- [slots](./options.md#slots)
Expand Down
103 changes: 103 additions & 0 deletions docs/en/api/renderToString.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# `renderToString(component {, options}])`

- **Arguments:**

- `{Component} component`
- `{Object} options`
- `{Object} context`
- `{Array<Component|Object>|Component} children`
- `{Object} slots`
- `{Array<Componet|Object>|Component|String} default`
- `{Array<Componet|Object>|Component|String} named`
- `{Object} mocks`
- `{Object|Array<string>} stubs`
- `{Vue} localVue`

- **Returns:** `{string}`

- **Options:**

See [options](./options.md)

- **Usage:**

Renders a component to HTML.

`renderToString` uses [`vue-server-renderer`](https://ssr.vuejs.org/en/basic.html) under the hood, to render a component to HTML.

**Without options:**

```js
import { renderToString } from '@vue/test-utils'
import { expect } from 'chai'
import Foo from './Foo.vue'

describe('Foo', () => {
it('renders a div', () => {
const renderedString = renderToString(Foo)
expect(renderedString).toContain('<div></div>')
})
})
```

**With Vue options:**

```js
import { renderToString } from '@vue/test-utils'
import { expect } from 'chai'
import Foo from './Foo.vue'

describe('Foo', () => {
it('renders a div', () => {
const renderedString = renderToString(Foo, {
propsData: {
color: 'red'
}
})
expect(renderedString).toContain('red')
})
})
```

**Default and named slots:**

```js
import { renderToString } from '@vue/test-utils'
import { expect } from 'chai'
import Foo from './Foo.vue'
import Bar from './Bar.vue'
import FooBar from './FooBar.vue'

describe('Foo', () => {
it('renders a div', () => {
const renderedString = renderToString(Foo, {
slots: {
default: [Bar, FooBar],
fooBar: FooBar, // Will match <slot name="FooBar" />,
foo: '<div />'
}
})
expect(renderedString).toContain('<div></div>')
})
})
```

**Stubbing global properties:**

```js
import { renderToString } from '@vue/test-utils'
import { expect } from 'chai'
import Foo from './Foo.vue'

describe('Foo', () => {
it('renders a div', () => {
const $route = { path: 'http://www.example-path.com' }
const renderedString = renderToString(Foo, {
mocks: {
$route
}
})
expect(renderedString).toContain($route.path)
})
})
```
4 changes: 4 additions & 0 deletions flow/modules.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ declare module 'lodash/cloneDeep' {
declare module 'vue-template-compiler' {
declare module.exports: any;
}

declare module 'vue-server-renderer' {
declare module.exports: any;
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"vue-class-component": "^6.1.2",
"vue-loader": "^13.6.2",
"vue-router": "^3.0.1",
"vue-server-renderer": "2.5.13",
"vue-template-compiler": "^2.5.13",
"vuetify": "^0.16.9",
"vuex": "^3.0.1",
Expand All @@ -96,6 +97,7 @@
},
"peerDependencies": {
"vue": "2.x",
"vue-server-renderer": "2.x",
"vue-template-compiler": "^2.x"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion scripts/test-compat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

test_version_number(){
echo "running unit tests with Vue $1"
yarn add vue@$1 vue-template-compiler@$1
yarn add vue@$1 vue-template-compiler@$1 vue-server-renderer@$1
yarn test:unit
yarn test:unit:karma
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import shallow from './shallow'
import mount from './mount'
import renderToString from './renderToString'
import createLocalVue from './create-local-vue'
import TransitionStub from './components/TransitionStub'
import TransitionGroupStub from './components/TransitionGroupStub'
Expand All @@ -11,6 +12,7 @@ export default {
config,
mount,
shallow,
renderToString,
TransitionStub,
TransitionGroupStub,
RouterLinkStub
Expand Down
3 changes: 3 additions & 0 deletions src/lib/add-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ function addSlots (vm: Component, slots: Object): void {

if (Array.isArray(slots[key])) {
slots[key].forEach((slotValue) => {
if (!isValidSlot(slotValue)) {
throwError('slots[key] must be a Component, string or an array of Components')
}
addSlotToVm(vm, key, slotValue)
})
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import errorHandler from './lib/error-handler'
import { findAllVueComponentsFromVm } from './lib/find-vue-components'

Vue.config.productionTip = false
Vue.config.errorHandler = errorHandler
Vue.config.devtools = false
Vue.config.errorHandler = errorHandler

export default function mount (component: Component, options: Options = {}): VueWrapper {
// Remove cached constructor
Expand Down
31 changes: 31 additions & 0 deletions src/renderToString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @flow

import Vue from 'vue'
import createInstance from './lib/create-instance'
import './lib/polyfills/object-assign-polyfill'
import { throwError } from './lib/util'

Vue.config.productionTip = false
Vue.config.devtools = false

export default function renderToString (component: Component, options: Options = {}): string {
const renderer = require('vue-server-renderer').createRenderer()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why vue-server-renderer gets imported using CommonJS and not ESM?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESM imports are hoisted to the top, so any ESM import will always be bundled.

vue-server-renderer relies on node modules, like path, which makes it difficult to compile with webpack. require is only run when the code runs, so putting it inside the renderToString means we only try and import vue-server-renderer when the renderToString function is run.

We should also add a warning that checks if the user is in node, and we can throw an error if they aren't to tell them renderToString needs to be run in node.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear explanation, thank you very much!

// Remove cached constructor
delete component._Ctor

if (options.attachToDocument) {
throwError('you cannot use attachToDocument with renderToString')
}

const vm = createInstance(component, options)

let renderedString = ''

renderer.renderToString(vm, (err, res) => {
if (err) {
console.log(err)
}
renderedString = res
})
return renderedString
}
31 changes: 27 additions & 4 deletions test/resources/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global describe, it*/

import Vue from 'vue'
import { shallow, mount } from '~vue-test-utils'
import { shallow, mount, renderToString } from '~vue-test-utils'

export const vueVersion = Number(`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`)

Expand All @@ -21,20 +21,43 @@ export function functionalSFCsSupported () {
return vueVersion >= 2.5
}

const shallowAndMount = [mount, shallow]
const shallowMountAndRender = (navigator.userAgent.includes && navigator.userAgent.includes('jsdom'))
? [mount, shallow, renderToString]
: [mount, shallow]

export function describeWithShallowAndMount (spec, cb) {
;[mount, shallow].forEach(method => {
shallowAndMount.forEach(method => {
describe(`${spec} with ${method.name}`, () => cb(method))
})
}

describeWithShallowAndMount.skip = function (spec, cb) {
;[mount, shallow].forEach(method => {
shallowAndMount.forEach(method => {
describe.skip(`${spec} with ${method.name}`, () => cb(method))
})
}

describeWithShallowAndMount.only = function (spec, cb) {
;[mount, shallow].forEach(method => {
shallowAndMount.forEach(method => {
describe.only(`${spec} with ${method.name}`, () => cb(method))
})
}

export function describeWithMountingMethods (spec, cb) {
shallowMountAndRender.forEach(method => {
describe(`${spec} with ${method.name}`, () => cb(method))
})
}

describeWithMountingMethods.skip = function (spec, cb) {
shallowMountAndRender.forEach(method => {
describe.skip(`${spec} with ${method.name}`, () => cb(method))
})
}

describeWithMountingMethods.only = function (spec, cb) {
shallowMountAndRender.forEach(method => {
describe.only(`${spec} with ${method.name}`, () => cb(method))
})
}
Expand Down
1 change: 0 additions & 1 deletion test/specs/components/TransitionGroupStub.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe('TransitionGroupStub', () => {
}),
watch: {
someWatchedData (newData) {
console.log('asd')
this.someData = newData
}
},
Expand Down
14 changes: 14 additions & 0 deletions test/specs/mounting-options/attachToDocument.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { compileToFunctions } from 'vue-template-compiler'
import { describeWithShallowAndMount } from '~resources/test-utils'
import { renderToString } from '~vue-test-utils'

describeWithShallowAndMount('options.attachToDocument', (mountingMethod) => {
it('returns VueWrapper with attachedToDocument set to true when passed attachToDocument in options', () => {
Expand All @@ -8,3 +9,16 @@ describeWithShallowAndMount('options.attachToDocument', (mountingMethod) => {
expect(wrapper.options.attachedToDocument).to.equal(true)
})
})

describe('options.attachToDocument with renderToString', () => {
it('throws error that renderToString does not accept attachToDocument', () => {
// renderToString can only be run in node
if (!(navigator.userAgent.includes && navigator.userAgent.includes('jsdom'))) {
return
}
const compiled = compileToFunctions('<div><input /></div>')
const fn = () => renderToString(compiled, { attachToDocument: true })
const message = '[vue-test-utils]: you cannot use attachToDocument with renderToString'
expect(fn).to.throw().with.property('message', message)
})
})
13 changes: 9 additions & 4 deletions test/specs/mounting-options/attrs.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { compileToFunctions } from 'vue-template-compiler'
import { attrsSupported } from '~resources/test-utils'
import { describeWithShallowAndMount } from '~resources/test-utils'
import {
describeWithMountingMethods,
itSkipIf
} from '~resources/test-utils'

describeWithShallowAndMount('options.attrs', (mountingMethod) => {
it('handles inherit attrs', () => {
describeWithMountingMethods('options.attrs', (mountingMethod) => {
itSkipIf(mountingMethod.name === 'renderToString',
'handles inherit attrs', () => {
if (!attrsSupported()) return
const wrapper = mountingMethod(compileToFunctions('<p :id="anAttr" />'), {
attrs: {
Expand All @@ -15,7 +19,8 @@ describeWithShallowAndMount('options.attrs', (mountingMethod) => {
expect(wrapper.vm.$attrs.anAttr).to.equal('an attribute')
})

it('defines attrs as empty object even when not passed', () => {
itSkipIf(mountingMethod.name === 'renderToString',
'defines attrs as empty object even when not passed', () => {
const wrapper = mountingMethod(compileToFunctions('<p />'))
expect(wrapper.vm.$attrs).to.deep.equal({})
})
Expand Down
22 changes: 15 additions & 7 deletions test/specs/mounting-options/context.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Vue from 'vue'
import { vueVersion } from '~resources/test-utils'
import { describeWithShallowAndMount } from '~resources/test-utils'
import { describeWithMountingMethods } from '~resources/test-utils'

describeWithShallowAndMount('options.context', (mountingMethod) => {
describeWithMountingMethods('options.context', (mountingMethod) => {
it('mounts functional component when passed context object', () => {
if (vueVersion <= 2.2) {
console.log('WARN: no current way to test functional component is component in v2.1.x')
Expand All @@ -21,8 +21,7 @@ describeWithShallowAndMount('options.context', (mountingMethod) => {
props: { show: true }
}

const wrapper = mountingMethod(Component, { context })
expect(wrapper.is(Component)).to.equal(true)
mountingMethod(Component, { context })
})

it('throws error if non functional component is passed with context option', () => {
Expand Down Expand Up @@ -69,7 +68,10 @@ describeWithShallowAndMount('options.context', (mountingMethod) => {
render: (h, { props }) => h('div', props.testProp)
}
const wrapper = mountingMethod(Component)
expect(wrapper.element.textContent).to.equal(defaultValue)
const HTML = mountingMethod.name === 'renderToString'
? wrapper
: wrapper.html()
expect(HTML).to.contain(defaultValue)
})

it('mounts functional component with a defined context.children text', () => {
Expand All @@ -84,7 +86,10 @@ describeWithShallowAndMount('options.context', (mountingMethod) => {
children: ['render text']
}
})
expect(wrapper.text()).to.equal('render text')
const HTML = mountingMethod.name === 'renderToString'
? wrapper
: wrapper.html()
expect(HTML).to.contain('render text')
})

it('mounts functional component with a defined context.children element', () => {
Expand All @@ -99,6 +104,9 @@ describeWithShallowAndMount('options.context', (mountingMethod) => {
children: [h => h('div', 'render component')]
}
})
expect(wrapper.text()).to.equal('render component')
const HTML = mountingMethod.name === 'renderToString'
? wrapper
: wrapper.html()
expect(HTML).to.contain('render component')
})
})
Loading