next.js/test/e2e/link-with-api-rewrite/index.test.ts
index.test.ts75 lines2.5 KB
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { join } from 'path'
import webdriver from 'next-webdriver'

describe('link-with-api-rewrite', () => {
  let next: NextInstance

  beforeAll(async () => {
    next = await createNext({
      files: {
        pages: new FileRef(join(__dirname, 'app/pages')),
        'next.config.js': new FileRef(join(__dirname, 'app/next.config.js')),
      },
      dependencies: {},
    })
  })
  afterAll(() => next.destroy())

  it('should perform hard navigation for rewritten urls', async () => {
    const browser = await webdriver(next.url, '/')

    try {
      // Click the link on the page, we expect that there will be a hard
      // navigation later (we do this be checking that the window global is
      // unset).
      await browser.eval('window.beforeNav = "hi"')
      await browser.elementById('rewrite').click()
      await retry(async () => {
        expect(await browser.eval('window.beforeNav')).not.toEqual('hi')
      })

      // Check to see that we were in fact navigated to the correct page.
      const pathname = await browser.eval('window.location.pathname')
      expect(pathname).toBe('/some/route/for')

      // Check to see that the resulting data is coming from the right endpoint.
      const text = await browser.eval(
        'window.document.documentElement.innerText'
      )
      expect(text).toBe('{"from":"/some/route/for"}')
    } finally {
      await browser.close()
    }
  })

  it('should perform hard navigation for direct urls', async () => {
    const browser = await webdriver(next.url, '/')

    try {
      // Click the link on the page, we expect that there will be a hard
      // navigation later (we do this be checking that the window global is
      // unset).
      await browser.eval('window.beforeNav = "hi"')
      await browser.elementById('direct').click()
      await retry(async () => {
        expect(await browser.eval('window.beforeNav')).not.toEqual('hi')
      })

      // Check to see that we were in fact navigated to the correct page.
      const pathname = await browser.eval('window.location.pathname')
      expect(pathname).toBe('/api/json')

      // Check to see that the resulting data is coming from the right endpoint.
      const text = await browser.eval(
        'window.document.documentElement.innerText'
      )
      expect(text).toBe('{"from":""}')
    } finally {
      await browser.close()
    }
  })
})
Quest for Codev2.0.0
/
SIGN IN