1 | import { expect } from 'chai';
|
2 | import * as React from 'react';
|
3 | import findOutermostIntrinsic from './findOutermostIntrinsic';
|
4 | import ReactTestRenderer from 'react-test-renderer';
|
5 | import testRef from './testRef';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function findRootComponent(wrapper, {
|
25 | component
|
26 | }) {
|
27 | const outermostHostElement = findOutermostIntrinsic(wrapper).getElement();
|
28 | return wrapper.find(component).filterWhere(componentWrapper => {
|
29 | return componentWrapper.contains(outermostHostElement);
|
30 | });
|
31 | }
|
32 |
|
33 | function randomStringValue() {
|
34 | return Math.random().toString(36).slice(2);
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function testClassName(element, getOptions) {
|
46 | it('applies the className to the root component', () => {
|
47 | const {
|
48 | mount
|
49 | } = getOptions();
|
50 | const className = randomStringValue();
|
51 | const wrapper = mount( React.cloneElement(element, {
|
52 | className
|
53 | }));
|
54 | expect(findOutermostIntrinsic(wrapper).hasClass(className)).to.equal(true, 'does have a custom `className`');
|
55 | });
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function testComponentProp(element, getOptions) {
|
67 | describe('prop: component', () => {
|
68 | it('can render another root component with the `component` prop', () => {
|
69 | const {
|
70 | classes,
|
71 | mount,
|
72 | testComponentPropWith: component = 'em'
|
73 | } = getOptions();
|
74 | const wrapper = mount( React.cloneElement(element, {
|
75 | component
|
76 | }));
|
77 | expect(findRootComponent(wrapper, {
|
78 | classes,
|
79 | component
|
80 | }).exists()).to.equal(true);
|
81 | });
|
82 | });
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function testPropsSpread(element, getOptions) {
|
94 | it(`spreads props to the root component`, () => {
|
95 |
|
96 | const {
|
97 | classes,
|
98 | inheritComponent,
|
99 | mount
|
100 | } = getOptions();
|
101 | const testProp = 'data-test-props-spread';
|
102 | const value = randomStringValue();
|
103 | const wrapper = mount( React.cloneElement(element, {
|
104 | [testProp]: value
|
105 | }));
|
106 | const root = findRootComponent(wrapper, {
|
107 | classes,
|
108 | component: inheritComponent
|
109 | });
|
110 | expect(root.props()[testProp]).to.equal(value);
|
111 | });
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function describeRef(element, getOptions) {
|
125 | describe('ref', () => {
|
126 | it(`attaches the ref`, () => {
|
127 |
|
128 | const {
|
129 | inheritComponent,
|
130 | mount,
|
131 | refInstanceof
|
132 | } = getOptions();
|
133 | testRef(element, mount, (instance, wrapper) => {
|
134 | expect(instance).to.be.instanceof(refInstanceof);
|
135 |
|
136 | if (inheritComponent && instance.nodeType === 1) {
|
137 | const rootHost = findOutermostIntrinsic(wrapper);
|
138 | expect(instance).to.equal(rootHost.instance());
|
139 | }
|
140 | });
|
141 | });
|
142 | });
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | function testRootClass(element, getOptions) {
|
153 | it('applies the root class to the root component if it has this class', () => {
|
154 | const {
|
155 | classes,
|
156 | mount
|
157 | } = getOptions();
|
158 |
|
159 | if (classes.root == null) {
|
160 | return;
|
161 | }
|
162 |
|
163 | const className = randomStringValue();
|
164 | const wrapper = mount( React.cloneElement(element, {
|
165 | className
|
166 | }));
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | expect(findOutermostIntrinsic(wrapper).hasClass(classes.root)).to.equal(true);
|
172 | expect(findOutermostIntrinsic(wrapper).hasClass(className)).to.equal(true);
|
173 | });
|
174 | }
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | function testReactTestRenderer(element) {
|
184 | it('should render without errors in ReactTestRenderer', () => {
|
185 | ReactTestRenderer.act(() => {
|
186 | ReactTestRenderer.create(element, {
|
187 | createNodeMock: node => {
|
188 | return document.createElement(node.type);
|
189 | }
|
190 | });
|
191 | });
|
192 | });
|
193 | }
|
194 |
|
195 | const fullSuite = {
|
196 | componentProp: testComponentProp,
|
197 | mergeClassName: testClassName,
|
198 | propsSpread: testPropsSpread,
|
199 | refForwarding: describeRef,
|
200 | rootClass: testRootClass,
|
201 | reactTestRenderer: testReactTestRenderer
|
202 | };
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | export default function describeConformance(minimalElement, getOptions) {
|
224 | const {
|
225 | after: runAfterHook = () => {},
|
226 | only = Object.keys(fullSuite),
|
227 | skip = []
|
228 | } = getOptions();
|
229 | describe('Material-UI component API', () => {
|
230 | after(runAfterHook);
|
231 | Object.keys(fullSuite).filter(testKey => only.indexOf(testKey) !== -1 && skip.indexOf(testKey) === -1).forEach(testKey => {
|
232 | const test = fullSuite[testKey];
|
233 | test(minimalElement, getOptions);
|
234 | });
|
235 | });
|
236 | } |
\ | No newline at end of file |