From d507a19b0ff5c5317954f60df22bfa6cfc0f2a32 Mon Sep 17 00:00:00 2001
From: zhenyuWang <13641039885@163.com>
Date: Sun, 26 May 2024 19:49:41 +0800
Subject: [PATCH] =?UTF-8?q?test(reactivity=20watch):=20=E4=BC=98=E5=8C=96?=
 =?UTF-8?q?=20watch=20=E7=9B=B8=E5=85=B3=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../watch-post-effect.test.js                 | 24 +++-----------
 .../watch-post-effect/watch-post-effect.uvue  |  2 +-
 .../watch-sync-effect.test.js                 | 22 ++-----------
 .../watch-sync-effect/watch-sync-effect.uvue  |  2 +-
 .../core/watch/watch-options.test.js          | 33 ++++++++-----------
 .../reactivity/core/watch/watch-options.uvue  |  6 +++-
 6 files changed, 28 insertions(+), 61 deletions(-)

diff --git a/pages/reactivity/core/watch-post-effect/watch-post-effect.test.js b/pages/reactivity/core/watch-post-effect/watch-post-effect.test.js
index f31b027..9153118 100644
--- a/pages/reactivity/core/watch-post-effect/watch-post-effect.test.js
+++ b/pages/reactivity/core/watch-post-effect/watch-post-effect.test.js
@@ -6,7 +6,7 @@ describe('watchPostEffect', () => {
   const isAndroid = platformInfo.startsWith('android')
   const isIos = platformInfo.startsWith('ios')
   const isWeb = platformInfo.startsWith('web')
-  
+
   beforeAll(async () => {
     page = await program.reLaunch(PAGE_PATH)
     await page.waitFor('view')
@@ -113,16 +113,8 @@ describe('watchPostEffect', () => {
     expect(await objArr.text()).toBe('[0]')
 
     const watchObjRes = await page.$('#watch-obj-res')
-    // TODO web端和安卓端JSON.stringify对属性的排序不一致
-    if (process.env.uniTestPlatformInfo.startsWith('web') || isIos) {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"num":0,"str":"num: 0","bool":false,"arr":[0]}'
-      )
-    } else {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"arr":[0],"bool":false,"num":0,"str":"num: 0"}'
-      )
-    }
+    expect(await watchObjRes.text()).toBe('obj: {"num":0,"str":"num: 0","bool":false,"arr":[0]}')
+
     const watchObjStrRes = await page.$('#watch-obj-str-res')
     expect(await watchObjStrRes.text()).toBe(
       'str: num: 0, obj.str ref text: num: 0')
@@ -142,15 +134,7 @@ describe('watchPostEffect', () => {
     expect(await objBool.text()).toBe('true')
     expect(await objArr.text()).toBe('[0,1]')
 
-    if (process.env.uniTestPlatformInfo.startsWith('web') || isIos) {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}'
-      )
-    } else {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"arr":[0,1],"bool":true,"num":1,"str":"num: 1"}'
-      )
-    }
+    expect(await watchObjRes.text()).toBe('obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}')
     expect(await watchObjStrRes.text()).toBe(
       'str: num: 1, obj.str ref text: num: 1')
 
diff --git a/pages/reactivity/core/watch-post-effect/watch-post-effect.uvue b/pages/reactivity/core/watch-post-effect/watch-post-effect.uvue
index b63bc45..7d9cbbd 100644
--- a/pages/reactivity/core/watch-post-effect/watch-post-effect.uvue
+++ b/pages/reactivity/core/watch-post-effect/watch-post-effect.uvue
@@ -127,7 +127,7 @@
 
   const watchObjRes = ref('')
   watchPostEffect(() => {
-    watchObjRes.value = `obj: ${JSON.stringify(obj)}`
+    watchObjRes.value = `obj: {"num":${obj.num},"str":"${obj.str}","bool":${obj.bool},"arr":${JSON.stringify(obj.arr)}}`
   })
 
   const objStrRef = ref<UniTextElement | null>(null)
