From 414c62d5d651db844cf8278a5ac37c53b7cd8ee5 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 7 Sep 2023 11:43:29 -0600 Subject: [PATCH] tests: added test to verify fix --- test/e2e/i18n-data-route/components/page.tsx | 12 +++ .../i18n-data-route/i18n-data-route.test.ts | 83 +++++++++++++++++++ test/e2e/i18n-data-route/next.config.js | 9 ++ .../pages/[slug]/about/index.tsx | 5 ++ .../e2e/i18n-data-route/pages/about/index.tsx | 5 ++ 5 files changed, 114 insertions(+) create mode 100644 test/e2e/i18n-data-route/components/page.tsx create mode 100644 test/e2e/i18n-data-route/i18n-data-route.test.ts create mode 100644 test/e2e/i18n-data-route/next.config.js create mode 100644 test/e2e/i18n-data-route/pages/[slug]/about/index.tsx create mode 100644 test/e2e/i18n-data-route/pages/about/index.tsx diff --git a/test/e2e/i18n-data-route/components/page.tsx b/test/e2e/i18n-data-route/components/page.tsx new file mode 100644 index 000000000000..60418c58cba4 --- /dev/null +++ b/test/e2e/i18n-data-route/components/page.tsx @@ -0,0 +1,12 @@ +import React from 'react' + +export function Page({ page }) { + return

{page}

+} + +export function createGetServerSideProps(page: string) { + return async function getServerSideProps(ctx) { + const output = ctx.req.headers['x-invoke-output'] ?? null + return { props: { page, output } } + } +} diff --git a/test/e2e/i18n-data-route/i18n-data-route.test.ts b/test/e2e/i18n-data-route/i18n-data-route.test.ts new file mode 100644 index 000000000000..a25d8e8adad2 --- /dev/null +++ b/test/e2e/i18n-data-route/i18n-data-route.test.ts @@ -0,0 +1,83 @@ +import { createNextDescribe } 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) + expect(data.pageProps).toHaveProperty('output', page) +} + +createNextDescribe( + 'i18n-data-route', + { + files: __dirname, + }, + ({ next }) => { + 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')).toBe('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')).toBe('application/json') + const data = await res.json() + checkDataRoute(data, page) + } + ) + }) + } +) diff --git a/test/e2e/i18n-data-route/next.config.js b/test/e2e/i18n-data-route/next.config.js new file mode 100644 index 000000000000..32a015bd37ca --- /dev/null +++ b/test/e2e/i18n-data-route/next.config.js @@ -0,0 +1,9 @@ +/** + * @type {import('next').NextConfig} + */ +module.exports = { + i18n: { + locales: ['en-CA', 'fr-CA'], + defaultLocale: 'en-CA', + }, +} diff --git a/test/e2e/i18n-data-route/pages/[slug]/about/index.tsx b/test/e2e/i18n-data-route/pages/[slug]/about/index.tsx new file mode 100644 index 000000000000..3c8a96c601f4 --- /dev/null +++ b/test/e2e/i18n-data-route/pages/[slug]/about/index.tsx @@ -0,0 +1,5 @@ +import { Page, createGetServerSideProps } from '../../../components/page' + +export default Page + +export const getServerSideProps = createGetServerSideProps('/[slug]/about') diff --git a/test/e2e/i18n-data-route/pages/about/index.tsx b/test/e2e/i18n-data-route/pages/about/index.tsx new file mode 100644 index 000000000000..83ef2115f532 --- /dev/null +++ b/test/e2e/i18n-data-route/pages/about/index.tsx @@ -0,0 +1,5 @@ +import { Page, createGetServerSideProps } from '../../components/page' + +export default Page + +export const getServerSideProps = createGetServerSideProps('/about')