const PAGE_PATH = '/pages/composition-api/reactivity/watch-post-effect/watch-post-effect' describe('watchPostEffect', () => { const isWeb = process.env.uniTestPlatformInfo.startsWith('web') let page = null beforeAll(async () => { page = await program.reLaunch(PAGE_PATH) await page.waitFor('view') }) it('count', async () => { const count = await page.$('#count') expect(await count.text()).toBe('count: 0') // watch const watchCountRes = await page.$('#watch-count-res') expect(await watchCountRes.text()).toBe( 'watch count result: count: 0, count ref text: count: 0') // track const watchCountTrackNum = await page.$('#watch-count-track-num') if (process.env.uniTestPlatformInfo.startsWith('android')) { expect(await watchCountTrackNum.text()).toBe('watch count track number: 3') } else if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) { // TODO: 确认 IOS 的差异是否正常 expect(await watchCountTrackNum.text()).toBe('watch count track number: 8') } else { expect(await watchCountTrackNum.text()).toBe('watch count track number: 3') } const watchCountCleanupRes = await page.$('#watch-count-cleanup-res') // TODO web端自动化测试text方法应使用textContent获取内容来保留空格,目前text方法未保留首尾空格 expect(await watchCountCleanupRes.text()).toBe(isWeb ? 'watch count cleanup result:' : 'watch count cleanup result: ') // watch count and obj.num const watchCountAndObjNumRes = await page.$('#watch-count-obj-num-res') expect(await watchCountAndObjNumRes.text()).toBe('watch count and obj.num result: count: 0, obj.num: 0') const incrementBtn = await page.$('#increment-btn') await incrementBtn.tap() expect(await count.text()).toBe('count: 1') expect(await watchCountRes.text()).toBe( 'watch count result: count: 1, count ref text: count: 1') if (process.env.uniTestPlatformInfo.startsWith('android')) { expect(await watchCountTrackNum.text()).toBe('watch count track number: 3') } else if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) { expect(await watchCountTrackNum.text()).toBe('watch count track number: 16') } else { expect(await watchCountTrackNum.text()).toBe('watch count track number: 6') } expect(await watchCountCleanupRes.text()).toBe('watch count cleanup result: watch count cleanup: 1') expect(await watchCountAndObjNumRes.text()).toBe('watch count and obj.num result: count: 1, obj.num: 0') await incrementBtn.tap() expect(await count.text()).toBe('count: 2') expect(await watchCountRes.text()).toBe( 'watch count result: count: 2, count ref text: count: 2') if (process.env.uniTestPlatformInfo.startsWith('android')) { expect(await watchCountTrackNum.text()).toBe('watch count track number: 3') } else if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) { expect(await watchCountTrackNum.text()).toBe('watch count track number: 24') } else { expect(await watchCountTrackNum.text()).toBe('watch count track number: 9') } expect(await watchCountCleanupRes.text()).toBe('watch count cleanup result: watch count cleanup: 2') expect(await watchCountAndObjNumRes.text()).toBe('watch count and obj.num result: count: 2, obj.num: 0') // stop watch const stopWatchCountBtn = await page.$('#stop-watch-count-btn') await stopWatchCountBtn.tap() await incrementBtn.tap() expect(await count.text()).toBe('count: 3') expect(await watchCountRes.text()).toBe( 'watch count result: count: 2, count ref text: count: 2') if (process.env.uniTestPlatformInfo.startsWith('android')) { expect(await watchCountTrackNum.text()).toBe('watch count track number: 3') } else if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) { expect(await watchCountTrackNum.text()).toBe('watch count track number: 24') } else { expect(await watchCountTrackNum.text()).toBe('watch count track number: 9') } expect(await watchCountCleanupRes.text()).toBe('watch count cleanup result: watch count cleanup: 2') expect(await watchCountAndObjNumRes.text()).toBe('watch count and obj.num result: count: 3, obj.num: 0') }) it('obj', async () => { const objStr = await page.$('#obj-str') expect(await objStr.text()).toBe('obj.str: num: 0') const objNum = await page.$('#obj-num') expect(await objNum.text()).toBe('obj.num: 0') const objBool = await page.$('#obj-bool') expect(await objBool.text()).toBe('obj.bool: false') const objArr = await page.$('#obj-arr') expect(await objArr.text()).toBe('obj.arr: [0]') const watchObjRes = await page.$('#watch-obj-res') // web端和安卓端JSON.stringify对属性的排序不一致 // 该用字符串特征判断和字符串 JSON 解析 const watchObjRestRaw = await watchObjRes.text() expect(watchObjRestRaw.startsWith('watch obj result: obj:')).toBe(true) const stringJSON = watchObjRestRaw.split('watch obj result: obj:') expect(Array.isArray(stringJSON)).toBe(true) expect(stringJSON.length).toBe(2) expect(JSON.parse(stringJSON[1])).toEqual({ "num": 0, "str": "num: 0", "bool": false, "arr": [0] }) const watchObjStrRes = await page.$('#watch-obj-str-res') expect(await watchObjStrRes.text()).toBe( 'watch obj.str result: str: num: 0, obj.str ref text: obj.str: num: 0') // trigger const watchObjStrTriggerNum = await page.$('#watch-obj-str-trigger-num') expect(await watchObjStrTriggerNum.text()).toBe('watch obj.str trigger number: 0') const watchObjArrRes = await page.$('#watch-obj-arr-res') expect(await watchObjArrRes.text()).toBe('watch obj.arr result: arr: [0]') const updateObjBtn = await page.$('#update-obj-btn') await updateObjBtn.tap() expect(await objStr.text()).toBe('obj.str: num: 1') expect(await objNum.text()).toBe('obj.num: 1') expect(await objBool.text()).toBe('obj.bool: true') expect(await objArr.text()).toBe('obj.arr: [0,1]') const watchObjRestRaw2 = await watchObjRes.text() expect(watchObjRestRaw2.startsWith('watch obj result: obj:')).toBe(true) const stringJSON2 = watchObjRestRaw2.split('watch obj result: obj:') expect(Array.isArray(stringJSON2)).toBe(true) expect(stringJSON2.length).toBe(2) expect(JSON.parse(stringJSON2[1])).toEqual({ "num": 1, "str": "num: 1", "bool": true, "arr": [0, 1] }) expect(await watchObjStrRes.text()).toBe( 'watch obj.str result: str: num: 1, obj.str ref text: obj.str: num: 1') expect(await watchObjStrTriggerNum.text()).toBe('watch obj.str trigger number: 1') expect(await watchObjArrRes.text()).toBe( 'watch obj.arr result: arr: [0,1]') const watchCountAndObjNumRes = await page.$('#watch-count-obj-num-res') expect(await watchCountAndObjNumRes.text()).toBe('watch count and obj.num result: count: 3, obj.num: 1') }) })