javascript - 如何测试 ThemeProvider/withStyle 包裹的 React 组件?

标签 javascript reactjs jestjs material-ui enzyme

我想测试一个由 withStyle 包裹的组件,但看起来主题对象没有经过该组件。 我很好奇是否有任何好的做法可以做到这一点。

我想尝试使用createShallow()和dive()来访问

//GameBoard.test.js
import React from 'react';
import { createShallow } from '@material-ui/core/test-utils'
import GameBoard from './GameBoard';
import { ThemeProvider} from '@material-ui/styles';
import { createMuiTheme } from '@material-ui/core'

describe('GameBoard', () => {

  const theme = createMuiTheme({
    background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
    typography: {
      htmlFontSize: 18
    }
  });

  let shallow

  const MockTheme = ({ children }) => {
    return (
      <ThemeProvider theme={theme}>
        {children}
      </ThemeProvider>
    );
  }

  beforeAll(() => {
    shallow = createShallow()
  })



  it('renders without crashing', () => {
    const board = shallow(<MockTheme><GameBoard/></MockTheme>)
  })

  it('random answer is between 0 and 100', () => {
    const board = shallow(<MockTheme><GameBoard/></MockTheme>)
    board.find(GameBoard).dive()
    //this is what I really want to try, to access its component method:
    board.find(GameBoard).dive().instance().setConfig(0, 100)
    //expect(board.state('answer')).toBeGreaterThanOrEqual(0)
    //expect(board.state('answer')).toBeLessThanOrEqual(100)
  })
})

我的 GameBoard 组件是用

导出的
export default withStyles(styles)(GameBoard)

App.js,其父组件


const theme = createMuiTheme({
  background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
  typography: {
    htmlFontSize: 18
  }
});

const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <GameBoard/>
    </ThemeProvider>
  );
}

export default App;

样式

const styles = theme => createStyles({
  root: {
    flexGrow: 1,
    marginBottom: theme.spacing(3),
  },

})

测试结果

FAIL  src/GameBoard.test.js
  ● Console

    console.error node_modules/warning/warning.js:34
      Warning: Material-UI: the `styles` argument provided is invalid.
      You are providing a function without a theme in the context.
      One of the parent elements needs to use a ThemeProvider.

  ● GameBoard › random answer is between 0 and 100

    TypeError: theme.spacing is not a function

      4 |   root: {
      5 |     flexGrow: 1,
    > 6 |     marginBottom: theme.spacing(3),
        |                         ^
      7 |   },
      8 |   paper: {
      9 |     height: "100%",

最佳答案

刚刚找到了一种使用 mount 并找到其子组件的方法,我认为这可以解决问题。

describe('GameBoard', () => {

  const theme = createMuiTheme({
    background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
    typography: {
      htmlFontSize: 18
    }
  });

  let mount

  const MockTheme = ({ children }) => {
    return (
      <ThemeProvider theme={theme}>
        {children}
      </ThemeProvider>
    );
  }

  beforeAll(() => {
    mount = createMount()
  })



  it('renders without crashing', () => {
    const board = mount(<MockTheme><GameBoard/></MockTheme>)
  })

  it('random answer is between 0 and 100', () => {
    const boardWrapper = mount(<MockTheme><GameBoard/></MockTheme>)
    const board = boardWrapper.find("GameBoard").at(0)
    board.instance().setNewConfig(0, 100)
    expect(board.state('answer')).toBeGreaterThanOrEqual(0)
    expect(board.state('answer')).toBeLessThanOrEqual(100)
  })
})

关于javascript - 如何测试 ThemeProvider/withStyle 包裹的 React 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56996984/

相关文章:

javascript - 下面给出的代码中的我的 javascript 函数被调用两次

javascript - <a> 标签内的 <a> 标签?

javascript - 如何在 React JS 上通过 Ajax 发布新数据

javascript - 单元测试依赖于其他 getter 的 Vuex getter

javascript - enzyme /Jest 测试错误 - 不变违规 : Target container is not a DOM element

Javascript:遍历 &lt;iframe&gt; 的高度对某些返回 0,所有高度都> 0

javascript - Angular 2自定义验证器如何设置control.valid true?

javascript - React.js 输入性能

javascript - 在 React 中,更新嵌套数组状态项的正确方法是什么

javascript - Jest 模拟第三方对象