forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-site.ts
executable file
·110 lines (95 loc) · 3.55 KB
/
check-site.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
import type http from 'http';
import type https from 'https';
import { join } from 'path';
import cheerio from 'cheerio';
import { globSync } from 'glob';
import { createServer } from 'http-server';
import fetch from 'isomorphic-fetch';
import uniq from 'lodash/uniq';
const components = uniq(
globSync('components/!(overview)/*.md', { cwd: join(process.cwd()), dot: false }).map((path) =>
path.replace(/(\/index)?((\.zh-cn)|(\.en-us))?\.md$/i, ''),
),
);
describe('site test', () => {
let server: http.Server | https.Server;
const port = 3000;
const render = async (path: string) => {
const resp = await fetch(`http://127.0.0.1:${port}${path}`).then(async (res) => {
const html = await res.text();
const $ = cheerio.load(html, { decodeEntities: false, recognizeSelfClosing: true });
return { html, status: res.status, $ };
});
return resp;
};
const handleComponentName = (name: string) => {
const [, componentName] = name.split('/');
return componentName.toLowerCase().replace('-cn', '').replace('-', '');
};
const expectComponent = async (component: string) => {
const { status, $ } = await render(`/${component}/`);
expect(status).toBe(200);
expect($('h1').text().toLowerCase()).toMatch(handleComponentName(component));
/**
* 断言组件的 api table 数量是否符合预期。
* 在 #45066, #45017 中,因为 markdown 写法问题,导致 api table 无法渲染。
* 结合每个组件页的 table 数量变动,可以判断出是否存在问题。
* (table 数量相对比较稳定,如果 PR 有新增,则应该更新这里快照)
*/
const tables = $('.markdown table');
expect(tables.length).toMatchSnapshot();
};
beforeAll(() => {
server = createServer({ root: join(process.cwd(), '_site') });
server.listen(port);
// eslint-disable-next-line no-console
console.log('site static server run: http://localhost:3000');
});
afterAll(() => {
server?.close();
});
it('Basic Pages en', async () => {
const { status, $ } = await render('/');
expect($('title').text()).toEqual(
`Ant Design - The world's second most popular React UI framework`,
);
expect(status).toBe(200);
});
it('Basic Pages zh', async () => {
const { status, $ } = await render('/index-cn');
expect($('title').text()).toEqual(`Ant Design - 一套企业级 UI 设计语言和 React 组件库`);
expect(status).toBe(200);
});
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(`组件总览`);
});
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(`资源`);
});
for (const component of components) {
if (component.split('/').length < 3) {
it(`Component ${component} zh Page`, async () => {
await expectComponent(`${component}-cn`);
});
it(`Component ${component} en Page`, async () => {
await expectComponent(component);
});
}
}
});