From b200772a01f9f56c23ec2dc3f40a0371557ea269 Mon Sep 17 00:00:00 2001 From: qixuan <58852732+GiveMe-A-Name@users.noreply.github.com> Date: Thu, 22 Feb 2024 11:42:14 +0800 Subject: [PATCH] chore: add more test (#5429) --- packages/server/core/jest.config.js | 6 +- .../middlewares/renderHandler/ssrRender.ts | 1 + .../data/bundles/main-server-loaders.js | 2 +- .../data/bundles/user-server-loaders.js | 4 + .../base/fixtures/render/ssr/bundles/main.js | 3 + .../base/fixtures/render/ssr/bundles/user.js | 3 + .../tests/base/fixtures/render/ssr/index.html | 3 + .../tests/base/fixtures/render/ssr/route.json | 32 +++++++ .../tests/base/fixtures/render/ssr/user.html | 3 + .../base/middlewares/dataHandler.test.ts | 16 +++- .../tests/base/{ => middlewares}/mock.test.ts | 4 +- .../tests/base/middlewares/render.test.ts | 85 ++++++++++++++++++- 12 files changed, 149 insertions(+), 13 deletions(-) create mode 100644 packages/server/core/tests/base/fixtures/data/bundles/user-server-loaders.js create mode 100644 packages/server/core/tests/base/fixtures/render/ssr/bundles/main.js create mode 100644 packages/server/core/tests/base/fixtures/render/ssr/bundles/user.js create mode 100644 packages/server/core/tests/base/fixtures/render/ssr/index.html create mode 100644 packages/server/core/tests/base/fixtures/render/ssr/route.json create mode 100644 packages/server/core/tests/base/fixtures/render/ssr/user.html rename packages/server/core/tests/base/{ => middlewares}/mock.test.ts (96%) diff --git a/packages/server/core/jest.config.js b/packages/server/core/jest.config.js index d287dcb7bb17..a8ab96f9a71d 100644 --- a/packages/server/core/jest.config.js +++ b/packages/server/core/jest.config.js @@ -1,5 +1,3 @@ -// eslint-disable-next-line node/no-unsupported-features/node-builtins -const { TextEncoder, TextDecoder } = require('util'); const sharedConfig = require('@scripts/jest-config'); /** @type {import('@jest/types').Config.InitialOptions} */ @@ -7,9 +5,9 @@ module.exports = { ...sharedConfig, rootDir: __dirname, globals: { - TextEncoder, - TextDecoder, + // FIXME: inject Headers polyfill }, + testEnvironment: 'node', moduleNameMapper: { '^@base/(.*)$': '/src/base/$1', }, diff --git a/packages/server/core/src/base/middlewares/renderHandler/ssrRender.ts b/packages/server/core/src/base/middlewares/renderHandler/ssrRender.ts index 15cb880e5461..50dabcc47209 100644 --- a/packages/server/core/src/base/middlewares/renderHandler/ssrRender.ts +++ b/packages/server/core/src/base/middlewares/renderHandler/ssrRender.ts @@ -36,6 +36,7 @@ const defaultReporter: Reporter = { interface SSRRenderOptions { pwd: string; + // FIXME: remove mode. mode: 'string' | 'stream'; html: string; routeInfo: ServerRoute; diff --git a/packages/server/core/tests/base/fixtures/data/bundles/main-server-loaders.js b/packages/server/core/tests/base/fixtures/data/bundles/main-server-loaders.js index fdb0a14bbbd1..cba524737284 100644 --- a/packages/server/core/tests/base/fixtures/data/bundles/main-server-loaders.js +++ b/packages/server/core/tests/base/fixtures/data/bundles/main-server-loaders.js @@ -1,4 +1,4 @@ export async function handleRequest() { // eslint-disable-next-line no-undef - return new Response('handle data'); + return new Response('handle main'); } diff --git a/packages/server/core/tests/base/fixtures/data/bundles/user-server-loaders.js b/packages/server/core/tests/base/fixtures/data/bundles/user-server-loaders.js new file mode 100644 index 000000000000..a20a45d51dc7 --- /dev/null +++ b/packages/server/core/tests/base/fixtures/data/bundles/user-server-loaders.js @@ -0,0 +1,4 @@ +export async function handleRequest() { + // eslint-disable-next-line no-undef + return new Response('handle user'); +} diff --git a/packages/server/core/tests/base/fixtures/render/ssr/bundles/main.js b/packages/server/core/tests/base/fixtures/render/ssr/bundles/main.js new file mode 100644 index 000000000000..eea5f076bc67 --- /dev/null +++ b/packages/server/core/tests/base/fixtures/render/ssr/bundles/main.js @@ -0,0 +1,3 @@ +export function serverRender() { + return 'SSR Render'; +} diff --git a/packages/server/core/tests/base/fixtures/render/ssr/bundles/user.js b/packages/server/core/tests/base/fixtures/render/ssr/bundles/user.js new file mode 100644 index 000000000000..ab99d181e238 --- /dev/null +++ b/packages/server/core/tests/base/fixtures/render/ssr/bundles/user.js @@ -0,0 +1,3 @@ +export function serverRender() { + return 'SSR User Render'; +} diff --git a/packages/server/core/tests/base/fixtures/render/ssr/index.html b/packages/server/core/tests/base/fixtures/render/ssr/index.html new file mode 100644 index 000000000000..a1729f452b3a --- /dev/null +++ b/packages/server/core/tests/base/fixtures/render/ssr/index.html @@ -0,0 +1,3 @@ + +

Hello Modern

+ diff --git a/packages/server/core/tests/base/fixtures/render/ssr/route.json b/packages/server/core/tests/base/fixtures/render/ssr/route.json new file mode 100644 index 000000000000..4e798d6d40ce --- /dev/null +++ b/packages/server/core/tests/base/fixtures/render/ssr/route.json @@ -0,0 +1,32 @@ +[ + { + "urlPath": "/user", + "entryName": "user", + "entryPath": "user.html", + "isSPA": true, + "isStream": false, + "isSSR": true, + "bundle": "bundles/user.js", + "redirect": "", + "responseHeader": [], + "extra": { + "entryName": "main" + }, + "specialHeader": [] + }, + { + "urlPath": "/", + "entryName": "main", + "entryPath": "index.html", + "isSPA": true, + "isStream": false, + "isSSR": true, + "bundle": "bundles/main.js", + "redirect": "", + "responseHeader": [], + "extra": { + "entryName": "main" + }, + "specialHeader": [] + } +] diff --git a/packages/server/core/tests/base/fixtures/render/ssr/user.html b/packages/server/core/tests/base/fixtures/render/ssr/user.html new file mode 100644 index 000000000000..20e43ceb8c90 --- /dev/null +++ b/packages/server/core/tests/base/fixtures/render/ssr/user.html @@ -0,0 +1,3 @@ + +

User

+ diff --git a/packages/server/core/tests/base/middlewares/dataHandler.test.ts b/packages/server/core/tests/base/middlewares/dataHandler.test.ts index a307ba2915ee..78e4f095f566 100644 --- a/packages/server/core/tests/base/middlewares/dataHandler.test.ts +++ b/packages/server/core/tests/base/middlewares/dataHandler.test.ts @@ -31,7 +31,7 @@ describe('test middlewares dataHandler', () => { expect(text).toBe('render'); }); - it('should handle by data', async () => { + it('should handle by data correctly', async () => { const server = createDefaultServer(); bindDataHandlers( @@ -42,7 +42,13 @@ describe('test middlewares dataHandler', () => { entryName: 'main', entryPath: '/', }, - ], + { + urlPath: '/user', + entryName: 'user', + entryPath: '/user', + }, + // TODO: remove sort + ].sort((a, b) => b.urlPath.length - a.urlPath.length), pwd, ); @@ -51,9 +57,11 @@ describe('test middlewares dataHandler', () => { }); const response = await server.request('/'); - const text = await response.text(); + expect(text).toBe('handle main'); - expect(text).toBe('handle data'); + const response1 = await server.request('/user/page'); + const text1 = await response1.text(); + expect(text1).toBe('handle user'); }); }); diff --git a/packages/server/core/tests/base/mock.test.ts b/packages/server/core/tests/base/middlewares/mock.test.ts similarity index 96% rename from packages/server/core/tests/base/mock.test.ts rename to packages/server/core/tests/base/middlewares/mock.test.ts index 27bf919d9968..2fec3cedcf29 100644 --- a/packages/server/core/tests/base/mock.test.ts +++ b/packages/server/core/tests/base/middlewares/mock.test.ts @@ -1,10 +1,10 @@ import path from 'path'; import { registerMockHandlers } from '@base/adapters/node/middlewares/mock'; import { createServerBase } from '@base/index'; -import { getDefaultConfig, getDefaultAppContext } from './helpers'; +import { getDefaultConfig, getDefaultAppContext } from '../helpers'; describe('should mock middleware work correctly', () => { - const pwd = path.join(__dirname, './fixtures/mock'); + const pwd = path.join(__dirname, '../fixtures/mock'); it('support cjs', async () => { const server = createServerBase({ diff --git a/packages/server/core/tests/base/middlewares/render.test.ts b/packages/server/core/tests/base/middlewares/render.test.ts index eb1989e0571e..ed68cd898581 100644 --- a/packages/server/core/tests/base/middlewares/render.test.ts +++ b/packages/server/core/tests/base/middlewares/render.test.ts @@ -1,7 +1,39 @@ import path from 'path'; -import { bindRenderHandler } from '@base/index'; -import { createServerBase, injectReporter } from '../../../src/base'; +import { + bindRenderHandler, + createServerBase, + injectReporter, +} from '@base/index'; import { getDefaultAppContext, getDefaultConfig } from '../helpers'; +import { ServerUserConfig } from '../../../src/types/config'; + +async function createSSRServer( + pwd: string, + serverConfig: ServerUserConfig = { ssr: true }, +) { + const config = getDefaultConfig(); + + config.server = serverConfig; + + const server = createServerBase({ + config, + pwd, + appContext: getDefaultAppContext(), + }); + + server.all('*', injectReporter()); + + await server.init(); + + await bindRenderHandler(server, { + pwd, + appContext: getDefaultAppContext(), + config, + routes: require(path.resolve(pwd, 'route.json')), + }); + + return server; +} describe('should render html correctly', () => { const pwd = path.join(__dirname, '../fixtures/render'); @@ -32,4 +64,53 @@ describe('should render html correctly', () => { expect(html).toMatch(/Hello Modern/); }); + + it('should render ssr correctly', async () => { + const ssrPwd = path.join(pwd, 'ssr'); + + const server = await createSSRServer(ssrPwd); + + const response = await server.request('/', {}, {}); + const html = await response.text(); + + expect(html).toBe('SSR Render'); + + const html2 = await Promise.resolve(server.request('/user', {}, {})).then( + res => res.text(), + ); + + expect(html2).toBe('SSR User Render'); + }); + + it('should force csr correctly', async () => { + const ssrPwd = path.join(pwd, 'ssr'); + const server = await createSSRServer(ssrPwd, { + ssr: { + forceCSR: true, + }, + }); + + const html1 = await Promise.resolve(server.request('/', {}, {})).then(res => + res.text(), + ); + expect(html1).toBe('SSR Render'); + + const html2 = await Promise.resolve(server.request('/?csr=1', {}, {})).then( + res => res.text(), + ); + expect(html2).toMatch(/Hello Modern/); + + const html3 = await Promise.resolve( + server.request( + '/', + { + headers: new Headers({ + 'x-modern-ssr-fallback': '1', + }), + }, + {}, + ), + ).then(res => res.text()); + expect(html3).toMatch(/Hello Modern/); + }); });