是否可以用jest测试react应用程序的入口点?
Is it possible to test your react app entry point with jest?
我有一个react应用程序,它有以下入口点:
// Import dependencies
import React from 'react';
import { render } from 'react-dom';
import { browserHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import configureStore from './store/configureStore';
import Root from './containers/Root';
const store = configureStore({});
const history = syncHistoryWithStore(browserHistory, store);
render(
<Root store={store} history={history} />,
document.getElementById('react')
);
一个非常常见的配置。我想知道您将如何测试这样的东西,因为它不导出任何东西,jest依赖于导入您想要测试的内容。
首先,您可能不需要测试这个,但您绝对可以。
如果你真的想要这样做,你需要重构你的代码,以便有一个函数来测试:
// Import dependencies
import React from 'react';
import { render } from 'react-dom';
import { browserHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import configureStore from './store/configureStore';
import Root from './containers/Root';
export const app = () => {
const store = configureStore({});
const history = syncHistoryWithStore(browserHistory, store);
render(
<Root store={store} history={history} />,
document.getElementById('react')
);
};
然后你可以使用react测试库之类的东西来确保你将Root
组件安装在适当的元素中:
import { render, screen } from '@testing-library/react';
import { app } from './app';
test('app should render the Root component into the expected element', () => {
// render the document with the element we plan on using for our react app
render(<div id="react" />);
// invoke the function that will render our component into that element
app();
// assert that the component exists in the document
// you can mock `Root` to be some element you expect to see in the test
// for the sake of this example, since I know nothing about `Root`
// let's assume it has some identifying text
expect(screen.getByText('rooty tooty toot toot')).toBeInTheDocument();
});
因此,这将允许我们在入口点输入正在完成的工作,但现在它是一个需要调用的函数!要使应用程序工作,入口点需要导入app
函数并调用它:
import { app } from './app';
app();
相关文章:
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- Emberjs应用程序加载在除Index之外的所有路由上
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- angular 1.5应用程序中的导航栏
- 在Web应用程序中使用Highcharts javascript
- angularjs+rails应用程序中未显示模板
- 使用acess代币登录Facebook,并通过网络应用程序离线发布
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 科尔多瓦页面类应用程序中的多个谷歌地图
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- Angularjs-utils高亮过滤器在搜索时破坏应用程序
- 具有多个入口点的Marionette应用程序
- 如何使用具有多个入口点的 Webpack 和 Gulp 来转译应用程序和测试目录
- 如何减少Gwt多入口点项目应用程序的初始加载时间
- 如何创建节点应用程序侦听主机入口域
- 是否可以用jest测试react应用程序的入口点?
- 为两个相似的react应用程序使用两个不同的入口点
- 我们可以为单页应用程序提供两个入口点吗?