From fb22177ed93382c7ec16c76f7006d16b1a69fbc2 Mon Sep 17 00:00:00 2001
From: zhenyuWang <13641039885@163.com>
Date: Fri, 22 Dec 2023 18:49:31 +0800
Subject: [PATCH] feat: globalData & globalProperties
---
App.uvue | 309 +++++++++---------
main.uts | 22 ++
pages.json | 12 +
.../globalData/globalData.test.js | 39 +++
pages/app-instance/globalData/globalData.uvue | 167 ++++++++++
.../globalProperties/globalProperties.test.js | 59 ++++
.../globalProperties/globalProperties.uvue | 110 +++++++
pages/index.uvue | 20 +-
8 files changed, 587 insertions(+), 151 deletions(-)
create mode 100644 pages/app-instance/globalData/globalData.test.js
create mode 100644 pages/app-instance/globalData/globalData.uvue
create mode 100644 pages/app-instance/globalProperties/globalProperties.test.js
create mode 100644 pages/app-instance/globalProperties/globalProperties.uvue
diff --git a/App.uvue b/App.uvue
index 4526ab5..5ea58c1 100644
--- a/App.uvue
+++ b/App.uvue
@@ -1,157 +1,174 @@
\ No newline at end of file
diff --git a/main.uts b/main.uts
index 83a1a18..0ee20ce 100644
--- a/main.uts
+++ b/main.uts
@@ -276,6 +276,28 @@ export function createApp() {
}, 'CompForPlugin', CompForPlugin)
// #endif
+ 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,
+ } as UTSJSONObject)
+ app.config.globalProperties.globalPropertiesFn = function (): 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 184c30b..a5eb2a6 100644
--- a/pages.json
+++ b/pages.json
@@ -12,6 +12,18 @@
"navigationBarTitleText": "app instance"
}
},
+ {
+ "path": "pages/app-instance/globalData/globalData",
+ "style": {
+ "navigationBarTitleText": "globalData"
+ }
+ },
+ {
+ "path": "pages/app-instance/globalProperties/globalProperties",
+ "style": {
+ "navigationBarTitleText": "globalProperties"
+ }
+ },
{
"path": "pages/built-in-component/keep-alive/keep-alive",
"style": {
diff --git a/pages/app-instance/globalData/globalData.test.js b/pages/app-instance/globalData/globalData.test.js
new file mode 100644
index 0000000..4a4459c
--- /dev/null
+++ b/pages/app-instance/globalData/globalData.test.js
@@ -0,0 +1,39 @@
+const PAGE_PATH = '/pages/app-instance/globalData/globalData'
+
+describe('globalData', () => {
+ let page = null
+ beforeAll(async () => {
+ 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')
+ })
+})
diff --git a/pages/app-instance/globalData/globalData.uvue b/pages/app-instance/globalData/globalData.uvue
new file mode 100644
index 0000000..9ee9372
--- /dev/null
+++ b/pages/app-instance/globalData/globalData.uvue
@@ -0,0 +1,167 @@
+
+
+
+
+
+
+
+
+ 初始的 globalData:
+ globalData string: {{ originGlobalData.str }}
+ globalData number: {{ originGlobalData.num }}
+ globalData boolean: {{ originGlobalData.bool }}
+ globalData object: {{ originGlobalData.obj }}
+ globalData null: {{ originGlobalData.null }}
+ globalData array: {{ originGlobalData.arr }}
+ globalData Set: {{ originGlobalData.mySet }}
+ globalData Map: {{ originGlobalData.myMap }}
+ globalData func 返回值: {{ originGlobalDataFuncRes }}
+
+
+
+ 更新后的 globalData:
+ globalData string: {{ newGlobalData.str }}
+ globalData number: {{ newGlobalData.num }}
+ globalData boolean: {{ newGlobalData.bool }}
+ globalData object: {{ newGlobalData.obj }}
+ globalData null: {{ newGlobalData.null }}
+ globalData array: {{ newGlobalData.arr }}
+ globalData Set: {{ newGlobalData.mySet }}
+ globalData Map: {{ newGlobalData.myMap }}
+ globalData func 返回值: {{ newGlobalDataFuncRes }}
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pages/app-instance/globalProperties/globalProperties.test.js b/pages/app-instance/globalProperties/globalProperties.test.js
new file mode 100644
index 0000000..bdc73f0
--- /dev/null
+++ b/pages/app-instance/globalProperties/globalProperties.test.js
@@ -0,0 +1,59 @@
+const PAGE_PATH = '/pages/app-instance/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 () => {
+ await page.waitFor(500)
+ const image = await program.screenshot({
+ fullPage: true
+ });
+ expect(image).toMatchImageSnapshot();
+ })
+})
\ No newline at end of file
diff --git a/pages/app-instance/globalProperties/globalProperties.uvue b/pages/app-instance/globalProperties/globalProperties.uvue
new file mode 100644
index 0000000..3b1c7b7
--- /dev/null
+++ b/pages/app-instance/globalProperties/globalProperties.uvue
@@ -0,0 +1,110 @@
+
+
+
+
+
+
+ globalProperties string: {{ globalPropertiesStr }}
+ globalProperties number: {{ globalPropertiesNum }}
+ globalProperties boolean: {{ globalPropertiesBool }}
+ globalProperties object: {{ globalPropertiesObj }}
+ globalProperties null: {{ globalPropertiesNull }}
+ globalProperties array: {{ globalPropertiesArr }}
+ globalProperties set: {{ globalPropertiesSet }}
+ globalProperties map: {{ globalPropertiesMap }}
+ globalProperties reactiveObj.str: {{ globalPropertiesReactiveObj['str'] }}
+ globalProperties reactiveObj.num: {{ globalPropertiesReactiveObj['num'] }}
+ globalProperties reactiveObj.boolean:
+ {{ globalPropertiesReactiveObj['bool'] }}
+ globalProperties fun 返回值: {{ globalPropertiesFn() }}
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pages/index.uvue b/pages/index.uvue
index 412ce15..d31b27b 100644
--- a/pages/index.uvue
+++ b/pages/index.uvue
@@ -61,6 +61,16 @@
url: 'index',
enable: true,
},
+ {
+ name: 'globalData',
+ url: 'globalData',
+ enable: true,
+ },
+ {
+ name: 'globalProperties',
+ url: 'globalProperties',
+ enable: true,
+ },
] as PageItem[],
},
{
@@ -415,11 +425,11 @@
enable: false,
// #endif
},
- {
- name: 'extends',
- url: 'extends',
- enable: false,
- },
+ {
+ name: 'extends',
+ url: 'extends',
+ enable: false,
+ }
] as PageItem[],
},
{
--
GitLab