我是新来的反应和玩笑。我一直在到处寻找测试,但我找不到任何有用的东西。这部分是因为我对它太陌生了,我不知道从哪里开始。所以请容忍我。
我有一个add to cart文件,它呈现一个带有按钮的表单。按钮是另一个组件,所以我不打算测试它。我必须测试表单的onSubmit函数。有什么想法吗?推荐人?
到目前为止,我的测试代码如下:
describe('AddToCart', () => {
const React = require('react');
const BaseRenderer = require('react/lib/ReactTestUtils');
const Renderer = BaseRenderer.createRenderer();
const ReactTestUtils = require('react-addons-test-utils');
const AddToCart = require('../index.js').BaseAddToCart;
it('Will Submit', () => {
formInstance = ReactTestUtils.renderIntoDocument(<AddToCart product="" quantity=""/>);
expect(ReactTestUtils.Simulate.onSubmit(formInstance)).toBeCalled();
});
});我得到了一个错误:
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.发布于 2016-10-27 22:14:14
考虑将取笑与酶结合使用。我认为这是一个很好的栈单元测试在反应。
此外,我还做了一个示例测试,测试onSubmit组件中的LogIn函数。
import React from 'react';
import {shallow} from 'enzyme';
import LogIn from './LogIn';
describe('<LogIn />', () => {
const testValues = {
username: 'FOO',
password: 'BAZ',
handleSubmit: jest.fn(),
};
it('Submit works', () => {
const component = shallow(
<LogIn {...testValues} />
);
component.find('#submitButton').simulate('click');
expect(testValues.handleSubmit).toHaveBeenCalledTimes(1);
expect(testValues.handleSubmit).toBeCalledWith({username: testValues.username, password: testValues.password});
});
});https://stackoverflow.com/questions/40291610
复制相似问题