import '@testing-library/jest-dom';
import React from 'react';
import { render, queryByAttribute, fireEvent } from '@testing-library/react';
import type { MemoryHistory} from '@umijs/runtime';
import { createMemoryHistory, Router } from '@umijs/runtime';
import { context as Context } from 'dumi/theme';
import SourceCode from '../builtins/SourceCode';
import Alert from '../builtins/Alert';
import Badge from '../builtins/Badge';
import Previewer from '../builtins/Previewer';
import API from '../builtins/API';
import Layout from '../layout';
let history: MemoryHistory;
// mock history location which import from 'dumi'
jest.mock('dumi', () => ({
history: { location: { pathname: '/' } },
}));
describe('default theme', () => {
history = createMemoryHistory({ initialEntries: ['/', '/en-US'], initialIndex: 0 });
const baseCtx = {
title: 'test',
locale: 'zh-CN',
routes: [
{
path: '/',
title: '首页',
meta: {},
},
{
path: '/en-US',
title: 'Home',
meta: {},
},
],
config: {
locales: [
{ name: 'zh-CN', label: '中文' },
{ name: 'en-US', label: 'English' },
],
menus: {},
navs: {},
title: 'test',
logo: '/',
mode: 'site' as 'doc' | 'site',
repository: { branch: 'mater' },
},
meta: {},
menu: [
{
title: '分组',
children: [
{
title: 'English',
path: '/en',
},
],
},
],
nav: [
{
path: '/',
title: '首页',
children: [],
},
{
title: '生态',
children: [
{
path: 'https://d.umijs.org',
title: 'GitHub',
children: [],
},
],
},
],
base: '/',
};
const baseProps = {
history,
location: { ...history.location, query: {} },
match: { params: {}, isExact: true, path: '/', url: '/' },
route: { path: '/', routes: baseCtx.routes },
};
it('should render site home page', () => {
const wrapper = ({ children }) => (
{children}
);
const { container, getAllByText, getByText } = render(
Home Page
,
{ wrapper },
);
// expect navbar be rendered
expect(getAllByText('首页')).not.toBeNull();
// expect content be rendered
expect(getByText('Home Page')).not.toBeNull();
// expect hero be rendered
expect(getByText('Hero')).not.toBeNull();
// expect features be rendered
expect(getByText('Feature')).not.toBeNull();
expect(getByText('Feat2')).not.toBeNull();
// trigger mobile menu display
queryByAttribute('class', container, '__dumi-default-navbar-toggle').click();
// expect sidemenu display for mobile
expect(queryByAttribute('data-mobile-show', container, 'true')).not.toBeNull();
});
it('should render documentation page', async () => {
const updatedTime = 1604026996000;
const wrapper = ({ children }) => (
{children}
);
const { getByText, getAllByText } = render(
Doc
,
{ wrapper },
);
// expect slugs be rendered
expect(getByText('Slug A')).not.toBeNull();
// expect footer date show
expect(new Date(updatedTime).toLocaleString([], { hour12: false })).not.toBeNull();
// trigger locale change
getAllByText('English')[0].click();
// expect location change
expect(history.location.pathname).toEqual(baseCtx.routes[1].path);
});
it('should render builtin components correctly', () => {
const code = "console.log('Hello World!')";
const wrapper = ({ children }) => (
{children}
);
const { getByText, getByTitle, getAllByTitle, container } = render(
<>
click
Alert
Badge
'JavaScript'",
tsx: "export default () => 'TypeScript'",
},
}}
dependencies={{}}
>
<>demo-1 Content>
'Main'",
},
'Other.jsx': {
import: './Other.jsx',
content: "export default () => 'Other'",
},
}}
dependencies={{}}
>
<>demo-2 Content>
'Main'",
},
'Other.jsx': {
import: './Other.jsx',
content: "export default () => 'Other'",
},
}}
dependencies={{}}
iframe={100}
>
<>demo-3 Content>
>
,
{ wrapper },
);
// toggle side menu display
fireEvent(
container.querySelector('.__dumi-default-navbar-toggle'),
new MouseEvent('click', {
bubbles: true,
cancelable: true,
}),
);
fireEvent(
container.querySelector('#btn'),
new MouseEvent('click', {
bubbles: true,
cancelable: true,
}),
);
// expect SourceCode highlight
expect(getByText('console')).toHaveClass('token');
// expect Alert be rendered
expect(getByText('Alert')).toHaveAttribute('type', 'info');
// expect Badge be rendered
expect(getByText('Badge')).toHaveClass('__dumi-default-badge');
// expect Previewer be rendered
expect(getByText('demo-1')).not.toBeNull();
// trigger source code display for demo-1
getAllByTitle('Toggle source code panel')[0].click();
// expect show TypeScript code default
expect(getByText("'TypeScript'")).not.toBeNull();
// trigger source code display for demo-2
getAllByTitle('Toggle source code panel')[1].click();
// expect show code of main file
expect(getByText("'Main'")).not.toBeNull();
// trigger file change
getByText('Other.jsx').click();
// expect show code of main file
expect(getByText("'Other'")).not.toBeNull();
// expect render iframe demo
(container.querySelector('[data-iframe] button[role=refresh]') as HTMLElement).click();
expect(container.querySelector('[data-iframe]').innerHTML).not.toContain('demo-3 Content');
expect(container.querySelector('[data-iframe] iframe')).not.toBeNull();
expect((container.querySelector('[data-iframe] iframe') as HTMLElement).style.height).toEqual(
'100px',
);
// expect render API property
expect(getByText('other', { selector: 'table td' })).not.toBeNull();
});
});