watch-post-effect.test.js 4.7 KB
Newer Older
1
const PAGE_PATH = '/pages/reactivity/core/watch-post-effect/watch-post-effect'
2

3
describe('watchPostEffect', () => {
雪洛's avatar
雪洛 已提交
4
  let page = null
5 6 7 8
  const platformInfo = process.env.uniTestPlatformInfo.toLowerCase()
  const isAndroid = platformInfo.startsWith('android')
  const isIos = platformInfo.startsWith('ios')
  const isWeb = platformInfo.startsWith('web')
9

雪洛's avatar
雪洛 已提交
10 11 12 13 14 15
  beforeAll(async () => {
    page = await program.reLaunch(PAGE_PATH)
    await page.waitFor('view')
  })
  it('count', async () => {
    const count = await page.$('#count')
16
    expect(await count.text()).toBe('0')
雪洛's avatar
雪洛 已提交
17 18 19 20

    // watch
    const watchCountRes = await page.$('#watch-count-res')
    expect(await watchCountRes.text()).toBe(
21
      'count: 0, count ref text: 0')
雪洛's avatar
雪洛 已提交
22 23 24

    // track
    const watchCountTrackNum = await page.$('#watch-count-track-num')
25 26
    if (isAndroid) {
      expect(await watchCountTrackNum.text()).toBe('3')
27
    } else {
28
      expect(await watchCountTrackNum.text()).toBe('3')
29
    }
雪洛's avatar
雪洛 已提交
30 31

    const watchCountCleanupRes = await page.$('#watch-count-cleanup-res')
32 33 34 35 36 37
    if (isAndroid || isWeb) {
      expect(await watchCountCleanupRes.text()).toBe('')
    }
    if (isIos) {
      expect(await watchCountCleanupRes.text()).toBe(null)
    }
雪洛's avatar
雪洛 已提交
38 39 40

    // watch count and obj.num
    const watchCountAndObjNumRes = await page.$('#watch-count-obj-num-res')
41
    expect(await watchCountAndObjNumRes.text()).toBe('count: 0, obj.num: 0')
雪洛's avatar
雪洛 已提交
42

43
    const incrementBtn = await page.$('.increment-btn')
雪洛's avatar
雪洛 已提交
44 45
    await incrementBtn.tap()

46
    expect(await count.text()).toBe('1')
雪洛's avatar
雪洛 已提交
47
    expect(await watchCountRes.text()).toBe(
48
      'count: 1, count ref text: 1')
49

50 51
    if (isAndroid) {
      expect(await watchCountTrackNum.text()).toBe('3')
52
    } else {
53
      expect(await watchCountTrackNum.text()).toBe('6')
54 55
    }

56
    expect(await watchCountCleanupRes.text()).toBe('watch count cleanup: 1')
雪洛's avatar
雪洛 已提交
57

58
    expect(await watchCountAndObjNumRes.text()).toBe('count: 1, obj.num: 0')
雪洛's avatar
雪洛 已提交
59 60 61

    await incrementBtn.tap()

62
    expect(await count.text()).toBe('2')
雪洛's avatar
雪洛 已提交
63
    expect(await watchCountRes.text()).toBe(
64
      'count: 2, count ref text: 2')
65

66 67
    if (isAndroid) {
      expect(await watchCountTrackNum.text()).toBe('3')
68
    } else {
69
      expect(await watchCountTrackNum.text()).toBe('9')
70 71
    }

72
    expect(await watchCountCleanupRes.text()).toBe('watch count cleanup: 2')
雪洛's avatar
雪洛 已提交
73

74
    expect(await watchCountAndObjNumRes.text()).toBe('count: 2, obj.num: 0')
雪洛's avatar
雪洛 已提交
75 76

    // stop watch
77
    const stopWatchCountBtn = await page.$('.stop-watch-count-btn')
雪洛's avatar
雪洛 已提交
78 79 80 81
    await stopWatchCountBtn.tap()

    await incrementBtn.tap()

82
    expect(await count.text()).toBe('3')
雪洛's avatar
雪洛 已提交
83
    expect(await watchCountRes.text()).toBe(
84
      'count: 2, count ref text: 2')
85

86 87
    if (isAndroid) {
      expect(await watchCountTrackNum.text()).toBe('3')
88
    } else {
89
      expect(await watchCountTrackNum.text()).toBe('9')
90 91
    }

92
    expect(await watchCountCleanupRes.text()).toBe('watch count cleanup: 2')
雪洛's avatar
雪洛 已提交
93

94
    expect(await watchCountAndObjNumRes.text()).toBe('count: 3, obj.num: 0')
雪洛's avatar
雪洛 已提交
95 96 97
  })
  it('obj', async () => {
    const objStr = await page.$('#obj-str')
98
    expect(await objStr.text()).toBe('num: 0')
雪洛's avatar
雪洛 已提交
99
    const objNum = await page.$('#obj-num')
100
    expect(await objNum.text()).toBe('0')
雪洛's avatar
雪洛 已提交
101
    const objBool = await page.$('#obj-bool')
102
    expect(await objBool.text()).toBe('false')
雪洛's avatar
雪洛 已提交
103
    const objArr = await page.$('#obj-arr')
104
    expect(await objArr.text()).toBe('[0]')
雪洛's avatar
雪洛 已提交
105 106

    const watchObjRes = await page.$('#watch-obj-res')
107 108
    expect(await watchObjRes.text()).toBe('obj: {"num":0,"str":"num: 0","bool":false,"arr":[0]}')

雪洛's avatar
雪洛 已提交
109 110
    const watchObjStrRes = await page.$('#watch-obj-str-res')
    expect(await watchObjStrRes.text()).toBe(
111
      'str: num: 0, obj.str ref text: num: 0')
112 113 114

    // trigger
    const watchObjStrTriggerNum = await page.$('#watch-obj-str-trigger-num')
115
    expect(await watchObjStrTriggerNum.text()).toBe('0')
116

雪洛's avatar
雪洛 已提交
117
    const watchObjArrRes = await page.$('#watch-obj-arr-res')
118
    expect(await watchObjArrRes.text()).toBe('arr: [0]')
雪洛's avatar
雪洛 已提交
119

120
    const updateObjBtn = await page.$('.update-obj-btn')
雪洛's avatar
雪洛 已提交
121 122
    await updateObjBtn.tap()

123 124 125 126
    expect(await objStr.text()).toBe('num: 1')
    expect(await objNum.text()).toBe('1')
    expect(await objBool.text()).toBe('true')
    expect(await objArr.text()).toBe('[0,1]')
127

128
    expect(await watchObjRes.text()).toBe('obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}')
雪洛's avatar
雪洛 已提交
129
    expect(await watchObjStrRes.text()).toBe(
130
      'str: num: 1, obj.str ref text: num: 1')
131

132
    expect(await watchObjStrTriggerNum.text()).toBe('1')
133

DCloud-WZF's avatar
DCloud-WZF 已提交
134
    expect(await watchObjArrRes.text()).toBe(
135
      'arr: [0,1]')
雪洛's avatar
雪洛 已提交
136 137

    const watchCountAndObjNumRes = await page.$('#watch-count-obj-num-res')
138
    expect(await watchCountAndObjNumRes.text()).toBe('count: 3, obj.num: 1')
雪洛's avatar
雪洛 已提交
139
  })
140
})