Skip to content

Commit

Permalink
chore: add more test (#5429)
Browse files Browse the repository at this point in the history
  • Loading branch information
GiveMe-A-Name authored Feb 22, 2024
1 parent 9b97082 commit b200772
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 13 deletions.
6 changes: 2 additions & 4 deletions packages/server/core/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// 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} */
module.exports = {
...sharedConfig,
rootDir: __dirname,
globals: {
TextEncoder,
TextDecoder,
// FIXME: inject Headers polyfill
},
testEnvironment: 'node',
moduleNameMapper: {
'^@base/(.*)$': '<rootDir>/src/base/$1',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const defaultReporter: Reporter = {

interface SSRRenderOptions {
pwd: string;
// FIXME: remove mode.
mode: 'string' | 'stream';
html: string;
routeInfo: ServerRoute;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export async function handleRequest() {
// eslint-disable-next-line no-undef
return new Response('handle data');
return new Response('handle main');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function handleRequest() {
// eslint-disable-next-line no-undef
return new Response('handle user');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function serverRender() {
return 'SSR Render';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function serverRender() {
return 'SSR User Render';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<body>
<h3>Hello Modern</h3>
</body>
32 changes: 32 additions & 0 deletions packages/server/core/tests/base/fixtures/render/ssr/route.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
3 changes: 3 additions & 0 deletions packages/server/core/tests/base/fixtures/render/ssr/user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<body>
<h3>User</h3>
</body>
16 changes: 12 additions & 4 deletions packages/server/core/tests/base/middlewares/dataHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
);

Expand All @@ -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');
});
});
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
85 changes: 83 additions & 2 deletions packages/server/core/tests/base/middlewares/render.test.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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/);
});
});

0 comments on commit b200772

Please sign in to comment.