navigator.test.js 7.5 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1
jest.setTimeout(80000)
DCloud-WZF's avatar
DCloud-WZF 已提交
2
const CURRENT_PAGE_PATH = '/pages/API/navigator/navigator'
DCloud-WZF's avatar
DCloud-WZF 已提交
3 4
const CHILD1_PAGE_PATH = '/pages/API/navigator/new-page/new-page-1'
const CHILD2_PAGE_PATH = '/pages/API/navigator/new-page/new-page-2'
DCloud-WZF's avatar
DCloud-WZF 已提交
5 6 7 8

describe('navigator', () => {
  let page
  let lifeCycleNum
DCloud-WZF's avatar
DCloud-WZF 已提交
9 10 11 12 13 14 15 16

  afterAll(async () => {
    const resetLifecycleNum = 1100
    await page.callMethod('setLifeCycleNum', resetLifecycleNum)
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(resetLifecycleNum)
  })

DCloud-WZF's avatar
DCloud-WZF 已提交
17 18
  it('reLaunch', async () => {
    page = await program.navigateTo(CURRENT_PAGE_PATH)
DCloud-WZF's avatar
DCloud-WZF 已提交
19
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
20 21
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('reLaunch')
DCloud-WZF's avatar
DCloud-WZF 已提交
22
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
23
    page = await program.navigateTo(CURRENT_PAGE_PATH)
DCloud-WZF's avatar
DCloud-WZF 已提交
24
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
25 26 27 28 29 30 31
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
  })

  it('navigateTo', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('navigateTo')
DCloud-WZF's avatar
DCloud-WZF 已提交
32
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
33 34 35
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
    page = await program.navigateBack()
DCloud-WZF's avatar
DCloud-WZF 已提交
36
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
37 38 39 40 41
  })

  it('navigateTo error page', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('navigateToErrorPage')
DCloud-WZF's avatar
DCloud-WZF 已提交
42
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
43 44 45 46 47 48 49
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
  })

  it('navigateTo debounce', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('navigateToDebounce')
DCloud-WZF's avatar
DCloud-WZF 已提交
50
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
51 52 53
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(4)
    page = await program.navigateBack()
DCloud-WZF's avatar
DCloud-WZF 已提交
54
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
55 56
  })

DCloud-WZF's avatar
DCloud-WZF 已提交
57 58 59 60
  it('navigateTo relative path', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('navigateToRelativePath1')
    page = await program.navigateBack()
DCloud-WZF's avatar
DCloud-WZF 已提交
61
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
62 63
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
DCloud-WZF's avatar
DCloud-WZF 已提交
64
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
65 66 67 68 69 70
  })

  it('navigateTo relative path ./', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('navigateToRelativePath2')
    page = await program.navigateBack()
DCloud-WZF's avatar
DCloud-WZF 已提交
71
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
72 73
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
DCloud-WZF's avatar
DCloud-WZF 已提交
74
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
75 76 77 78 79 80
  })

  it('navigateTo relative path ../', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('navigateToRelativePath3')
    page = await program.navigateBack()
DCloud-WZF's avatar
DCloud-WZF 已提交
81
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
82 83
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
DCloud-WZF's avatar
DCloud-WZF 已提交
84
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
85 86
  })

DCloud-WZF's avatar
DCloud-WZF 已提交
87 88 89
  it('navigateBack', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('navigateBack')
DCloud-WZF's avatar
DCloud-WZF 已提交
90
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
91
    page = await program.navigateTo(CURRENT_PAGE_PATH)
DCloud-WZF's avatar
DCloud-WZF 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    await page.waitFor(500)
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
  })

  it('navigateBackWithDelta1', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('navigateBackWithDelta1')
    await page.waitFor(1000)
    page = await program.currentPage()
    expect(`/${page.path}`).toBe(CURRENT_PAGE_PATH)
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
  })

DCloud-WZF's avatar
DCloud-WZF 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120
  it('navigateBackWithDelta2', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await program.navigateTo(CHILD2_PAGE_PATH)
    await page.waitFor(500)
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    await page.waitFor(500)
    await page.callMethod('navigateBackWithDelta2')
    await page.waitFor(500)
    page = await program.currentPage()
    expect(`/${page.path}`).toBe(CURRENT_PAGE_PATH)
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(10)
  })

DCloud-WZF's avatar
DCloud-WZF 已提交
121 122 123 124
  it('navigateBackWithDelta100', async () => {
    page = await program.reLaunch(CURRENT_PAGE_PATH)
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('navigateBackWithDelta100')
DCloud-WZF's avatar
DCloud-WZF 已提交
125
    await page.waitFor(1000)
DCloud-WZF's avatar
DCloud-WZF 已提交
126 127
    page = await program.currentPage()
    expect(`/${page.path}`).toBe(CURRENT_PAGE_PATH)
DCloud-WZF's avatar
DCloud-WZF 已提交
128 129 130 131 132 133 134
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
  })

  it('redirectTo', async () => {
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('redirectTo')
DCloud-WZF's avatar
DCloud-WZF 已提交
135
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
136
    page = await program.redirectTo(CURRENT_PAGE_PATH)
DCloud-WZF's avatar
DCloud-WZF 已提交
137
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
138 139 140 141 142
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
  })

  it('switchTab', async () => {
DCloud-WZF's avatar
DCloud-WZF 已提交
143
    page = await program.reLaunch(CURRENT_PAGE_PATH)
DCloud-WZF's avatar
DCloud-WZF 已提交
144 145
    await page.callMethod('setLifeCycleNum', 0)
    await page.callMethod('switchTab')
DCloud-WZF's avatar
DCloud-WZF 已提交
146
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
147
    page = await program.navigateTo(CURRENT_PAGE_PATH)
DCloud-WZF's avatar
DCloud-WZF 已提交
148
    await page.waitFor(500)
DCloud-WZF's avatar
DCloud-WZF 已提交
149 150 151
    lifeCycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifeCycleNum).toBe(2)
  })
DCloud-WZF's avatar
DCloud-WZF 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

  it('20 times navigateTo', async () => {
    const originPageStack = await program.pageStack()
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD1_PAGE_PATH)
    expect(page.path).toBe(CHILD1_PAGE_PATH.substring(1))
    page = await program.navigateTo(CHILD2_PAGE_PATH)
    expect(page.path).toBe(CHILD2_PAGE_PATH.substring(1))
    const newPageStack = await program.pageStack()
    expect(newPageStack.length - originPageStack.length).toBe(20)
  })
DCloud-WZF's avatar
DCloud-WZF 已提交
198
})