File size: 3,398 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* eslint-env jest */

import path from 'path'
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'

describe('Client navigation with onClick action', () => {
  const { next } = nextTestSetup({
    files: path.join(__dirname, 'fixture'),
    env: {
      TEST_STRICT_NEXT_HEAD: String(true),
    },
  })

  it('should reload the page and perform additional action', async () => {
    const browser = await next.browser('/nav/on-click')
    const defaultCountQuery = await browser.elementByCss('#query-count').text()
    const defaultCountState = await browser.elementByCss('#state-count').text()
    expect(defaultCountQuery).toBe('QUERY COUNT: 0')
    expect(defaultCountState).toBe('STATE COUNT: 0')

    await browser.elementByCss('#on-click-link').click()

    await retry(async () => {
      const countQueryAfterClicked = await browser
        .elementByCss('#query-count')
        .text()
      const countStateAfterClicked = await browser
        .elementByCss('#state-count')
        .text()
      expect(countQueryAfterClicked).toBe('QUERY COUNT: 1')
      expect(countStateAfterClicked).toBe('STATE COUNT: 1')
    })
  })

  it('should not reload if default was prevented', async () => {
    const browser = await next.browser('/nav/on-click')
    const defaultCountQuery = await browser.elementByCss('#query-count').text()
    const defaultCountState = await browser.elementByCss('#state-count').text()
    expect(defaultCountQuery).toBe('QUERY COUNT: 0')
    expect(defaultCountState).toBe('STATE COUNT: 0')

    await browser.elementByCss('#on-click-link-prevent-default').click()

    await retry(async () => {
      const countQueryAfterClicked = await browser
        .elementByCss('#query-count')
        .text()
      const countStateAfterClicked = await browser
        .elementByCss('#state-count')
        .text()
      expect(countQueryAfterClicked).toBe('QUERY COUNT: 0')
      expect(countStateAfterClicked).toBe('STATE COUNT: 1')
    })

    await browser.elementByCss('#on-click-link').click()

    await retry(async () => {
      const countQueryAfterClickedAgain = await browser
        .elementByCss('#query-count')
        .text()
      const countStateAfterClickedAgain = await browser
        .elementByCss('#state-count')
        .text()
      expect(countQueryAfterClickedAgain).toBe('QUERY COUNT: 1')
      expect(countStateAfterClickedAgain).toBe('STATE COUNT: 2')
    })
  })

  it('should always replace the state and perform additional action', async () => {
    const browser = await next.browser('/nav')

    await browser.elementByCss('#on-click-link').click()

    await retry(async () => {
      const defaultCountQuery = await browser
        .elementByCss('#query-count')
        .text()
      expect(defaultCountQuery).toBe('QUERY COUNT: 1')
    })

    await browser.elementByCss('#on-click-link').click()

    await retry(async () => {
      const countQueryAfterClicked = await browser
        .elementByCss('#query-count')
        .text()
      const countStateAfterClicked = await browser
        .elementByCss('#state-count')
        .text()
      expect(countQueryAfterClicked).toBe('QUERY COUNT: 2')
      expect(countStateAfterClicked).toBe('STATE COUNT: 1')
    })

    // Since we replace the state, back button would simply go us back to /nav
    await browser.back().waitForElementByCss('.nav-home')
  })
})