提交 d507a19b 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

test(reactivity watch): 优化 watch 相关测试

上级 886f8c9e
......@@ -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')
......
......@@ -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)
......
......@@ -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')
......
......@@ -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)
......
......@@ -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]')
})
......
......@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册
新手
引导
客服 返回
顶部