Skip to content

Commit 5d343e2

Browse files
committed
[Tests] add tests:
- `enzyme-adapter-utils` `assertDomAvailable` & `elementToTree` - `EnzymeAdapter` `matchesElementType`
1 parent cd5c93f commit 5d343e2

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

packages/enzyme-test-suite/test/Adapter-spec.jsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { configure, shallow, EnzymeAdapter } from 'enzyme';
66
import inspect from 'object-inspect';
77
import {
88
Portal,
9+
Memo,
10+
isMemo,
911
} from 'react-is';
1012
import PropTypes from 'prop-types';
1113
import wrap from 'mocha-wrap';
@@ -1207,4 +1209,50 @@ Warning: Failed Adapter-spec type: Invalid Adapter-spec \`foo\` of type \`string
12071209
);
12081210
});
12091211
});
1212+
1213+
describe('matchesElementType(node, matchingType)', () => {
1214+
it('returns a falsy node', () => {
1215+
expect(adapter.matchesElementType()).to.equal();
1216+
expect(adapter.matchesElementType(null)).to.equal(null);
1217+
expect(adapter.matchesElementType(false)).to.equal(false);
1218+
expect(adapter.matchesElementType('')).to.equal('');
1219+
expect(adapter.matchesElementType(0)).to.equal(0);
1220+
});
1221+
1222+
it('compares the node’s `type` property to `matchingType`', () => {
1223+
const sentinel = {};
1224+
expect(adapter.matchesElementType({ type: sentinel }, sentinel)).to.equal(true);
1225+
expect(adapter.matchesElementType({ type: {} }, sentinel)).to.equal(false);
1226+
});
1227+
1228+
describeIf(is('>= 16.6'), 'memoized components', () => {
1229+
const matchingType = {};
1230+
const node = { type: matchingType };
1231+
const memoNode = {
1232+
$$typeof: Memo,
1233+
type: node.type,
1234+
};
1235+
const memoMatchingType = {
1236+
$$typeof: Memo,
1237+
type: matchingType,
1238+
};
1239+
1240+
beforeEach(() => {
1241+
expect(isMemo(memoNode)).to.equal(true); // sanity check
1242+
expect(isMemo(memoMatchingType)).to.equal(true); // sanity check
1243+
});
1244+
1245+
it('unmemoizes the node’s type', () => {
1246+
expect(adapter.matchesElementType(memoNode, matchingType)).to.equal(true);
1247+
});
1248+
1249+
it('unmemoizes the matchingType', () => {
1250+
expect(adapter.matchesElementType(node, memoMatchingType)).to.equal(true);
1251+
});
1252+
1253+
it('unmemoizes both the node’s type and matchingType', () => {
1254+
expect(adapter.matchesElementType(memoNode, memoMatchingType)).to.equal(true);
1255+
});
1256+
});
1257+
});
12101258
});

packages/enzyme-test-suite/test/adapter-utils-spec.jsx

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import {
1313
wrapWithWrappingComponent,
1414
getWrappingComponentMountRenderer,
1515
fakeDynamicImport,
16+
assertDomAvailable,
1617
} from 'enzyme-adapter-utils';
18+
import wrap from 'mocha-wrap';
1719

1820
import './_helpers/setupAdapters';
19-
import { describeIf } from './_helpers';
21+
import { describeIf, describeWithDOM } from './_helpers';
2022
import { is } from './_helpers/version';
2123

2224
describe('enzyme-adapter-utils', () => {
@@ -205,6 +207,65 @@ describe('enzyme-adapter-utils', () => {
205207
});
206208
});
207209

210+
describe('elementToTree', () => {
211+
class Target extends React.Component { render() { return null; } }
212+
const classNodeType = is('< 0.14') ? 'function' : 'class';
213+
214+
it('produces a tree', () => {
215+
const target = elementToTree(<Target a="1"><div /></Target>);
216+
expect(target).to.eql({
217+
nodeType: classNodeType,
218+
type: Target,
219+
props: {
220+
a: '1',
221+
children: <div />,
222+
},
223+
key: undefined,
224+
ref: null,
225+
instance: null,
226+
rendered: {
227+
instance: null,
228+
key: undefined,
229+
nodeType: 'host',
230+
props: {},
231+
ref: null,
232+
rendered: null,
233+
type: 'div',
234+
},
235+
});
236+
});
237+
238+
it('works with Array map', () => {
239+
const targets = [<Target a="1"><div /></Target>];
240+
expect(targets.map(elementToTree)).to.eql([{
241+
nodeType: classNodeType,
242+
type: Target,
243+
props: {
244+
a: '1',
245+
children: <div />,
246+
},
247+
key: undefined,
248+
ref: null,
249+
instance: null,
250+
rendered: {
251+
instance: null,
252+
key: undefined,
253+
nodeType: 'host',
254+
props: {},
255+
ref: null,
256+
rendered: null,
257+
type: 'div',
258+
},
259+
}]);
260+
});
261+
262+
it('throws when `dangerouslySetInnerHTML` and `children` are combined on host elements', () => {
263+
/* eslint react/no-danger-with-children: 0 */
264+
expect(() => elementToTree(<div dangerouslySetInnerHTML="hi">nope</div>)).to.throw();
265+
expect(() => elementToTree(<Target dangerouslySetInnerHTML="hi">yep</Target>)).not.to.throw();
266+
});
267+
});
268+
208269
describe('findElement', () => {
209270
class Target extends React.Component { render() { return null; } }
210271
class Other extends React.Component { render() { return null; } }
@@ -405,4 +466,21 @@ describe('enzyme-adapter-utils', () => {
405466
});
406467
});
407468
});
469+
470+
describe('assertDomAvailable', () => {
471+
describeWithDOM('with DOM', () => {
472+
it('throws', () => {
473+
expect(global).to.have.property('document');
474+
expect(global.document).to.have.property('createElement');
475+
expect(assertDomAvailable).not.to.throw();
476+
});
477+
});
478+
479+
describe('without DOM', () => {
480+
wrap().withGlobal('document', () => null).it('noops', () => {
481+
expect(!!global.document).to.equal(false);
482+
expect(assertDomAvailable).to.throw();
483+
});
484+
});
485+
});
408486
});

0 commit comments

Comments
 (0)