You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.8 KiB
89 lines
2.8 KiB
5 years ago
|
/* eslint-disable no-await-in-loop */
|
||
|
/* eslint-disable no-restricted-syntax */
|
||
2 years ago
|
import cheerio from 'cheerio';
|
||
|
import { globSync } from 'glob';
|
||
|
import type http from 'http';
|
||
|
import { createServer } from 'http-server';
|
||
|
import type https from 'https';
|
||
|
import fetch from 'isomorphic-fetch';
|
||
|
import uniq from 'lodash/uniq';
|
||
|
import { join } from 'path';
|
||
5 years ago
|
|
||
|
const components = uniq(
|
||
2 years ago
|
globSync('components/!(overview)/*.md', { cwd: join(process.cwd()), dot: false }).map((path) =>
|
||
|
path.replace(/(\/index)?((\.zh-cn)|(\.en-us))?\.md$/i, ''),
|
||
|
),
|
||
5 years ago
|
);
|
||
|
|
||
|
describe('site test', () => {
|
||
2 years ago
|
let server: http.Server | https.Server;
|
||
5 years ago
|
const port = 3000;
|
||
2 years ago
|
const render = async (path: string) => {
|
||
2 years ago
|
const resp = await fetch(`http://127.0.0.1:${port}${path}`).then(async (res) => {
|
||
5 years ago
|
const html = await res.text();
|
||
|
const $ = cheerio.load(html, { decodeEntities: false, recognizeSelfClosing: true });
|
||
2 years ago
|
return { html, status: res.status, $ };
|
||
5 years ago
|
});
|
||
|
return resp;
|
||
|
};
|
||
5 years ago
|
|
||
2 years ago
|
const handleComponentName = (name: string) => {
|
||
|
const [, componentName] = name.split('/');
|
||
2 years ago
|
return componentName.toLowerCase().replace('-cn', '').replace('-', '');
|
||
5 years ago
|
};
|
||
|
|
||
2 years ago
|
const expectComponent = async (component: string) => {
|
||
5 years ago
|
const { status, $ } = await render(`/${component}/`);
|
||
|
expect(status).toBe(200);
|
||
2 years ago
|
expect($('h1').text().toLowerCase()).toMatch(handleComponentName(component));
|
||
5 years ago
|
};
|
||
|
|
||
|
beforeAll(() => {
|
||
2 years ago
|
server = createServer({ root: join(process.cwd(), '_site') });
|
||
5 years ago
|
server.listen(port);
|
||
5 years ago
|
// eslint-disable-next-line no-console
|
||
5 years ago
|
console.log('site static server run: http://localhost:3000');
|
||
|
});
|
||
|
|
||
|
afterAll(() => {
|
||
2 years ago
|
server?.close();
|
||
5 years ago
|
});
|
||
|
|
||
2 years ago
|
it('Basic Pages en', async () => {
|
||
5 years ago
|
const { status, $ } = await render('/');
|
||
5 years ago
|
expect($('title').text()).toEqual(
|
||
|
`Ant Design - The world's second most popular React UI framework`,
|
||
|
);
|
||
5 years ago
|
expect(status).toBe(200);
|
||
|
});
|
||
|
|
||
2 years ago
|
it('Basic Pages zh', async () => {
|
||
5 years ago
|
const { status, $ } = await render('/index-cn');
|
||
5 years ago
|
expect($('title').text()).toEqual(`Ant Design - 一套企业级 UI 设计语言和 React 组件库`);
|
||
5 years ago
|
expect(status).toBe(200);
|
||
|
});
|
||
|
|
||
2 years ago
|
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(`组件总览`);
|
||
|
});
|
||
|
|
||
5 years ago
|
for (const component of components) {
|
||
5 years ago
|
if (component.split('/').length < 3) {
|
||
|
it(`Component ${component} zh Page`, async () => {
|
||
2 years ago
|
await expectComponent(`${component}-cn`);
|
||
5 years ago
|
});
|
||
|
it(`Component ${component} en Page`, async () => {
|
||
|
await expectComponent(component);
|
||
|
});
|
||
|
}
|
||
5 years ago
|
}
|
||
|
});
|