diff --git a/pages/reactivity/core/watch-sync-effect/watch-sync-effect.test.js b/pages/reactivity/core/watch-sync-effect/watch-sync-effect.test.js
index 09bda00..4605ad9 100644
--- a/pages/reactivity/core/watch-sync-effect/watch-sync-effect.test.js
+++ b/pages/reactivity/core/watch-sync-effect/watch-sync-effect.test.js
@@ -6,7 +6,7 @@ describe('watchSyncEffect', () => {
   const isAndroid = platformInfo.startsWith('android')
   const isIos = platformInfo.startsWith('ios')
   const isWeb = platformInfo.startsWith('web')
-  
+
   beforeAll(async () => {
     page = await program.reLaunch(PAGE_PATH)
     await page.waitFor('view')
@@ -111,15 +111,7 @@ describe('watchSyncEffect', () => {
     expect(await objArr.text()).toBe('[0]')
 
     const watchObjRes = await page.$('#watch-obj-res')
-    if (isWeb || isIos) {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"num":0,"str":"num: 0","bool":false,"arr":[0]}'
-      )
-    } else {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"arr":[0],"bool":false,"num":0,"str":"num: 0"}'
-      )
-    }
+    expect(await watchObjRes.text()).toBe('obj: {"num":0,"str":"num: 0","bool":false,"arr":[0]}')
 
     const watchObjStrRes = await page.$('#watch-obj-str-res')
     expect(await watchObjStrRes.text()).toBe(
@@ -140,15 +132,7 @@ describe('watchSyncEffect', () => {
     expect(await objBool.text()).toBe('true')
     expect(await objArr.text()).toBe('[0,1]')
 
-    if (isWeb || isIos) {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}'
-      )
-    } else {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"arr":[0,1],"bool":true,"num":1,"str":"num: 1"}'
-      )
-    }
+    expect(await watchObjRes.text()).toBe('obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}')
 
     expect(await watchObjStrRes.text()).toBe(
       'str: num: 1, obj.str ref text: num: 0')
diff --git a/pages/reactivity/core/watch-sync-effect/watch-sync-effect.uvue b/pages/reactivity/core/watch-sync-effect/watch-sync-effect.uvue
index 88d2fd0..7c21482 100644
--- a/pages/reactivity/core/watch-sync-effect/watch-sync-effect.uvue
+++ b/pages/reactivity/core/watch-sync-effect/watch-sync-effect.uvue
@@ -127,7 +127,7 @@
 
   const watchObjRes = ref('')
   watchSyncEffect(() => {
-    watchObjRes.value = `obj: ${JSON.stringify(obj)}`
+    watchObjRes.value = `obj: {"num":${obj.num},"str":"${obj.str}","bool":${obj.bool},"arr":${JSON.stringify(obj.arr)}}`
   })
 
   const objStrRef = ref<UniTextElement | null>(null)
diff --git a/pages/reactivity/core/watch/watch-options.test.js b/pages/reactivity/core/watch/watch-options.test.js
index 99969ef..49be8cb 100644
--- a/pages/reactivity/core/watch/watch-options.test.js
+++ b/pages/reactivity/core/watch/watch-options.test.js
@@ -36,7 +36,7 @@ describe('watch', () => {
     expect(await count.text()).toBe('1')
 
     expect(await watchCountRes.text()).toBe(
-      'count: 1, prevCount: 0, count ref text (flush sync): 0')
+      `count: 1, prevCount: 0, count ref text (flush sync): ${isWeb? 'null': '0'}`)
 
     if (isAndroid) {
       expect(await watchCountTrackNum.text()).toBe('1')
@@ -49,12 +49,13 @@ describe('watch', () => {
     await incrementBtn.tap()
 
     expect(await count.text()).toBe('2')
-    expect(await watchCountRes.text()).toBe('count: 2, prevCount: 1, count ref text (flush sync): 1')
+    expect(await watchCountRes.text()).toBe(
+      `count: 2, prevCount: 1, count ref text (flush sync): ${isWeb?'null': '1'}`)
 
     if (isAndroid) {
       expect(await watchCountTrackNum.text()).toBe('1')
     }
-    
+
     if (isIos || isWeb) {
       expect(await watchCountTrackNum.text()).toBe('3')
     }
@@ -74,12 +75,12 @@ describe('watch', () => {
     await incrementBtn.tap()
 
     expect(await count.text()).toBe('3')
-    
+
     if (isApp) {
       expect(await watchCountRes.text()).toBe('count: 3, prevCount: 2, count ref text (flush sync): 2')
     }
     if (isWeb) {
-      expect(await watchCountRes.text()).toBe('count: 2, prevCount: 1, count ref text (flush sync): 1')
+      expect(await watchCountRes.text()).toBe('count: 2, prevCount: 1, count ref text (flush sync): null')
     }
 
     if (isAndroid) {
@@ -91,7 +92,7 @@ describe('watch', () => {
     if (isWeb) {
       expect(await watchCountTrackNum.text()).toBe('3')
     }
-    
+
     expect(await watchCountCleanupRes.text()).toBe('watch count cleanup: 2')
   })
 
@@ -108,7 +109,7 @@ describe('watch', () => {
     const watchObjRes = await page.$('#watch-obj-res')
     if (isAndroid) {
       expect(await watchObjRes.text()).toBe(
-        'obj: {"arr":[0],"bool":false,"num":0,"str":"num: 0"}, prevObj: {"arr":[0],"bool":false,"num":0,"str":"num: 0"}'
+        'obj: {"num":0,"str":"num: 0","bool":false,"arr":[0]}, prevObj: {"num":0,"str":"num: 0","bool":false,"arr":[0]}'
       )
     }
     if (isIos || isWeb) {
@@ -136,23 +137,17 @@ describe('watch', () => {
     expect(await objBool.text()).toBe('true')
     expect(await objArr.text()).toBe('[0,1]')
 
-    if (isAndroid) {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"arr":[0,1],"bool":true,"num":1,"str":"num: 1"}, prevObj: {"arr":[0,1],"bool":true,"num":1,"str":"num: 1"}'
-      )
-    }
-    if (isIos || isWeb) {
-      expect(await watchObjRes.text()).toBe(
-        'obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}, prevObj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}'
-      )
-    }
+
+    expect(await watchObjRes.text()).toBe(
+      'obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}, prevObj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}'
+    )
     expect(await watchObjStrRes.text()).toBe(
-      'str: num: 1, prevStr: num: 0, obj.str ref text (flush pre): num: 0')
+      `str: num: 1, prevStr: num: 0, obj.str ref text (flush pre): ${isWeb?'null': 'num: 0'}`)
 
     expect(await watchObjStrTriggerNum.text()).toBe('0')
 
     expect(await watchObjBoolRes.text()).toBe(
-      'bool: true, prevBool: false, obj.bool ref text (flush post): true'
+      `bool: true, prevBool: false, obj.bool ref text (flush post): ${isWeb?'null':'true'}`
     )
     expect(await watchObjArrRes.text()).toBe('arr: [0,1], prevArr: [0,1]')
   })
diff --git a/pages/reactivity/core/watch/watch-options.uvue b/pages/reactivity/core/watch/watch-options.uvue
index 9bb77d7..2caa7dd 100644
--- a/pages/reactivity/core/watch/watch-options.uvue
+++ b/pages/reactivity/core/watch/watch-options.uvue
@@ -156,7 +156,11 @@
     watch: {
       obj: {
         handler(obj : Obj, prevObj ?: Obj) {
-          this.watchObjRes = `obj: ${JSON.stringify(obj)}, prevObj: ${JSON.stringify(prevObj)}`
+          if (prevObj === null) {
+            this.watchObjRes = `obj: {"num":${obj.num},"str":"${obj.str}","bool":${obj.bool},"arr":${JSON.stringify(obj.arr)}}, prevObj: ${JSON.stringify(prevObj)}`
+          } else {
+            this.watchObjRes = `obj: {"num":${obj.num},"str":"${obj.str}","bool":${obj.bool},"arr":${JSON.stringify(obj.arr)}}, prevObj: {"num":${prevObj?.num},"str":"${prevObj?.str}","bool":${prevObj?.bool},"arr":${JSON.stringify(prevObj?.arr)}}`
+          }
         },
         // immediate: true 第一次触发, 旧值应该是 undefined, 现在 app 是初始值
         immediate: true,
-- 
GitLab