From d7389302992b88ef503340b307959be24609a683 Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Wed, 11 Sep 2024 17:48:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(getApp):=20=E8=A1=A5=E5=85=85=20UniApp?= =?UTF-8?q?=20=E7=A4=BA=E4=BE=8B=E5=8F=8A=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/API/get-app/get-app.test.js | 8 +++++ pages/API/get-app/get-app.uvue | 57 +++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/pages/API/get-app/get-app.test.js b/pages/API/get-app/get-app.test.js index 598a639b..88dcc920 100644 --- a/pages/API/get-app/get-app.test.js +++ b/pages/API/get-app/get-app.test.js @@ -46,5 +46,13 @@ describe('getApp', () => { const newLifeCycleNum = await page.data('lifeCycleNum') expect(newLifeCycleNum - oldLifeCycleNum).toBe(100) await page.callMethod('setLifeCycleNum', oldLifeCycleNum) + }) + it('eventBus', async () => { + const res = await page.callMethod('checkEventBus') + expect(res).toBe(true) + }) + it('getAndroidApplication', async () => { + const res = await page.callMethod('checkGetAndroidApplication') + expect(res).toBe(true) }) }) diff --git a/pages/API/get-app/get-app.uvue b/pages/API/get-app/get-app.uvue index 02f99f6e..4fc32f47 100644 --- a/pages/API/get-app/get-app.uvue +++ b/pages/API/get-app/get-app.uvue @@ -35,10 +35,16 @@ 点击按钮调用 App.uvue methods increasetLifeCycleNum 方法 - + lifeCycleNum: {{ lifeCycleNum }} + + - lifeCycleNum: {{ lifeCycleNum }} @@ -167,6 +173,51 @@ // 自动化测试 setLifeCycleNum(num : number) { setLifeCycleNum(num) + }, + checkEventBus() : boolean { + const app = getApp() + let num = 0 + const fn1 = (args : any | null) => { + console.log('fn1 triggred', args) + num++ + } + const fn2 = (args : any | null) => { + console.log('fn2 triggred', args) + num++ + } + const fn3 = (args : any | null) => { + console.log('fn3 triggred', args) + num++ + } + + app.on('fn12', fn1) + app.on('fn12', fn2) + app.once('fn3', fn3) + + app.emit('fn12', { name: 'name' }) + app.emit('fn12', { age: 20 }) + app.off('fn12', fn1) + app.emit('fn12', null) + + app.emit('fn3', { name: 'name' }) + app.emit('fn3', { age: 20 }) + app.emit('fn3', null) + + const res = num == 6 + console.log('checkEventBus', res) + return res + }, + checkGetAndroidApplication() : boolean { + const app = getApp() + const androidApplication = app.getAndroidApplication() + // #ifdef APP-ANDROID + const res = androidApplication != null + // #endif + // #ifndef APP-ANDROID + const res = androidApplication == null + // #endif + console.log('checkGetAndroidApplication', res) + return res } }, } -- GitLab