From 751eabaae9c597ff905a5e39e14ef4ec6098831c Mon Sep 17 00:00:00 2001 From: Amour1688 Date: Thu, 14 May 2020 13:24:22 +0800 Subject: [PATCH] with @vue/test-utils --- .gitignore | 1 + package.json | 1 + test/index.test.js | 85 ++++++++++++++++++++++++++++++---------------- 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index cab290e..f02ca0a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ pids *.pid *.seed *.pid.lock +yarn.lock # Directory for instrumented libs generated by jscoverage/JSCover lib-cov diff --git a/package.json b/package.json index eb447da..5713389 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "devDependencies": { "@babel/core": "^7.9.6", "@babel/preset-env": "^7.9.6", + "@vue/test-utils": "^2.0.0-alpha.4", "babel-eslint": "^10.1.0", "babel-jest": "^26.0.1", "babel-loader": "^8.1.0", diff --git a/test/index.test.js b/test/index.test.js index dc44570..9b9e8fe 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,21 +1,32 @@ -import { createApp } from 'vue'; - -const render = (app) => { - const root = document.createElement('div'); - document.body.append(root); - createApp(app).mount(root); - return root; -}; +import { shallowMount } from '@vue/test-utils'; test('should render with setup', () => { - const App = { + const wrapper = shallowMount({ setup() { return () =>
123
; }, - }; + }); + expect(wrapper.text()).toBe('123'); +}); - const wrapper = render(App); - expect(wrapper.innerHTML).toBe('
123
'); +test('Extracts attrs', () => { + const wrapper = shallowMount({ + setup() { + return () =>
; + }, + }); + expect(wrapper.element.id).toBe('hi'); + expect(wrapper.element.dir).toBe('ltr'); +}); + +test('Binds attrs', () => { + const id = 'foo'; + const wrapper = shallowMount({ + setup() { + return () =>
{id}
; + }, + }); + expect(wrapper.text()).toBe('foo'); }); test('should not fallthrough with inheritAttrs: false', () => { @@ -23,12 +34,14 @@ test('should not fallthrough with inheritAttrs: false', () => { Child.inheritAttrs = false; - const Parent = () => ( - - ); - - const wrapper = render(Parent); - expect(wrapper.innerHTML).toBe('
1
'); + const wrapper = shallowMount({ + setup() { + return () => ( + + ); + }, + }); + expect(wrapper.text()).toBe('1'); }); @@ -38,28 +51,40 @@ test('should render', () => { return
1234
; }, }; - const wrapper = render(App); - expect(wrapper.innerHTML).toBe('
1234
'); + const wrapper = shallowMount(App); + expect(wrapper.html()).toBe('
1234
'); }); test('xlink:href', () => { - const wrapper = render(() => ); - expect(wrapper.innerHTML).toBe(''); + const wrapper = shallowMount({ + setup() { + return () => ; + }, + }); + expect(wrapper.attributes()['xlink:href']).toBe('#name'); }); -// test('Merge class', () => { -// const wrapper = render(() =>
); -// expect(wrapper.innerHTML).toBe('
'); -// }); +// // test('Merge class', () => { +// // const wrapper = render(() =>
); +// // expect(wrapper.innerHTML).toBe('
'); +// // }); test('JSXSpreadChild', () => { const a = ['1', '2']; - const wrapper = render(() =>
{[...a]}
); - expect(wrapper.innerHTML).toBe('
12
'); + const wrapper = shallowMount({ + setup() { + return () =>
{[...a]}
; + }, + }); + expect(wrapper.text).toBe('12'); }); test('domProps input[value]', () => { const val = 'foo'; - const wrapper = render(() => ); - expect(wrapper.innerHTML).toBe(''); + const wrapper = shallowMount({ + setup() { + return () => ; + }, + }); + expect(wrapper.html()).toBe(''); });