文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
attach()
参数:
{Component} component
{Object} options
{boolean} attachToDocument
{Object} slots
{Array<Component|Object>|Component|string} default
{Array<Component|Object>|Component|string} named
{Object|Array<string>} stubs
{Object} data
{Object|Component} parentComponent
{Object} methods
返回值:
{Wrapper}
选项:移步 挂载选项
用法:
创建一个包含被挂载和渲染的 San 组件的 Wrapper
。
不使用options选项
import {attach} from 'san-test-utils';
import foo from './foo';
describe('Foo', () => {
it('renders a div', () => {
const wrapper = attach(foo);
expect(wrapper.contains('div')).toBe(true);
});
});
使用options选项
import {attach} from 'san-test-utils';
import foo from './foo';
describe('Foo', () => {
it('renders a div', () => {
const wrapper = attach(foo, {
data: {
color: 'red'
}
});
expect(wrapper.data().color).toBe('red');
});
});
固定在DOM上
import {attach} from 'san-test-utils';
import foo from './foo';
describe('Foo', () => {
it('renders a div', () => {
const wrapper = attach(foo, {
attachToDocument: true
});
expect(wrapper.contains('div')).toBe(true);
});
});
默认插槽和具名插槽
import {attach} from 'san-test-utils';
import foo from './foo';
import bar from './bar';
import fooBar from './fooBar';
describe('Foo', () => {
it('renders a div', () => {
const wrapper = attach(foo, {
slots: {
default: [bar, fooBar],
fooBar: fooBar, // 将匹配 `<slot name="fooBar" />`。
foo: '<div />'
}
});
expect(wrapper.contains('div')).toBe(true);
});
});
将组件存根
import {attach} from 'san-test-utils';
import foo from './foo';
import bar from './bar';
import faz from './faz';
describe('Foo', () => {
it('renders a div', () => {
const wrapper = attach(foo, {
stubs: {
bar: '<div class="stubbed" />',
barfoo: true,
foobar: faz
}
});
expect(wrapper.contains('.stubbed')).toBe(true);
expect(wrapper.contains(bar)).toBe(true);
});
});
- 延伸阅读:
Wrapper
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论