From 05ec03c24a3feed5c4fbc1509b43b3802d43116c Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Wed, 27 Dec 2023 18:00:01 +0800 Subject: [PATCH] feat: globalData --- App.uvue | 17 ++++ pages/API/get-app/get-app.test.js | 36 +++++++- pages/API/get-app/get-app.uvue | 133 +++++++++++++++++++++++++++++- 3 files changed, 183 insertions(+), 3 deletions(-) diff --git a/App.uvue b/App.uvue index f1aa02c9..5db44554 100644 --- a/App.uvue +++ b/App.uvue @@ -3,6 +3,23 @@ let firstBackTime = 0 export default { + globalData: { + str: 'default globalData str', + num: 0, + bool: false, + obj: { + str: 'default globalData obj str', + num: 0, + bool: false, + }, + null: null as string | null, + arr: [] as number[], + mySet: new Set(), + myMap: new Map(), + func: () : string => { + return 'globalData func' + } + }, onLaunch: function () { // 自动化测试 setLifeCycleNum(state.lifeCycleNum + 1000) diff --git a/pages/API/get-app/get-app.test.js b/pages/API/get-app/get-app.test.js index 5ede34d4..598a639b 100644 --- a/pages/API/get-app/get-app.test.js +++ b/pages/API/get-app/get-app.test.js @@ -6,6 +6,40 @@ describe('getApp', () => { page = await program.navigateTo(PAGE_PATH) await page.waitFor('view') }) + it('globalData', async () => { + await page.callMethod('getGlobalData') + let data = await page.data() + expect(data.originGlobalData.str).toBe('default globalData str') + expect(data.originGlobalData.num).toBe(0) + expect(data.originGlobalData.bool).toBe(false) + expect(data.originGlobalData.obj).toEqual({ + bool: false, + num: 0, + str: 'default globalData obj str', + }) + expect(data.originGlobalData.arr).toEqual([]) + expect(data.originGlobalData.mySet).toEqual([]) + expect(data.originGlobalData.myMap).toEqual({}) + expect(data.originGlobalDataFuncRes).toBe('globalData func') + await page.callMethod('setGlobalData') + data = await page.data() + expect(data.newGlobalData.str).toBe('new globalData str') + expect(data.newGlobalData.num).toBe(100) + expect(data.newGlobalData.bool).toBe(true) + expect(data.newGlobalData.obj).toEqual({ + bool: true, + num: 200, + str: 'new globalData obj str', + }) + expect(data.newGlobalData.arr).toEqual([1, 2, 3]) + expect(data.newGlobalData.mySet).toEqual(['a', 'b', 'c']) + expect(data.newGlobalData.myMap).toEqual({ + a: 1, + b: 2, + c: 3 + }) + expect(data.newGlobalDataFuncRes).toBe('new globalData func') + }) it('method', async () => { const oldLifeCycleNum = await page.data('lifeCycleNum') await page.callMethod('_increasetLifeCycleNum') @@ -13,4 +47,4 @@ describe('getApp', () => { expect(newLifeCycleNum - oldLifeCycleNum).toBe(100) await page.callMethod('setLifeCycleNum', oldLifeCycleNum) }) -}) +}) diff --git a/pages/API/get-app/get-app.uvue b/pages/API/get-app/get-app.uvue index aaf5fa1a..dbea3b1d 100644 --- a/pages/API/get-app/get-app.uvue +++ b/pages/API/get-app/get-app.uvue @@ -5,6 +5,32 @@ + + + + 点击按钮调用 App.uvue methods increasetLifeCycleNum 方法