2023-07-28 16:17:43 +08:00
|
|
|
|
import type http from 'http';
|
|
|
|
|
import type https from 'https';
|
|
|
|
|
import { join } from 'path';
|
2023-10-15 21:02:36 +08:00
|
|
|
|
import { load } from 'cheerio';
|
2023-04-28 19:24:32 +08:00
|
|
|
|
import { globSync } from 'glob';
|
|
|
|
|
import { createServer } from 'http-server';
|
|
|
|
|
import fetch from 'isomorphic-fetch';
|
|
|
|
|
import uniq from 'lodash/uniq';
|
2019-10-18 21:27:36 +08:00
|
|
|
|
|
|
|
|
|
const components = uniq(
|
2023-04-28 19:24:32 +08:00
|
|
|
|
globSync('components/!(overview)/*.md', { cwd: join(process.cwd()), dot: false }).map((path) =>
|
|
|
|
|
path.replace(/(\/index)?((\.zh-cn)|(\.en-us))?\.md$/i, ''),
|
|
|
|
|
),
|
2024-03-21 15:55:40 +08:00
|
|
|
|
).filter((component) => !component.includes('_util'));
|
2019-10-18 21:27:36 +08:00
|
|
|
|
|
|
|
|
|
describe('site test', () => {
|
2023-04-28 19:24:32 +08:00
|
|
|
|
let server: http.Server | https.Server;
|
2019-11-11 14:30:11 +08:00
|
|
|
|
const port = 3000;
|
2023-04-28 19:24:32 +08:00
|
|
|
|
const render = async (path: string) => {
|
2022-11-17 01:22:09 +08:00
|
|
|
|
const resp = await fetch(`http://127.0.0.1:${port}${path}`).then(async (res) => {
|
2023-10-15 21:02:36 +08:00
|
|
|
|
const html: string = await res.text();
|
2024-08-11 17:39:06 +08:00
|
|
|
|
const $ = load(html, { xml: true });
|
2023-10-15 21:02:36 +08:00
|
|
|
|
return { status: res.status, $ };
|
2019-10-18 21:27:36 +08:00
|
|
|
|
});
|
|
|
|
|
return resp;
|
|
|
|
|
};
|
2019-12-18 12:09:05 +08:00
|
|
|
|
|
2023-04-28 19:24:32 +08:00
|
|
|
|
const handleComponentName = (name: string) => {
|
|
|
|
|
const [, componentName] = name.split('/');
|
2023-02-08 22:54:21 +08:00
|
|
|
|
return componentName.toLowerCase().replace('-cn', '').replace('-', '');
|
2019-10-18 21:27:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-04-28 19:24:32 +08:00
|
|
|
|
const expectComponent = async (component: string) => {
|
2019-10-18 21:27:36 +08:00
|
|
|
|
const { status, $ } = await render(`/${component}/`);
|
|
|
|
|
expect(status).toBe(200);
|
2023-02-08 22:54:21 +08:00
|
|
|
|
expect($('h1').text().toLowerCase()).toMatch(handleComponentName(component));
|
2023-09-28 14:18:09 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 断言组件的 api table 数量是否符合预期。
|
|
|
|
|
* 在 #45066, #45017 中,因为 markdown 写法问题,导致 api table 无法渲染。
|
|
|
|
|
* 结合每个组件页的 table 数量变动,可以判断出是否存在问题。
|
|
|
|
|
* (table 数量相对比较稳定,如果 PR 有新增,则应该更新这里快照)
|
|
|
|
|
*/
|
|
|
|
|
const tables = $('.markdown table');
|
|
|
|
|
|
|
|
|
|
expect(tables.length).toMatchSnapshot();
|
2019-10-18 21:27:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
beforeAll(() => {
|
2023-04-28 19:24:32 +08:00
|
|
|
|
server = createServer({ root: join(process.cwd(), '_site') });
|
2019-11-11 14:30:11 +08:00
|
|
|
|
server.listen(port);
|
2024-09-19 03:30:19 +08:00
|
|
|
|
|
2023-10-15 20:32:05 +08:00
|
|
|
|
console.log(`site static server run: http://localhost:${port}`);
|
2019-10-18 21:27:36 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(() => {
|
2023-04-28 19:24:32 +08:00
|
|
|
|
server?.close();
|
2019-10-18 21:27:36 +08:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-17 01:22:09 +08:00
|
|
|
|
it('Basic Pages en', async () => {
|
2019-10-18 21:27:36 +08:00
|
|
|
|
const { status, $ } = await render('/');
|
2024-06-22 21:59:12 +08:00
|
|
|
|
expect($('title').first().text()).toEqual(
|
2020-04-24 18:48:05 +08:00
|
|
|
|
`Ant Design - The world's second most popular React UI framework`,
|
|
|
|
|
);
|
2019-10-18 21:27:36 +08:00
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-17 01:22:09 +08:00
|
|
|
|
it('Basic Pages zh', async () => {
|
2019-10-18 21:27:36 +08:00
|
|
|
|
const { status, $ } = await render('/index-cn');
|
2024-06-22 21:59:12 +08:00
|
|
|
|
expect($('title').first().text()).toEqual(`Ant Design - 一套企业级 UI 设计语言和 React 组件库`);
|
2019-10-18 21:27:36 +08:00
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-08 22:54:21 +08:00
|
|
|
|
it('Overview en', async () => {
|
|
|
|
|
const { status, $ } = await render('/components/overview');
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
expect($('h1').text()).toMatch(`Overview`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Overview zh', async () => {
|
|
|
|
|
const { status, $ } = await render('/components/overview-cn');
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
expect($('h1').text()).toMatch(`组件总览`);
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-25 15:15:42 +08:00
|
|
|
|
it('Resource en', async () => {
|
|
|
|
|
const { status, $ } = await render('/docs/resources');
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
expect($('h1').text()).toMatch(`Resources`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Resource zh', async () => {
|
|
|
|
|
const { status, $ } = await render('/docs/resources-cn');
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
expect($('h1').text()).toMatch(`资源`);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-18 21:27:36 +08:00
|
|
|
|
for (const component of components) {
|
2020-02-28 12:09:00 +08:00
|
|
|
|
if (component.split('/').length < 3) {
|
|
|
|
|
it(`Component ${component} zh Page`, async () => {
|
2023-02-08 22:54:21 +08:00
|
|
|
|
await expectComponent(`${component}-cn`);
|
2024-09-19 03:30:19 +08:00
|
|
|
|
expect(component).toBeTruthy();
|
2020-02-28 12:09:00 +08:00
|
|
|
|
});
|
|
|
|
|
it(`Component ${component} en Page`, async () => {
|
|
|
|
|
await expectComponent(component);
|
2024-09-19 03:30:19 +08:00
|
|
|
|
expect(component).toBeTruthy();
|
2020-02-28 12:09:00 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2019-10-18 21:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
});
|