keep-alive.test.js 3.5 KB
Newer Older
1 2
const PAGE_PATH_OPTIONS = '/pages/built-in/component/keep-alive/keep-alive-options'
const PAGE_PATH_COMPOSITION = '/pages/built-in/component/keep-alive/keep-alive-composition'
3 4

describe('keep-alive', () => {
D
DCloud_LXH 已提交
5 6 7
  let page = null
  const testKeepAlive = async () => {
    await page.waitFor('view')
DCloud-WZF's avatar
DCloud-WZF 已提交
8 9 10
    let shouldExcludeBtnList = await page.$$('.should-exclude-btn')
    for (let i = 0; i < shouldExcludeBtnList.length; i++) {
      await shouldExcludeBtnList[i].tap()
D
DCloud_LXH 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23
    }
    let shouldExcludeTexts = await page.$$('.should-exclude-text')
    for (let i = 0; i < shouldExcludeTexts.length; i++) {
      expect(await shouldExcludeTexts[i].text()).toBe('count: 1')
    }

    const showCounterBtn = await page.$('.show-counter')
    await showCounterBtn.tap()

    const activatedNum = await page.$('#activated-num')
    expect(await activatedNum.text()).toBe('activated num: 1')
    const deactivatedNum = await page.$('#deactivated-num')
    expect(await deactivatedNum.text()).toBe('deactivated num: 0')
DCloud-WZF's avatar
DCloud-WZF 已提交
24 25
    shouldExcludeBtnList = await page.$$('.should-exclude-btn')
    expect(shouldExcludeBtnList.length).toBe(0)
D
DCloud_LXH 已提交
26

DCloud-WZF's avatar
DCloud-WZF 已提交
27
    const counterBtnList = await page.$$('.counter-btn')
28
    for (let i = 0; i < counterBtnList.length; i++) {
DCloud-WZF's avatar
DCloud-WZF 已提交
29
      await counterBtnList[i].tap()
D
DCloud_LXH 已提交
30 31 32 33 34 35
    }

    const showShouldExcludeBtn = await page.$('.show-should-exclude')
    await showShouldExcludeBtn.tap()

    shouldExcludeTexts = await page.$$('.should-exclude-text')
36
    for (let i = 0;i < shouldExcludeTexts.length;i++) {
DCloud-WZF's avatar
DCloud-WZF 已提交
37
      const text = await shouldExcludeTexts[i].text()
38
      if (i < shouldExcludeTexts.length - 1) {
DCloud-WZF's avatar
DCloud-WZF 已提交
39
        expect(text).toBe('count: 0')
D
DCloud_LXH 已提交
40
      } else {
DCloud-WZF's avatar
DCloud-WZF 已提交
41
        expect(text).toBe('count: 1')
D
DCloud_LXH 已提交
42 43 44 45 46 47 48 49 50
      }
    }

    await showCounterBtn.tap()

    expect(await activatedNum.text()).toBe('activated num: 2')
    expect(await deactivatedNum.text()).toBe('deactivated num: 1')

    let counterTexts = await page.$$('.counter-text')
51
    for (let i = 0; i < counterTexts.length; i++) {
D
DCloud_LXH 已提交
52 53 54 55 56 57
      expect(await counterTexts[i].text()).toBe('count: 1')
    }

    const showMessageBtn = await page.$('.show-message')
    await showMessageBtn.tap()

DCloud-WZF's avatar
DCloud-WZF 已提交
58 59 60
    const changeMessageBtnList = await page.$$('.change-message')
    for (let i = 0; i < changeMessageBtnList.length; i++) {
      await changeMessageBtnList[i].tap()
D
DCloud_LXH 已提交
61 62 63
    }

    let messageTexts = await page.$$('.message-text')
64
    for (let i = 0; i < messageTexts.length; i++) {
D
DCloud_LXH 已提交
65 66 67 68 69 70 71 72 73
      expect(await messageTexts[i].text()).toBe('msg: message changed')
    }

    await showCounterBtn.tap()

    expect(await activatedNum.text()).toBe('activated num: 3')
    expect(await deactivatedNum.text()).toBe('deactivated num: 2')

    counterTexts = await page.$$('.counter-text')
74
    for (let i = 0; i < counterTexts.length; i++) {
D
DCloud_LXH 已提交
75 76 77 78 79 80
      expect(await counterTexts[i].text()).toBe('count: 1')
    }

    await showMessageBtn.tap()

    messageTexts = await page.$$('.message-text')
81
    for (let i = 0; i < messageTexts.length; i++) {
D
DCloud_LXH 已提交
82 83 84 85
      expect(await messageTexts[i].text()).toBe('msg: message changed')
    }

    await showShouldExcludeBtn.tap()
DCloud-WZF's avatar
DCloud-WZF 已提交
86
    
D
DCloud_LXH 已提交
87
    shouldExcludeTexts = await page.$$('.should-exclude-text')
88
    for (let i = 0; i < shouldExcludeTexts.length; i++) {
D
DCloud_LXH 已提交
89 90 91 92 93 94 95 96 97 98 99
      expect(await shouldExcludeTexts[i].text()).toBe('count: 0')
    }
  }
  it('keep-alive Options API', async () => {
    page = await program.reLaunch(PAGE_PATH_OPTIONS)
    await testKeepAlive()
  })
  it('keep-alive Composition API', async () => {
    page = await program.reLaunch(PAGE_PATH_COMPOSITION)
    await testKeepAlive()
  })
100
})