2019-10-18 21:27:36 +08:00
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
/* eslint-disable no-restricted-syntax */
|
2021-09-26 18:21:15 +08:00
|
|
|
const fetch = require('isomorphic-fetch');
|
2019-10-18 21:27:36 +08:00
|
|
|
const { join } = require('path');
|
|
|
|
const cheerio = require('cheerio');
|
|
|
|
const glob = require('glob');
|
|
|
|
const uniq = require('lodash/uniq');
|
|
|
|
const { createServer } = require('http-server');
|
|
|
|
|
|
|
|
const components = uniq(
|
|
|
|
glob
|
2023-03-13 21:19:14 +08:00
|
|
|
.globSync('components/!(overview)/*.md', {
|
2019-10-18 21:27:36 +08:00
|
|
|
cwd: join(process.cwd()),
|
|
|
|
dot: false,
|
|
|
|
})
|
2022-11-17 01:22:09 +08:00
|
|
|
.map((path) => path.replace(/(\/index)?((\.zh-cn)|(\.en-us))?\.md$/i, '')),
|
2019-10-18 21:27:36 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
describe('site test', () => {
|
|
|
|
let server;
|
2019-11-11 14:30:11 +08:00
|
|
|
const port = 3000;
|
2022-11-17 01:22:09 +08:00
|
|
|
const render = async (path) => {
|
|
|
|
const resp = await fetch(`http://127.0.0.1:${port}${path}`).then(async (res) => {
|
2019-10-18 21:27:36 +08:00
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html, { decodeEntities: false, recognizeSelfClosing: true });
|
|
|
|
return {
|
|
|
|
html,
|
|
|
|
status: res.status,
|
|
|
|
$,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return resp;
|
|
|
|
};
|
2019-12-18 12:09:05 +08:00
|
|
|
|
2022-11-17 01:22:09 +08:00
|
|
|
const handleComponentName = (name) => {
|
2019-12-18 12:09:05 +08:00
|
|
|
const componentName = name.split('/')[1];
|
2023-02-08 22:54:21 +08:00
|
|
|
return componentName.toLowerCase().replace('-cn', '').replace('-', '');
|
2019-10-18 21:27:36 +08:00
|
|
|
};
|
|
|
|
|
2022-11-17 01:22:09 +08:00
|
|
|
const expectComponent = async (component) => {
|
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));
|
2019-10-18 21:27:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
server = createServer({
|
|
|
|
root: join(process.cwd(), '_site'),
|
|
|
|
});
|
2019-11-11 14:30:11 +08:00
|
|
|
server.listen(port);
|
2019-10-22 11:47:09 +08:00
|
|
|
// eslint-disable-next-line no-console
|
2019-10-18 21:27:36 +08:00
|
|
|
console.log('site static server run: http://localhost:3000');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
if (server) {
|
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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('/');
|
2020-04-24 18:48:05 +08:00
|
|
|
expect($('title').text()).toEqual(
|
|
|
|
`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');
|
2020-04-24 18:48:05 +08:00
|
|
|
expect($('title').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(`组件总览`);
|
|
|
|
});
|
|
|
|
|
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`);
|
2020-02-28 12:09:00 +08:00
|
|
|
});
|
2019-10-18 21:27:36 +08:00
|
|
|
|
2020-02-28 12:09:00 +08:00
|
|
|
it(`Component ${component} en Page`, async () => {
|
|
|
|
await expectComponent(component);
|
|
|
|
});
|
|
|
|
}
|
2019-10-18 21:27:36 +08:00
|
|
|
}
|
|
|
|
});
|