From dbd4486f61d583f6ebacf009d00cc210fdc105df Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Mon, 27 Nov 2023 20:57:57 +0800 Subject: [PATCH] feat: globalProperties --- main.uts | 40 ++- pages.json | 334 +++++++++--------- .../globalProperties/globalProperties.test.js | 36 ++ .../globalProperties/globalProperties.uvue | 98 +++++ pages/tabBar/API.uvue | 10 +- 5 files changed, 342 insertions(+), 176 deletions(-) create mode 100644 pages/API/globalProperties/globalProperties.test.js create mode 100644 pages/API/globalProperties/globalProperties.uvue diff --git a/main.uts b/main.uts index b369567e..f8fa5022 100644 --- a/main.uts +++ b/main.uts @@ -1,10 +1,32 @@ -import App from './App.uvue' - -import { createSSRApp } from 'vue' -export function createApp() { - const app = createSSRApp(App) - - return { - app - } +import App from './App.uvue' + +import { createSSRApp, reactive } from 'vue' +export function createApp() { + const app = createSSRApp(App) + app.config.globalProperties.globalPropertiesStr = 'default string' + app.config.globalProperties.globalPropertiesNum = 0 + app.config.globalProperties.globalPropertiesBool = false + app.config.globalProperties.globalPropertiesObj = { + str: 'default globalProperties obj string', + num: 0, + bool: false, + } + app.config.globalProperties.globalPropertiesNull = null as string | null + app.config.globalProperties.globalPropertiesArr = [] as number[] + app.config.globalProperties.globalPropertiesSet = new Set() + app.config.globalProperties.globalPropertiesMap = new Map() + app.config.globalProperties.globalPropertiesReactiveObj = reactive({ + str: 'default reactive string', + num: 0, + bool: false, + }) + app.config.globalProperties.globalPropertiesFn = () : string => { + console.log('this.globalPropertiesStr', this.globalPropertiesStr) + console.log('this.globalPropertiesNum', this.globalPropertiesNum) + return `globalPropertiesStr: ${this.globalPropertiesStr}, globalPropertiesNum: ${this.globalPropertiesNum}` + } + + return { + app + } } diff --git a/pages.json b/pages.json index 910d66c3..92cc5f4e 100644 --- a/pages.json +++ b/pages.json @@ -1,6 +1,6 @@ { "pages": [ - //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages + // pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages { "path": "pages/tabBar/component", "style": { @@ -389,6 +389,173 @@ "style": { "navigationBarTitleText": "拦截器测试页面 2" } + }, + { + "path": "pages/API/request/request", + "style": { + "navigationBarTitleText": "request" + } + }, + { + "path": "pages/API/upload-file/upload-file", + "style": { + "navigationBarTitleText": "upload-file" + } + }, + { + "path": "pages/API/download-file/download-file", + "style": { + "navigationBarTitleText": "download-file" + } + }, + { + "path": "pages/API/websocket-socketTask/websocket-socketTask", + "style": { + "navigationBarTitleText": "websocket-socketTask" + } + }, + { + "path": "pages/API/websocket-global/websocket-global", + "style": { + "navigationBarTitleText": "websocket-global" + } + }, + { + "path": "pages/API/unicloud-call-function/unicloud-call-function", + "style": { + "navigationBarTitleText": "" + } + }, + { + "path": "pages/API/unicloud-import-object/unicloud-import-object", + "style": { + "navigationBarTitleText": "" + } + }, + { + "path": "pages/API/get-system-info/get-system-info", + "style": { + "navigationBarTitleText": "get-system-info" + } + }, + { + "path": "pages/API/get-device-info/get-device-info", + "style": { + "navigationBarTitleText": "get-device-info" + } + }, + { + "path": "pages/API/get-app-base-info/get-app-base-info", + "style": { + "navigationBarTitleText": "get-app-base-info" + } + }, + { + "path": "pages/API/get-system-setting/get-system-setting", + "style": { + "navigationBarTitleText": "get-system-setting" + } + }, + { + "path": "pages/API/get-app-authorize-setting/get-app-authorize-setting", + "style": { + "navigationBarTitleText": "get-app-authorize-setting" + } + }, + { + "path": "pages/API/preview-image/preview-image", + "style": { + "navigationBarTitleText": "图片预览" + } + }, + { + "path": "pages/API/save-image-to-photos-album/save-image-to-photos-album", + "style": { + "navigationBarTitleText": "保存图片到相册" + } + }, + { + "path": "pages/API/choose-image/choose-image", + "style": { + "navigationBarTitleText": "图片" + } + }, + { + "path": "pages/API/get-network-type/get-network-type", + "style": { + "navigationBarTitleText": "get-network-type" + } + }, + { + "path": "pages/API/page-scroll-to/page-scroll-to", + "style": { + "navigationBarTitleText": "pageScrollTo" + } + }, + { + "path": "pages/API/event-bus/event-bus", + "style": { + "navigationBarTitleText": "event-bus" + } + }, + { + "path": "pages/API/unicloud-file-api/unicloud-file-api", + "style": { + "navigationBarTitleText": "" + } + }, + { + "path": "pages/API/unicloud-database/unicloud-database", + "style": { + "navigationBarTitleText": "" + } + }, + { + "path": "pages/API/get-battery-info/get-battery-info", + "style": { + "navigationBarTitleText": "电量" + } + }, + { + "path": "pages/API/get-window-info/get-window-info", + "style": { + "navigationBarTitleText": "get-window-info" + } + }, + { + "path": "pages/API/facial-recognition-verify/facial-recognition-verify", + "style": { + "navigationBarTitleText": "", + "enablePullDownRefresh": false + } + }, + { + "path": "pages/API/element-takesnapshot/element-takesnapshot", + "style": { + "navigationBarTitleText": "takeSnapshot", + "enablePullDownRefresh": false + } + }, + { + "path": "pages/API/element-draw/element-draw", + "style": { + "navigationBarTitleText": "getDrawableContext", + "enablePullDownRefresh": false + } + }, + { + "path": "pages/API/install-apk/install-apk", + "style": { + "navigationBarTitleText": "install-apk", + "enablePullDownRefresh": false + } + }, + { + "path": "pages/API/globalProperties/globalProperties", + "style": { + "navigationBarTitleText": "globalProperties", + "enablePullDownRefresh": false + } }, { "path": "pages/tabBar/CSS", @@ -733,96 +900,6 @@ "navigationBarTitleText": "Rotate" } }, - { - "path": "pages/API/request/request", - "style": { - "navigationBarTitleText": "request" - } - }, - { - "path": "pages/API/upload-file/upload-file", - "style": { - "navigationBarTitleText": "upload-file" - } - }, - { - "path": "pages/API/download-file/download-file", - "style": { - "navigationBarTitleText": "download-file" - } - }, - { - "path": "pages/API/websocket-socketTask/websocket-socketTask", - "style": { - "navigationBarTitleText": "websocket-socketTask" - } - }, - { - "path": "pages/API/websocket-global/websocket-global", - "style": { - "navigationBarTitleText": "websocket-global" - } - }, - { - "path": "pages/API/unicloud-call-function/unicloud-call-function", - "style": { - "navigationBarTitleText": "" - } - }, - { - "path": "pages/API/unicloud-import-object/unicloud-import-object", - "style": { - "navigationBarTitleText": "" - } - }, - { - "path": "pages/API/get-system-info/get-system-info", - "style": { - "navigationBarTitleText": "get-system-info" - } - }, - { - "path": "pages/API/get-device-info/get-device-info", - "style": { - "navigationBarTitleText": "get-device-info" - } - }, - { - "path": "pages/API/get-app-base-info/get-app-base-info", - "style": { - "navigationBarTitleText": "get-app-base-info" - } - }, - { - "path": "pages/API/get-system-setting/get-system-setting", - "style": { - "navigationBarTitleText": "get-system-setting" - } - }, - { - "path": "pages/API/get-app-authorize-setting/get-app-authorize-setting", - "style": { - "navigationBarTitleText": "get-app-authorize-setting" - } - }, - { - "path": "pages/API/preview-image/preview-image", - "style": { - "navigationBarTitleText": "图片预览" - } - }, - { - "path": "pages/API/save-image-to-photos-album/save-image-to-photos-album", - "style": { - "navigationBarTitleText": "保存图片到相册" - } - }, - { - "path": "pages/API/choose-image/choose-image", - "style": { - "navigationBarTitleText": "图片" - } - }, { "path": "pages/component/scroll-view/scroll-view-refresher", "style": { @@ -841,24 +918,6 @@ "navigationBarTitleText": "详情示例" } }, - { - "path": "pages/API/get-network-type/get-network-type", - "style": { - "navigationBarTitleText": "get-network-type" - } - }, - { - "path": "pages/API/page-scroll-to/page-scroll-to", - "style": { - "navigationBarTitleText": "pageScrollTo" - } - }, - { - "path": "pages/API/event-bus/event-bus", - "style": { - "navigationBarTitleText": "event-bus" - } - }, { "path": "pages/template/drop-card/drop-card", "style": { @@ -895,12 +954,6 @@ "navigationBarTitleText": "scroll-view自定义滚动吸顶" } }, - { - "path": "pages/API/get-battery-info/get-battery-info", - "style": { - "navigationBarTitleText": "电量" - } - }, { "path": "pages/CSS/border/complex-border/complex-border", "style": { @@ -926,12 +979,6 @@ "navigationBarTitleText": "自定义下拉刷新" } }, - { - "path": "pages/API/get-window-info/get-window-info", - "style": { - "navigationBarTitleText": "get-window-info" - } - }, { "path": "pages/template/half-screen/half-screen", "style": { @@ -982,18 +1029,6 @@ "navigationStyle": "custom" } }, - { - "path": "pages/API/unicloud-file-api/unicloud-file-api", - "style": { - "navigationBarTitleText": "" - } - }, - { - "path": "pages/API/unicloud-database/unicloud-database", - "style": { - "navigationBarTitleText": "" - } - }, { "path": "pages/template/navbar-lite/navbar-lite", "style": { @@ -1007,34 +1042,13 @@ "navigationBarTitleText": "自定义TabBar" } }, - { - "path": "pages/API/facial-recognition-verify/facial-recognition-verify", - "style": { - "navigationBarTitleText": "", - "enablePullDownRefresh": false - } - }, { "path": "pages/component/sticky-header/sticky-header", "style": { "navigationBarTitleText": "sticky-header", "enablePullDownRefresh": false } - }, - { - "path": "pages/API/element-takesnapshot/element-takesnapshot", - "style": { - "navigationBarTitleText": "takeSnapshot", - "enablePullDownRefresh": false - } - }, - { - "path": "pages/API/element-draw/element-draw", - "style": { - "navigationBarTitleText": "getDrawableContext", - "enablePullDownRefresh": false - } - }, + }, { "path": "pages/component/general-event/transition-event", "style": { @@ -1042,13 +1056,6 @@ "enablePullDownRefresh": false } }, - { - "path": "pages/API/install-apk/install-apk", - "style": { - "navigationBarTitleText": "install-apk", - "enablePullDownRefresh": false - } - }, { "path": "pages/template/schema/schema", "style": { @@ -1084,7 +1091,6 @@ "enablePullDownRefresh" : false } } - ], "globalStyle": { "pageOrientation": "portrait", @@ -1135,4 +1141,4 @@ "query": "" //启动参数,在页面的onLoad函数里面得到 }] } -} \ No newline at end of file +} diff --git a/pages/API/globalProperties/globalProperties.test.js b/pages/API/globalProperties/globalProperties.test.js new file mode 100644 index 00000000..e116b502 --- /dev/null +++ b/pages/API/globalProperties/globalProperties.test.js @@ -0,0 +1,36 @@ +const PAGE_PATH = '/pages/API/globalProperties/globalProperties' + +describe('globalProperties', () => { + let page = null + beforeAll(async () => { + page = await program.navigateTo(PAGE_PATH) + await page.waitFor(500) + }) + it('globalProperties', async () => { + let data = await page.data() + expect(data.myGlobalProperties.str).toBe('default string') + expect(data.myGlobalProperties.num).toBe(0) + expect(data.myGlobalProperties.bool).toBe(false) + expect(data.myGlobalProperties.obj).toEqual({bool: false, num: 0, str: 'default globalProperties obj string'}) + expect(data.myGlobalProperties.arr).toEqual([]) + expect(data.myGlobalProperties.set).toEqual([]) + expect(data.myGlobalProperties.map).toEqual({}) + expect(data.myGlobalProperties.reactiveObj).toEqual({str: 'default reactive string', num: 0, bool: false}) + expect(data.globalPropertiesFnRes).toBe('globalPropertiesStr: default string, globalPropertiesNum: 0') + await page.callMethod('updateGlobalProperties') + data = await page.data() + expect(data.myGlobalProperties.str).toBe('new string') + expect(data.myGlobalProperties.num).toBe(100) + expect(data.myGlobalProperties.bool).toBe(true) + expect(data.myGlobalProperties.obj).toEqual({bool: true, num: 100, str: 'new globalProperties obj string'}) + expect(data.myGlobalProperties.arr).toEqual([1,2,3]) + expect(data.myGlobalProperties.set).toEqual(['a', 'b', 'c']) + expect(data.myGlobalProperties.map).toEqual({'a': 1, 'b': 2, 'c': 3}) + expect(data.myGlobalProperties.reactiveObj).toEqual({str: 'new reactive string', num: 200, bool: true}) + expect(data.globalPropertiesFnRes).toBe('globalPropertiesStr: new string, globalPropertiesNum: 100') + }) + it('screenshot', async () => { + const image = await program.screenshot({fullPage: true}); + expect(image).toMatchImageSnapshot(); + }) +}) diff --git a/pages/API/globalProperties/globalProperties.uvue b/pages/API/globalProperties/globalProperties.uvue new file mode 100644 index 00000000..850cead3 --- /dev/null +++ b/pages/API/globalProperties/globalProperties.uvue @@ -0,0 +1,98 @@ + + + diff --git a/pages/tabBar/API.uvue b/pages/tabBar/API.uvue index a002cb77..9f930554 100644 --- a/pages/tabBar/API.uvue +++ b/pages/tabBar/API.uvue @@ -63,9 +63,13 @@ name: 'getApp', url: 'get-app', }, - { - name: 'getCurrentPages', - url: 'get-current-pages', + { + name: 'getCurrentPages', + url: 'get-current-pages', + }, + { + name: 'globalProperties', + url: 'globalProperties', } ] as Page[], }, -- GitLab