dialog-page.test.js 9.6 KB
Newer Older
1 2 3
jest.setTimeout(20000)

const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
4 5
const isWeb = platformInfo.startsWith('web')
const isAndroid = platformInfo.startsWith('android')
6 7 8 9
const FIRST_PAGE_PATH = '/pages/API/dialog-page/dialog-page'
const NEXT_PAGE_PATH = '/pages/API/dialog-page/next-page'

describe('dialog page', () => {
10 11 12 13 14 15 16
  if (process.env.UNI_AUTOMATOR_APP_WEBVIEW == 'true') {
		it('skip app-webview', () => {
			expect(1).toBe(1)
		})
		return
	}

17 18 19 20 21 22 23 24 25 26
  let page;
  let initLifeCycleNum;
  let lifecycleNum;
  beforeAll(async () => {
    page = await program.reLaunch(FIRST_PAGE_PATH)
    await page.waitFor('view');
    initLifeCycleNum = await page.callMethod('getLifeCycleNum');
    await page.callMethod('setLifeCycleNum', 0)
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifecycleNum).toBe(0)
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
27 28
  });

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  it('open dialog1', async () => {
    await page.callMethod('openDialog1');
    // 无法通过获取 dom 元素来判断是否打开了 dialogPage
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // 不应触发父页面的生命周期,应该触发:
46
    // 1. openDialogPage success & complete callback
47
    // 2. dialog page 生命周期
48
    expect(lifecycleNum).toBe(7)
49
    await page.callMethod('setLifeCycleNum', 0)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  });

  it('closeDialogPage', async () => {
    await page.callMethod('closeDialog');
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    // closeDialogPage success & complete callback 应被触发
    // dialogPage onUnload 应被触发
    lifecycleNum = await page.callMethod('getLifeCycleNum')
65 66
    expect(lifecycleNum).toBe(-3)
    await page.callMethod('setLifeCycleNum', 0)
67 68 69 70 71
  })

  it('openDialog with wrong path', async () => {
    await page.callMethod('openDialog1WrongPath')
    lifecycleNum = await page.callMethod('getLifeCycleNum')
72 73
    expect(lifecycleNum).toBe(-3)
    await page.callMethod('setLifeCycleNum', 0)
74 75 76 77 78 79
  })

  it('navigateTo nextPage & open Dialog', async () => {
    await page.callMethod('goNextPageOpenDialog1')
    await page.waitFor(2000)
    if (isWeb) {
80
      await page.waitFor(3000)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    }
    page = await program.currentPage()
    expect(page.path).toBe(NEXT_PAGE_PATH.substring(1))
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
97 98
    expect(lifecycleNum).toBe(-4)
    await page.callMethod('setLifeCycleNum', 0)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  })

  it('dialog1 navigateBack', async () => {
    await program.navigateBack()
    page = await program.currentPage()
    // dialogPage onBackPress 返回 true, 应可以拦截 navigateBack
    expect(page.path).toBe(NEXT_PAGE_PATH.substring(1))
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // onBackPress 生命周期应该被触发
116 117
    expect(lifecycleNum).toBe(1)
    await page.callMethod('setLifeCycleNum', 0)
118 119 120 121 122 123 124 125 126 127
  })

  it('open dialog2', async () => {
    await page.callMethod('openDialog2')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // 应触发前一个 dialogPage 的 onHide
128 129
    expect(lifecycleNum).toBe(4)
    await page.callMethod('setLifeCycleNum', 0)
130 131 132 133 134 135 136
  })

  it('closeDialogPage', async () => {
    await page.callMethod('closeDialog')
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // 应触发 success & complete 回调
    // 应触发 dialogPage 的 unload,下层的 dialogPage 会先 show 再 unload
137
    expect(lifecycleNum).toBe(-7)
138 139 140 141 142 143 144 145 146

    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
147
    await page.callMethod('setLifeCycleNum', 0)
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  })

  it('open multiple dialog page', async () => {
    await page.callMethod('openDialog1')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image1 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image1).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
165
    expect(lifecycleNum).toBe(4)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

    await page.callMethod('openDialog2')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image2 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image2).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
181 182
    expect(lifecycleNum).toBe(8)
    await page.callMethod('setLifeCycleNum', 0)
183 184 185 186 187 188 189 190 191
  })

  it('openDialogPage to home page', async () => {
    await page.callMethod('openDialogPage1ToHomePage')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    lifecycleNum = await page.callMethod('getLifeCycleNum')
192 193
    expect(lifecycleNum).toBe(4)
    await page.callMethod('setLifeCycleNum', 0)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  })

  it('dialog2 navigateBack', async () => {
    await program.navigateBack()
    page = await program.currentPage()
    // dialogPage onBackPress 返回 true, 应可以拦截 navigateBack
    expect(page.path).toBe(FIRST_PAGE_PATH.substring(1))
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // onBackPress 生命周期应该被触发
211 212
    expect(lifecycleNum).toBe(2)
    await page.callMethod('setLifeCycleNum', 0)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  })

  it('close specified dialogPage', async () => {
    await page.callMethod('openDialog2')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image1 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image1).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
230
    expect(lifecycleNum).toBe(4)
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

    await page.callMethod('openDialog1')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image2 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image2).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
246
    expect(lifecycleNum).toBe(10)
247 248 249 250 251 252 253 254 255 256 257

    await page.callMethod('closeSpecifiedDialog', 0)
    const image3 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image3).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
258
    expect(lifecycleNum).toBe(7)
259 260 261 262 263 264 265 266 267 268 269

    await page.callMethod('closeSpecifiedDialog', 1)
    const image4 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image4).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
270
    expect(lifecycleNum).toBe(5)
271 272 273 274 275 276 277 278 279 280 281

    await page.callMethod('closeSpecifiedDialog', 0)
    const image5 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image5).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
282
    expect(lifecycleNum).toBe(2)
283
  })
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310


  it('input-hold-keyboard in dialog', async () => {
    await page.callMethod('jest_OpenDialog1')
    await page.waitFor(2000);
    await page.callMethod('jest_getTapPoint')
    const point_x = await page.data('jest_click_x');
    const point_y = await page.data('jest_click_y');
    if (isAndroid){
      await program.adbCommand("input tap" + " " + point_x + " " + point_y)
      console.log("input tap" + " " + point_x + " " + point_y);
    } else {
      await program.tap({x: point_x, y: point_y})
    }

    await page.waitFor(1000);
    const image = await program.screenshot({
        deviceShot: true,
        area: {
          x: 0,
          y: 200,
        }
    })
    expect(image).toSaveImageSnapshot()
    await page.waitFor(2000);
    await page.callMethod('jest_CloseDialog1')
  })
311 312

  it('dialogPage hideStatusBar hideBottomNavigationIndicator', async () => {
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    if (isAndroid) {
      await page.callMethod('openDialog2ForTest');
      await page.waitFor(1000);
      await page.callMethod('setPageStyleForTest', {
        hideStatusBar: true,
        hideBottomNavigationIndicator: true
      });
      await page.waitFor(2000);
      const image = await program.screenshot({
        deviceShot: true
      });
      expect(image).toSaveImageSnapshot();
      await page.waitFor(2000);
      await page.callMethod('closeDialog2ForTest');
    }
328
  });
329 330 331 332 333

  afterAll(async () => {
    await page.callMethod('setLifeCycleNum', initLifeCycleNum)
  });
});