|
| 1 | +# `render(component {, options}])` |
| 2 | + |
| 3 | +- **引数:** |
| 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 | +- **戻り値:** `{CheerioWrapper}` |
| 17 | + |
| 18 | +- **オプション:** |
| 19 | + |
| 20 | +[オプション](./options.md)を参照してください。 |
| 21 | + |
| 22 | +- **使い方:** |
| 23 | + |
| 24 | +オブジェクトを文字列にレンダリングして [cheerio wrapper](https://github.com/cheeriojs/cheerio) を返します。 |
| 25 | + |
| 26 | +Cheerio は Node.js で jQuery のように DOM をスキャンするためのライブラリです。 |
| 27 | +これは Vue Test Utils の [`Wrapper`](wrapper/README.md) に似ているAPIを持っています。 |
| 28 | + |
| 29 | +コンポーネントを静的なHTMLにレンダリングするために、`render` は内部で [`vue-server-renderer`](https://ssr.vuejs.org/en/basic.html) を使用します。 |
| 30 | + |
| 31 | +`render` は `@vue/server-test-utils` パッケージに含まれています。 |
| 32 | + |
| 33 | +**オプションなし:** |
| 34 | + |
| 35 | +```js |
| 36 | +import { render } from '@vue/server-test-utils' |
| 37 | +import Foo from './Foo.vue' |
| 38 | + |
| 39 | +describe('Foo', () => { |
| 40 | + it('renders a div', () => { |
| 41 | + const wrapper = render(Foo) |
| 42 | + expect(wrapper.text()).toContain('<div></div>') |
| 43 | + }) |
| 44 | +}) |
| 45 | +``` |
| 46 | + |
| 47 | +**Vueオプションを使用:** |
| 48 | + |
| 49 | +```js |
| 50 | +import { render } from '@vue/server-test-utils' |
| 51 | +import Foo from './Foo.vue' |
| 52 | + |
| 53 | +describe('Foo', () => { |
| 54 | + it('renders a div', () => { |
| 55 | + const wrapper = render(Foo, { |
| 56 | + propsData: { |
| 57 | + color: 'red' |
| 58 | + } |
| 59 | + }) |
| 60 | + expect(wrapper.text()).toContain('red') |
| 61 | + }) |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +**デフォルトおよび名前付きスロット:** |
| 66 | + |
| 67 | +```js |
| 68 | +import { render } from '@vue/server-test-utils' |
| 69 | +import Foo from './Foo.vue' |
| 70 | +import Bar from './Bar.vue' |
| 71 | +import FooBar from './FooBar.vue' |
| 72 | + |
| 73 | +describe('Foo', () => { |
| 74 | + it('renders a div', () => { |
| 75 | + const wrapper = render(Foo, { |
| 76 | + slots: { |
| 77 | + default: [Bar, FooBar], |
| 78 | + fooBar: FooBar, // <slot name="FooBar" /> にマッチします。 |
| 79 | + foo: '<div />' |
| 80 | + } |
| 81 | + }) |
| 82 | + expect(wrapper.text()).toContain('<div></div>') |
| 83 | + }) |
| 84 | +}) |
| 85 | +``` |
| 86 | + |
| 87 | +**グローバルプロパティをスタブする:** |
| 88 | + |
| 89 | +```js |
| 90 | +import { render } from '@vue/server-test-utils' |
| 91 | +import Foo from './Foo.vue' |
| 92 | + |
| 93 | +describe('Foo', () => { |
| 94 | + it('renders a div', () => { |
| 95 | + const $route = { path: 'http://www.example-path.com' } |
| 96 | + const wrapper = render(Foo, { |
| 97 | + mocks: { |
| 98 | + $route |
| 99 | + } |
| 100 | + }) |
| 101 | + expect(wrapper.text()).toContain($route.path) |
| 102 | + }) |
| 103 | +}) |
| 104 | +``` |
0 commit comments