|
| 1 | +# `renderToString(component {, options}])` |
| 2 | + |
| 3 | +- **Arguments:** |
| 4 | + |
| 5 | + - `{Component} component` |
| 6 | + - `{Object} options` |
| 7 | + - `{Object} context` |
| 8 | + - `{Array<Component|Object>|Component} children` |
| 9 | + - `{Object} slots` |
| 10 | + - `{Array<Componet|Object>|Component|String} default` |
| 11 | + - `{Array<Componet|Object>|Component|String} named` |
| 12 | + - `{Object} mocks` |
| 13 | + - `{Object|Array<string>} stubs` |
| 14 | + - `{Vue} localVue` |
| 15 | + |
| 16 | +- **Returns:** `{string}` |
| 17 | + |
| 18 | +- **Options:** |
| 19 | + |
| 20 | +See [options](./options.md) |
| 21 | + |
| 22 | +- **Usage:** |
| 23 | + |
| 24 | +Renders a component to HTML. |
| 25 | + |
| 26 | +`renderToString` uses [`vue-server-renderer`](https://ssr.vuejs.org/en/basic.html) under the hood, to render a component to HTML. |
| 27 | + |
| 28 | +**Without options:** |
| 29 | + |
| 30 | +```js |
| 31 | +import { renderToString } from '@vue/test-utils' |
| 32 | +import { expect } from 'chai' |
| 33 | +import Foo from './Foo.vue' |
| 34 | + |
| 35 | +describe('Foo', () => { |
| 36 | + it('renders a div', () => { |
| 37 | + const renderedString = renderToString(Foo) |
| 38 | + expect(renderedString).toContain('<div></div>') |
| 39 | + }) |
| 40 | +}) |
| 41 | +``` |
| 42 | + |
| 43 | +**With Vue options:** |
| 44 | + |
| 45 | +```js |
| 46 | +import { renderToString } from '@vue/test-utils' |
| 47 | +import { expect } from 'chai' |
| 48 | +import Foo from './Foo.vue' |
| 49 | + |
| 50 | +describe('Foo', () => { |
| 51 | + it('renders a div', () => { |
| 52 | + const renderedString = renderToString(Foo, { |
| 53 | + propsData: { |
| 54 | + color: 'red' |
| 55 | + } |
| 56 | + }) |
| 57 | + expect(renderedString).toContain('red') |
| 58 | + }) |
| 59 | +}) |
| 60 | +``` |
| 61 | + |
| 62 | +**Default and named slots:** |
| 63 | + |
| 64 | +```js |
| 65 | +import { renderToString } from '@vue/test-utils' |
| 66 | +import { expect } from 'chai' |
| 67 | +import Foo from './Foo.vue' |
| 68 | +import Bar from './Bar.vue' |
| 69 | +import FooBar from './FooBar.vue' |
| 70 | + |
| 71 | +describe('Foo', () => { |
| 72 | + it('renders a div', () => { |
| 73 | + const renderedString = renderToString(Foo, { |
| 74 | + slots: { |
| 75 | + default: [Bar, FooBar], |
| 76 | + fooBar: FooBar, // Will match <slot name="FooBar" />, |
| 77 | + foo: '<div />' |
| 78 | + } |
| 79 | + }) |
| 80 | + expect(renderedString).toContain('<div></div>') |
| 81 | + }) |
| 82 | +}) |
| 83 | +``` |
| 84 | + |
| 85 | +**Stubbing global properties:** |
| 86 | + |
| 87 | +```js |
| 88 | +import { renderToString } from '@vue/test-utils' |
| 89 | +import { expect } from 'chai' |
| 90 | +import Foo from './Foo.vue' |
| 91 | + |
| 92 | +describe('Foo', () => { |
| 93 | + it('renders a div', () => { |
| 94 | + const $route = { path: 'http://www.example-path.com' } |
| 95 | + const renderedString = renderToString(Foo, { |
| 96 | + mocks: { |
| 97 | + $route |
| 98 | + } |
| 99 | + }) |
| 100 | + expect(renderedString).toContain($route.path) |
| 101 | + }) |
| 102 | +}) |
| 103 | +``` |
0 commit comments