watch-sync-effect.test.js 6.9 KB
Newer Older
1 2
const PAGE_PATH = '/pages/composition-api/reactivity/watch-sync-effect/watch-sync-effect'

3
describe('watchSyncEffect', () => {
雪洛's avatar
雪洛 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  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')
20 21
    if (process.env.uniTestPlatformInfo.startsWith('android')) {
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 3')
22
    } else if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) {
23 24 25 26 27
      // TODO: 确认 IOS 的差异是否正常
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 11')
    } else {
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 6')
    }
雪洛's avatar
雪洛 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41

    const watchCountCleanupRes = await page.$('#watch-count-cleanup-res')
    expect(await watchCountCleanupRes.text()).toBe('watch count cleanup result: watch count cleanup: 0')

    // 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: 0')
42 43 44

    if (process.env.uniTestPlatformInfo.startsWith('android')) {
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 3')
45
    } else if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) {
46 47 48 49 50 51
      // TODO: 确认 IOS 的差异是否正常
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 19')
    } else {
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 9')
    }

雪洛's avatar
雪洛 已提交
52 53 54 55 56 57 58 59 60
    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: 1')
61 62 63

    if (process.env.uniTestPlatformInfo.startsWith('android')) {
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 3')
64
    } else if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) {
65 66 67 68 69 70
      // TODO: 确认 IOS 的差异是否正常
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 27')
    } else {
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 12')
    }

雪洛's avatar
雪洛 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83
    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: 1')
84 85 86

    if (process.env.uniTestPlatformInfo.startsWith('android')) {
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 3')
87
    } else if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) {
88 89 90 91 92 93
      // TODO: 确认 IOS 的差异是否正常
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 27')
    } else {
      expect(await watchCountTrackNum.text()).toBe('watch count track number: 12')
    }

雪洛's avatar
雪洛 已提交
94 95 96 97 98 99 100 101 102 103 104 105
    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')
106
    expect(await objArr.text()).toBe('obj.arr: [0]')
雪洛's avatar
雪洛 已提交
107 108

    const watchObjRes = await page.$('#watch-obj-res')
109 110 111 112 113 114 115 116 117 118 119
    if (process.env.uniTestPlatformInfo.startsWith('web') || process.env.uniTestPlatformInfo.startsWith(
        'IOS')) {
      expect(await watchObjRes.text()).toBe(
        'watch obj result: obj: {"num":0,"str":"num: 0","bool":false,"arr":[0]}'
      )
    } else {
      expect(await watchObjRes.text()).toBe(
        'watch obj result: obj: {"arr":[0],"bool":false,"num":0,"str":"num: 0"}'
      )
    }

雪洛's avatar
雪洛 已提交
120 121 122
    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')
123 124 125 126 127

    // trigger
    const watchObjStrTriggerNum = await page.$('#watch-obj-str-trigger-num')
    expect(await watchObjStrTriggerNum.text()).toBe('watch obj.str trigger number: 1')

雪洛's avatar
雪洛 已提交
128 129 130 131 132 133 134 135 136
    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')
137 138 139 140 141 142 143 144 145 146 147 148
    expect(await objArr.text()).toBe('obj.arr: [0,1]')

    if (process.env.uniTestPlatformInfo.startsWith('web') || process.env.uniTestPlatformInfo.startsWith(
        'IOS')) {
      expect(await watchObjRes.text()).toBe(
        'watch obj result: obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}'
      )
    } else {
      expect(await watchObjRes.text()).toBe(
        'watch obj result: obj: {"arr":[0],"bool":false,"num":0,"str":"num: 0"}'
      )
    }
雪洛's avatar
雪洛 已提交
149 150 151

    expect(await watchObjStrRes.text()).toBe(
      'watch obj.str result: str: num: 1, obj.str ref text: obj.str: num: 0')
152 153 154

    expect(await watchObjStrTriggerNum.text()).toBe('watch obj.str trigger number: 2')

雪洛's avatar
雪洛 已提交
155 156 157 158 159
    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')
  })
160
})