From d377f1b76d293ca0931125a8521339d5fc4f3b91 Mon Sep 17 00:00:00 2001 From: yurj26 <1816387074@qq.com> Date: Wed, 28 Jun 2023 17:19:03 +0800 Subject: [PATCH] feat(button): add test --- env.js | 38 +++++++++++++ jest.config.js | 11 ++++ pages/component/button/button.test.js | 82 +++++++++++++++++++++++++++ pages/component/button/button.uvue | 22 +++++-- 4 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 env.js create mode 100644 jest.config.js create mode 100644 pages/component/button/button.test.js diff --git a/env.js b/env.js new file mode 100644 index 00000000..d7553e4b --- /dev/null +++ b/env.js @@ -0,0 +1,38 @@ +module.exports = { + "is-custom-runtime": false, + "compile": true, + "h5": { + "options": { + "headless": true + }, + "executablePath": "" + }, + "mp-weixin": { + "port": 9420, + "account": "", + "args": "", + "cwd": "", + "launch": true, + "teardown": "disconnect", + "remote": false, + "executablePath": "" + }, + "app-plus": { + "android": { + "id": "", + "executablePath": "" + }, + "version": "", + "ios": { + "id": "", + "executablePath": "" + }, + "uni-app-x": { + "version": "/Applications/HBuilderX-Alpha.app/Contents/HBuilderX/plugins/uniappx-launcher/base/version.txt", + "android": { + "id": "emulator-5554", + "executablePath": "/Applications/HBuilderX-Alpha.app/Contents/HBuilderX/plugins/uniappx-launcher/base/android_base.apk" + } + } + } +} \ No newline at end of file diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 00000000..882d3642 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,11 @@ +module.exports = { + testTimeout: 10000, + reporters: [ + 'default' + ], + watchPathIgnorePatterns: ['/node_modules/', '/dist/', '/.git/'], + moduleFileExtensions: ['js', 'json'], + rootDir: __dirname, + testMatch: ["/pages/**/*test.[jt]s?(x)"], + testPathIgnorePatterns: ['/node_modules/'] +} diff --git a/pages/component/button/button.test.js b/pages/component/button/button.test.js new file mode 100644 index 00000000..3a3c2011 --- /dev/null +++ b/pages/component/button/button.test.js @@ -0,0 +1,82 @@ +function getData(key = '') { + return new Promise(async (resolve, reject) => { + const data = await page.data() + resolve(key ? data[key] : data) + }) +} + +let page +beforeAll(async () => { + page = await program.reLaunch('/pages/component/button/button') + await page.waitFor(1000) +}) + +describe('Button.uvue', () => { + it('click', async () => { + const defaultBtn = await page.$('.default-button') + const disabledBtn = await page.$('.disabled-button') + expect(await getData('count')).toEqual(0) + await defaultBtn.tap() + expect(await getData('count')).toEqual(1) + await disabledBtn.tap() + expect(await getData('count')).toEqual(1) + }) + it('length', async () => { + const btnLen = await page.$$('.button') + expect(btnLen.length).toBe(13) + }) + it('text', async () => { + const textBtn = await page.$('.text-button') + expect(await textBtn.text()).toEqual('按钮') + await page.setData({ + text: 'button' + }) + // TODO 待修复 + expect(await textBtn.text()).toEqual('button') + }) + it('type', async () => { + // TODO 目前获取组件属性只能通过组件的name,并且拿到的是最后一个 + const btn = await page.$('Button') + expect(await btn.property('type')).toBe('warn') + await page.setData({ + type: 'default' + }) + await page.waitFor(500) + expect(await btn.property('type')).toBe('default') + await page.setData({ + type: 'warn' + }) + await page.waitFor(500) + expect(await btn.property('type')).toBe('warn') + }) + it('size', async () => { + const btn = await page.$('Button') + expect(await btn.property('size')).toBe('mini') + await page.setData({ + size: 'default' + }) + await page.waitFor(500) + expect(await btn.property('size')).toBe('default') + await page.setData({ + size: 'mini' + }) + }) + it('plain', async () => { + const btn = await page.$('Button') + expect(await btn.property('plain')).toBe(false) + await page.setData({ + plain: true + }) + await page.waitFor(500) + expect(await btn.property('plain')).toBe(true) + }) + it('disabled', async () => { + const btn = await page.$('Button') + expect(await btn.property('disabled')).toBe(false) + await page.setData({ + disabled: true + }) + await page.waitFor(500) + expect(await btn.property('disabled')).toBe(true) + }) +}) \ No newline at end of file diff --git a/pages/component/button/button.uvue b/pages/component/button/button.uvue index 8e7d90f2..31a3c1cd 100644 --- a/pages/component/button/button.uvue +++ b/pages/component/button/button.uvue @@ -2,18 +2,18 @@ - + - - + + - + @@ -24,7 +24,8 @@ - + @@ -36,7 +37,13 @@ return { title: 'button', loading: false, - _timer: 0 + _timer: 0, + text: '按钮', + type: 'warn', + size: 'mini', + plain: false, + disabled: false, + count: 0 } }, onShow() { @@ -55,6 +62,9 @@ // clearTimeout(this._timer); // } // } + addCount() { + this.count++ + } }, } -- GitLab