File size: 2,769 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
import { nextTestSetup } from 'e2e-utils'

const { i18n } = require('./next.config')

const pages = [
  { url: '/about', page: '/about', params: null },
  { url: '/blog/about', page: '/[slug]/about', params: { slug: 'blog' } },
]

function checkDataRoute(data: any, page: string) {
  expect(data).toHaveProperty('pageProps')
  expect(data.pageProps).toHaveProperty('page', page)
}

describe('i18n-data-route', () => {
  const { next, skipped } = nextTestSetup({
    files: __dirname,
    // This test skips deployment because env vars that are doubled underscore prefixed
    // are not supported.
    skipDeployment: true,
    env: {
      // Disable internal header stripping so we can test the invoke output.
      __NEXT_NO_STRIP_INTERNAL_HEADERS: '1',
    },
  })

  if (skipped) return

  describe('with locale prefix', () => {
    describe.each(i18n.locales)('/%s', (locale) => {
      const prefixed = pages.map((page) => ({
        ...page,
        url: `/${locale}${page.url}`,
      }))

      it.each(prefixed)(
        'should render $page via $url',
        async ({ url, page }) => {
          const $ = await next.render$(url)
          expect($('[data-page]').data('page')).toBe(page)
        }
      )

      it.each(prefixed)(
        'should serve data for $page',
        async ({ url, page, params }) => {
          url = `/_next/data/${next.buildId}${url}.json`
          if (params) {
            const query = new URLSearchParams(params)
            // Ensure the query is sorted so it's deterministic.
            query.sort()
            url += `?${query.toString()}`
          }

          const res = await next.fetch(url)
          expect(res.status).toBe(200)
          expect(res.headers.get('content-type')).toStartWith(
            'application/json'
          )
          const data = await res.json()
          checkDataRoute(data, page)
        }
      )
    })
  })

  describe('without locale prefix', () => {
    it.each(pages)('should render $page via $url', async ({ url, page }) => {
      const $ = await next.render$(url)
      expect($('[data-page]').data('page')).toBe(page)
    })

    it.each(pages)(
      'should serve data for $page',
      async ({ url, page, params }) => {
        url = `/_next/data/${next.buildId}/${i18n.defaultLocale}${url}.json`
        if (params) {
          const query = new URLSearchParams(params)
          // Ensure the query is sorted so it's deterministic.
          query.sort()
          url += `?${query.toString()}`
        }
        const res = await next.fetch(url)
        expect(res.status).toBe(200)
        expect(res.headers.get('content-type')).toStartWith('application/json')
        const data = await res.json()
        checkDataRoute(data, page)
      }
    )
  })
})