首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何基于材料ui测试组件?

如何基于材料ui测试组件?
EN

Stack Overflow用户
提问于 2018-04-11 11:12:03
回答 1查看 2.4K关注 0票数 1

我有以下组件,即用https://material-ui-next.com/构建。

代码语言:javascript
复制
import React from 'react';
import { AppBar, Toolbar } from 'material-ui';
import { Typography } from 'material-ui';
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
import {lightBlue} from 'material-ui/colors';

const theme = createMuiTheme({
  palette: {
    primary: {
      main:lightBlue['A700']
    },
    text: {
      primary: '#fff',
    }
  },
});

const View = (props) => (
  <MuiThemeProvider theme={theme}>
    <AppBar position="static">
      <Toolbar>
      <Typography variant="title">
        {props.title}
      </Typography>          
      </Toolbar>
    </AppBar>
  </MuiThemeProvider>
);
export default View;  

我正试着为它写一个测试:

代码语言:javascript
复制
import React from 'react';
import { shallow } from 'enzyme';
import View from '../Views/View';
import { Typography } from 'material-ui';

it('renders', () => {
  const wrapper = shallow(<View title='Welcome' />);
  expect(wrapper.find('Typography').text()).toEqual('Welcome');
});  

如何为组件编写测试,即使用material-ui组件?在上面的例子中,我试图找出组件是否包含Welcome

我读过https://material-ui-next.com/guides/testing/,但还不清楚,我怎么写一个测试。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-11 12:29:54

API从shallow mount

你试过使用他们的API 在此描述吗?也许您的测试结果如下所示:

代码语言:javascript
复制
import React from 'react';
import { createMount } from 'material-ui/test-utils';
import View from '../Views/View';
import { Typography } from 'material-ui';

it('renders', () => {
  const mount = createMount();
  const wrapper = mount(<View title='Welcome' />);
  expect(wrapper.find('Typography').text()).toEqual('Welcome');
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49773593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档