Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: merge rsc custom app cases #36713

Merged
merged 3 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fs from 'fs-extra'

import {
fetchViaHTTP,
renderViaHTTP,
nextBuild,
runDevSuite,
runProdSuite,
Expand All @@ -15,7 +14,6 @@ import {
appDir,
nativeModuleTestAppDir,
appPage,
appServerPage,
error500Page,
nextConfig,
} from './utils'
Expand All @@ -27,13 +25,6 @@ import basic from './basic'
import runtime from './runtime'
import { getNodeBuiltinModuleNotSupportedInEdgeRuntimeMessage } from 'next/dist/build/utils'

const rscAppPage = `
import Container from '../components/container.server'
export default function App({children}) {
return <Container>{children}</Container>
}
`

const appWithGlobalCss = `
import '../styles.css'

Expand Down Expand Up @@ -184,19 +175,6 @@ const nodejsRuntimeBasicSuite = {
},
}

const customAppPageSuite = {
runTests: (context) => {
it('should render container in app', async () => {
const indexHtml = await renderViaHTTP(context.appPort, '/')
const indexFlight = await renderViaHTTP(context.appPort, '/?__flight__=1')
expect(indexHtml).toContain('container-server')
expect(indexFlight).toContain('container-server')
})
},
beforeAll: () => appServerPage.write(rscAppPage),
afterAll: () => appServerPage.delete(),
}

const cssSuite = {
runTests: css,
beforeAll: () => appPage.write(appWithGlobalCss),
Expand All @@ -208,8 +186,5 @@ runProdSuite('Node.js runtime', appDir, nodejsRuntimeBasicSuite)
runDevSuite('Edge runtime', appDir, edgeRuntimeBasicSuite)
runProdSuite('Edge runtime', appDir, edgeRuntimeBasicSuite)

runDevSuite('Custom App', appDir, customAppPageSuite)
runProdSuite('Custom App', appDir, customAppPageSuite)

runDevSuite('CSS', appDir, cssSuite)
runProdSuite('CSS', appDir, cssSuite)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,48 @@ function splitLines(text) {
.filter(Boolean)
}

function getOccurrence(text, matcher) {
return (text.match(matcher) || []).length
}

function flight(context) {
describe('flight response', () => {
it('should contain _app.server in flight response (node)', async () => {
const html = await renderViaHTTP(context.appPort, '/node-rsc')
const flightResponse = await renderViaHTTP(
context.appPort,
'/node-rsc?__flight__=1'
)
expect(
getOccurrence(html, new RegExp(`class="app-server-root"`, 'g'))
).toBe(1)
expect(
getOccurrence(
flightResponse,
new RegExp(`"className":\s*"app-server-root"`, 'g')
)
).toBe(1)
})
})

it('should contain _app.server in flight response (edge)', async () => {
const html = await renderViaHTTP(context.appPort, '/edge-rsc')
const flightResponse = await renderViaHTTP(
context.appPort,
'/edge-rsc?__flight__=1'
)
expect(
getOccurrence(html, new RegExp(`class="app-server-root"`, 'g'))
).toBe(1)
expect(
getOccurrence(
flightResponse,
new RegExp(`"className":\s*"app-server-root"`, 'g')
huozhi marked this conversation as resolved.
Show resolved Hide resolved
)
).toBe(1)
})
}

async function testRoute(appPort, url, { isStatic, isEdge, isRSC }) {
const html1 = await renderViaHTTP(appPort, url)
const renderedAt1 = +html1.match(/Time: (\d+)/)[1]
Expand All @@ -44,9 +86,8 @@ async function testRoute(appPort, url, { isStatic, isEdge, isRSC }) {
const [rootClass, oppositeRootClass] = isRSC
? [rootClasses[0], rootClasses[1]]
: [rootClasses[1], rootClasses[0]]
const appServerOccurrence =
html1.match(new RegExp(`class="${rootClass}"`, 'g')) || []
expect(appServerOccurrence.length).toBe(1)

expect(getOccurrence(html1, new RegExp(`class="${rootClass}"`, 'g'))).toBe(1)
expect(html1).not.toContain(`"${oppositeRootClass}"`)
}

Expand All @@ -67,6 +108,8 @@ describe('Switchable runtime (prod)', () => {
await killApp(context.server)
})

flight(context)

it('should build /static as a static page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/static', {
isStatic: true,
Expand Down Expand Up @@ -281,6 +324,7 @@ describe('Switchable runtime (dev)', () => {
await killApp(context.server)
})

flight(context)
it('should support client side navigation to ssr rsc pages', async () => {
let flightRequest = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const nativeModuleTestAppDir = join(
'../unsupported-native-module'
)
export const appPage = new File(join(appDir, 'pages/_app.js'))
export const appServerPage = new File(join(appDir, 'pages/_app.server.js'))
export const error500Page = new File(join(appDir, 'pages/500.js'))
export const nextConfig = new File(join(appDir, 'next.config.js'))

Expand Down