10
'use strict';
11
12
var React;
13
-var ReactTestUtils;
13
+var ReactDOM;
14
15
-var AutoMockedComponent;
15
var MockedComponent;
16
var ReactDOMServer;
17
18
describe('ReactMockedComponent', () => {
19
beforeEach(() => {
20
React = require('react');
22
- ReactTestUtils = require('react-dom/test-utils');
21
+ ReactDOM = require('react-dom');
22
ReactDOMServer = require('react-dom/server');
23
25
- AutoMockedComponent = jest.genMockFromModule(
26
- 'ReactMockedComponentTestComponent',
27
- );
28
- MockedComponent = jest.genMockFromModule(
29
- 'ReactMockedComponentTestComponent',
30
- );
31
-
32
- ReactTestUtils.mockComponent(MockedComponent);
33
- });
34
-
35
- it('should allow an implicitly mocked component to be rendered without warnings', () => {
36
- spyOn(console, 'error');
37
- ReactTestUtils.renderIntoDocument(<AutoMockedComponent />);
38
- expectDev(console.error.calls.count()).toBe(0);
39
- });
40
-
41
- it('should allow an implicitly mocked component to be rendered without warnings (SSR)', () => {
42
- spyOn(console, 'error');
43
- ReactDOMServer.renderToString(<AutoMockedComponent />);
44
- expectDev(console.error.calls.count()).toBe(0);
45
- });
46
-
47
- it('should allow an implicitly mocked component to be updated', () => {
48
- class Wrapper extends React.Component {
49
- state = {foo: 1};
50
-
51
- update = () => {
52
- this.setState({foo: 2});
53
- };
54
-
24
+ MockedComponent = class extends React.Component {
25
render() {
56
- return <div><AutoMockedComponent prop={this.state.foo} /></div>;
26
+ throw new Error('Should not get here.');
27
}
58
- }
59
-
60
- var instance = ReactTestUtils.renderIntoDocument(<Wrapper />);
61
-
62
- var found = ReactTestUtils.findRenderedComponentWithType(
63
- instance,
64
- AutoMockedComponent,
65
- );
66
- expect(typeof found).toBe('object');
67
-
68
- instance.update();
69
- });
70
-
71
- it('has custom methods on the implicitly mocked component', () => {
72
- var instance = ReactTestUtils.renderIntoDocument(<AutoMockedComponent />);
73
- expect(typeof instance.hasCustomMethod).toBe('function');
28
+ };
29
+ // This is close enough to what a Jest mock would give us.
30
+ MockedComponent.prototype.render = jest.fn();
31
});
32
76
- it('should allow an explicitly mocked component to be rendered', () => {
77
- ReactTestUtils.renderIntoDocument(<MockedComponent />);
33
+ it('should allow a mocked component to be rendered', () => {
34
+ var container = document.createElement('container');
35
+ ReactDOM.render(<MockedComponent />, container);
36
});
37
80
- it('should allow an explicitly mocked component to be updated', () => {
81
- class Wrapper extends React.Component {
82
- state = {foo: 1};
83
-
84
- update = () => {
85
- this.setState({foo: 2});
86
- };
87
-
88
- render() {
89
- return <div><MockedComponent prop={this.state.foo} /></div>;
90
- }
91
- }
92
-
93
- var instance = ReactTestUtils.renderIntoDocument(<Wrapper />);
94
-
95
- var found = ReactTestUtils.findRenderedComponentWithType(
96
- instance,
97
- MockedComponent,
98
- );
99
- expect(typeof found).toBe('object');
100
-
101
- instance.update();
38
+ it('should allow a mocked component to be updated', () => {
39
+ var container = document.createElement('container');
40
+ ReactDOM.render(<MockedComponent />, container);
41
+ ReactDOM.render(<MockedComponent />, container);
42
});
43
104
- it('has custom methods on the explicitly mocked component', () => {
105
- var instance = ReactTestUtils.renderIntoDocument(<MockedComponent />);
106
- expect(typeof instance.hasCustomMethod).toBe('function');
44
+ it('should allow a mocked component to be rendered (SSR)', () => {
45
+ ReactDOMServer.renderToString(<MockedComponent />);
46
});
47
});