-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathrender.test.js
177 lines (145 loc) · 4.24 KB
/
render.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* eslint-env jest */
import React from 'react';
import Enzyme, {render} from 'enzyme';
import omitBy from 'lodash/omitBy';
import Adapter from 'enzyme-adapter-react-16';
import renderToJson from '../src/render';
import {
BasicPure,
BasicWithAList,
ArrayRender,
FragmentAsChild,
FragmentAsRoot,
InlineScript,
ExternalScript,
} from './fixtures/pure-function';
import {
BasicClass,
ClassWithNull,
ClassWithNullChildren,
ClassArrayRender,
} from './fixtures/class';
Enzyme.configure({adapter: new Adapter()});
it('doesnt break when called without arguments', () => {
expect(renderToJson()).toBe(null);
expect(renderToJson([null])).toBe(null);
expect(renderToJson(['a'])).toBe(null);
});
it('converts basic pure render', () => {
const rendered = render(
<BasicPure className="pure">
<strong>Hello!</strong>
</BasicPure>,
);
expect(renderToJson(rendered)).toMatchSnapshot();
});
it('converts basic class render', () => {
const rendered = render(
<BasicClass className="class">
<strong>Hello!</strong>
</BasicClass>,
);
expect(renderToJson(rendered)).toMatchSnapshot();
});
it('renders the whole list', () => {
const wrapper = render(<BasicWithAList />);
expect(renderToJson(wrapper.find('ul'))).toMatchSnapshot();
});
it('handles a component which returns null', () => {
const rendered = render(<ClassWithNull />);
expect(renderToJson(rendered)).toMatchSnapshot();
});
it('handles a component having null children', () => {
const rendered = render(<ClassWithNullChildren />);
expect(renderToJson(rendered)).toMatchSnapshot();
});
it('renders multiple elements as a result of find', () => {
const rendered = render(<BasicWithAList />);
expect(renderToJson(rendered.find('li'))).toMatchSnapshot();
});
it('converts function components with render returning top level arrays', () => {
const rendered = render(
<ArrayRender>
<strong>Hello!</strong>
</ArrayRender>,
);
expect(renderToJson(rendered)).toMatchSnapshot();
});
it('converts class components with render returning top level arrays', () => {
const rendered = render(
<ClassArrayRender>
<strong>Hello!</strong>
</ClassArrayRender>,
);
expect(renderToJson(rendered)).toMatchSnapshot();
});
it('accepts a map option allowing to customize content', () => {
const rendered = render(<strong>Hello!</strong>);
expect(
renderToJson(rendered, {
map: json => ({...json, children: ['Goodbye!']}),
}),
).toMatchSnapshot();
});
it('accepts a map option allowing to customize content of all nested components', () => {
const rendered = render(
<div randomlygeneratedkey={Date.now()} className="wrapper">
<strong randomlygeneratedkey={Date.now()}>Hello!</strong>
<strong className="strong2">Hello 2</strong>
</div>,
);
expect(
renderToJson(rendered, {
map: json => ({
...json,
props: omitBy(json.props, (val, key) => key === 'randomlygeneratedkey'),
}),
}),
).toMatchSnapshot();
});
it('can skip a component I dont want to see with the map option', () => {
const rendered = render(
<div>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<strong>Hello 2</strong>
</div>,
);
expect(
renderToJson(rendered, {
map: json => {
if (json.type === 'ul') {
return null;
}
return json;
},
}),
).toMatchSnapshot();
});
it('outputs the snapshot even with inline JSX conditions being falsy', () => {
const rendered = render(
<div>
<span>I am there</span>
{false && <span>Issue</span>}
</div>,
);
expect(renderToJson(rendered)).toMatchSnapshot();
});
it('renders a component that has a child fragment', () => {
const wrapper = render(<FragmentAsChild />);
expect(renderToJson(wrapper)).toMatchSnapshot();
});
it('renders a component that has a fragment root', () => {
const wrapper = render(<FragmentAsRoot />);
expect(renderToJson(wrapper)).toMatchSnapshot();
});
it('renders an inline script', () => {
const wrapper = render(<InlineScript />);
expect(renderToJson(wrapper)).toMatchSnapshot();
});
it('renders an external script', () => {
const wrapper = render(<ExternalScript />);
expect(renderToJson(wrapper)).toMatchSnapshot();
});