dialog-page.test.js 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
jest.setTimeout(20000)

const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
const isWeb = platformInfo.startsWith('web')
const FIRST_PAGE_PATH = '/pages/API/dialog-page/dialog-page'
const NEXT_PAGE_PATH = '/pages/API/dialog-page/next-page'

describe('dialog page', () => {
  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)
  });

  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')
    // 不应触发父页面的生命周期,应该触发:
38
    // 1. openDialogPage success & complete callback
39
    // 2. dialog page 生命周期
40
    expect(lifecycleNum).toBe(7)
41
    await page.callMethod('setLifeCycleNum', 0)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  });

  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')
57 58
    expect(lifecycleNum).toBe(-3)
    await page.callMethod('setLifeCycleNum', 0)
59 60 61 62 63
  })

  it('openDialog with wrong path', async () => {
    await page.callMethod('openDialog1WrongPath')
    lifecycleNum = await page.callMethod('getLifeCycleNum')
64 65
    expect(lifecycleNum).toBe(-3)
    await page.callMethod('setLifeCycleNum', 0)
66 67 68 69 70 71
  })

  it('navigateTo nextPage & open Dialog', async () => {
    await page.callMethod('goNextPageOpenDialog1')
    await page.waitFor(2000)
    if (isWeb) {
72
      await page.waitFor(3000)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    }
    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')
89 90
    expect(lifecycleNum).toBe(-4)
    await page.callMethod('setLifeCycleNum', 0)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  })

  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 生命周期应该被触发
108 109
    expect(lifecycleNum).toBe(1)
    await page.callMethod('setLifeCycleNum', 0)
110 111 112 113 114 115 116 117 118 119
  })

  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
120 121
    expect(lifecycleNum).toBe(4)
    await page.callMethod('setLifeCycleNum', 0)
122 123 124 125 126 127 128
  })

  it('closeDialogPage', async () => {
    await page.callMethod('closeDialog')
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // 应触发 success & complete 回调
    // 应触发 dialogPage 的 unload,下层的 dialogPage 会先 show 再 unload
129
    expect(lifecycleNum).toBe(-7)
130 131 132 133 134 135 136 137 138

    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
139
    await page.callMethod('setLifeCycleNum', 0)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  })

  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')
157
    expect(lifecycleNum).toBe(4)
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

    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')
173 174
    expect(lifecycleNum).toBe(8)
    await page.callMethod('setLifeCycleNum', 0)
175 176 177 178 179 180 181 182 183
  })

  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')
184 185
    expect(lifecycleNum).toBe(4)
    await page.callMethod('setLifeCycleNum', 0)
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  })

  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 生命周期应该被触发
203 204
    expect(lifecycleNum).toBe(2)
    await page.callMethod('setLifeCycleNum', 0)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  })

  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')
222
    expect(lifecycleNum).toBe(4)
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

    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')
238
    expect(lifecycleNum).toBe(10)
239 240 241 242 243 244 245 246 247 248 249

    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')
250
    expect(lifecycleNum).toBe(7)
251 252 253 254 255 256 257 258 259 260 261

    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')
262
    expect(lifecycleNum).toBe(5)
263 264 265 266 267 268 269 270 271 272 273

    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')
274
    expect(lifecycleNum).toBe(2)
275 276 277 278 279 280 281
  })


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