with @vue/test-utils

This commit is contained in:
Amour1688 2020-05-14 13:24:22 +08:00
parent b5a91efd93
commit 751eabaae9
3 changed files with 57 additions and 30 deletions

1
.gitignore vendored
View File

@ -14,6 +14,7 @@ pids
*.pid *.pid
*.seed *.seed
*.pid.lock *.pid.lock
yarn.lock
# Directory for instrumented libs generated by jscoverage/JSCover # Directory for instrumented libs generated by jscoverage/JSCover
lib-cov lib-cov

View File

@ -33,6 +33,7 @@
"devDependencies": { "devDependencies": {
"@babel/core": "^7.9.6", "@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6", "@babel/preset-env": "^7.9.6",
"@vue/test-utils": "^2.0.0-alpha.4",
"babel-eslint": "^10.1.0", "babel-eslint": "^10.1.0",
"babel-jest": "^26.0.1", "babel-jest": "^26.0.1",
"babel-loader": "^8.1.0", "babel-loader": "^8.1.0",

View File

@ -1,21 +1,32 @@
import { createApp } from 'vue'; import { shallowMount } from '@vue/test-utils';
const render = (app) => {
const root = document.createElement('div');
document.body.append(root);
createApp(app).mount(root);
return root;
};
test('should render with setup', () => { test('should render with setup', () => {
const App = { const wrapper = shallowMount({
setup() { setup() {
return () => <div>123</div>; return () => <div>123</div>;
}, },
}; });
expect(wrapper.text()).toBe('123');
});
const wrapper = render(App); test('Extracts attrs', () => {
expect(wrapper.innerHTML).toBe('<div>123</div>'); const wrapper = shallowMount({
setup() {
return () => <div id="hi" dir="ltr" />;
},
});
expect(wrapper.element.id).toBe('hi');
expect(wrapper.element.dir).toBe('ltr');
});
test('Binds attrs', () => {
const id = 'foo';
const wrapper = shallowMount({
setup() {
return () => <div>{id}</div>;
},
});
expect(wrapper.text()).toBe('foo');
}); });
test('should not fallthrough with inheritAttrs: false', () => { test('should not fallthrough with inheritAttrs: false', () => {
@ -23,12 +34,14 @@ test('should not fallthrough with inheritAttrs: false', () => {
Child.inheritAttrs = false; Child.inheritAttrs = false;
const Parent = () => ( const wrapper = shallowMount({
<Child class="parent" foo={1} /> setup() {
); return () => (
<Child class="parent" foo={1} />
const wrapper = render(Parent); );
expect(wrapper.innerHTML).toBe('<div>1</div>'); },
});
expect(wrapper.text()).toBe('1');
}); });
@ -38,28 +51,40 @@ test('should render', () => {
return <div>1234</div>; return <div>1234</div>;
}, },
}; };
const wrapper = render(App); const wrapper = shallowMount(App);
expect(wrapper.innerHTML).toBe('<div>1234</div>'); expect(wrapper.html()).toBe('<div>1234</div>');
}); });
test('xlink:href', () => { test('xlink:href', () => {
const wrapper = render(() => <use xlinkHref={'#name'}></use>); const wrapper = shallowMount({
expect(wrapper.innerHTML).toBe('<use xlink:href="#name"></use>'); setup() {
return () => <use xlinkHref={'#name'}></use>;
},
});
expect(wrapper.attributes()['xlink:href']).toBe('#name');
}); });
// test('Merge class', () => { // // test('Merge class', () => {
// const wrapper = render(() => <div class="a" {...{ class: 'b' } } />); // // const wrapper = render(() => <div class="a" {...{ class: 'b' } } />);
// expect(wrapper.innerHTML).toBe('<div class="a b"></div>'); // // expect(wrapper.innerHTML).toBe('<div class="a b"></div>');
// }); // // });
test('JSXSpreadChild', () => { test('JSXSpreadChild', () => {
const a = ['1', '2']; const a = ['1', '2'];
const wrapper = render(() => <div>{[...a]}</div>); const wrapper = shallowMount({
expect(wrapper.innerHTML).toBe('<div>12</div>'); setup() {
return () => <div>{[...a]}</div>;
},
});
expect(wrapper.text).toBe('12');
}); });
test('domProps input[value]', () => { test('domProps input[value]', () => {
const val = 'foo'; const val = 'foo';
const wrapper = render(() => <input type="text" value={val} />); const wrapper = shallowMount({
expect(wrapper.innerHTML).toBe('<input type="text">'); setup() {
return () => <input type="text" value={val} />;
},
});
expect(wrapper.html()).toBe('<input type="text">');
}); });