Skip to content

Commit

Permalink
[Tests] add tests:
Browse files Browse the repository at this point in the history
 - `enzyme-adapter-utils` `assertDomAvailable` & `elementToTree`
 - `EnzymeAdapter` `matchesElementType`
  • Loading branch information
ljharb committed Sep 18, 2019
1 parent cd5c93f commit 5d343e2
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
48 changes: 48 additions & 0 deletions packages/enzyme-test-suite/test/Adapter-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { configure, shallow, EnzymeAdapter } from 'enzyme';
import inspect from 'object-inspect';
import {
Portal,
Memo,
isMemo,
} from 'react-is';
import PropTypes from 'prop-types';
import wrap from 'mocha-wrap';
Expand Down Expand Up @@ -1207,4 +1209,50 @@ Warning: Failed Adapter-spec type: Invalid Adapter-spec \`foo\` of type \`string
);
});
});

describe('matchesElementType(node, matchingType)', () => {
it('returns a falsy node', () => {
expect(adapter.matchesElementType()).to.equal();
expect(adapter.matchesElementType(null)).to.equal(null);
expect(adapter.matchesElementType(false)).to.equal(false);
expect(adapter.matchesElementType('')).to.equal('');
expect(adapter.matchesElementType(0)).to.equal(0);
});

it('compares the node’s `type` property to `matchingType`', () => {
const sentinel = {};
expect(adapter.matchesElementType({ type: sentinel }, sentinel)).to.equal(true);
expect(adapter.matchesElementType({ type: {} }, sentinel)).to.equal(false);
});

describeIf(is('>= 16.6'), 'memoized components', () => {
const matchingType = {};
const node = { type: matchingType };
const memoNode = {
$$typeof: Memo,
type: node.type,
};
const memoMatchingType = {
$$typeof: Memo,
type: matchingType,
};

beforeEach(() => {
expect(isMemo(memoNode)).to.equal(true); // sanity check
expect(isMemo(memoMatchingType)).to.equal(true); // sanity check
});

it('unmemoizes the node’s type', () => {
expect(adapter.matchesElementType(memoNode, matchingType)).to.equal(true);
});

it('unmemoizes the matchingType', () => {
expect(adapter.matchesElementType(node, memoMatchingType)).to.equal(true);
});

it('unmemoizes both the node’s type and matchingType', () => {
expect(adapter.matchesElementType(memoNode, memoMatchingType)).to.equal(true);
});
});
});
});
80 changes: 79 additions & 1 deletion packages/enzyme-test-suite/test/adapter-utils-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import {
wrapWithWrappingComponent,
getWrappingComponentMountRenderer,
fakeDynamicImport,
assertDomAvailable,
} from 'enzyme-adapter-utils';
import wrap from 'mocha-wrap';

import './_helpers/setupAdapters';
import { describeIf } from './_helpers';
import { describeIf, describeWithDOM } from './_helpers';
import { is } from './_helpers/version';

describe('enzyme-adapter-utils', () => {
Expand Down Expand Up @@ -205,6 +207,65 @@ describe('enzyme-adapter-utils', () => {
});
});

describe('elementToTree', () => {
class Target extends React.Component { render() { return null; } }
const classNodeType = is('< 0.14') ? 'function' : 'class';

it('produces a tree', () => {
const target = elementToTree(<Target a="1"><div /></Target>);
expect(target).to.eql({
nodeType: classNodeType,
type: Target,
props: {
a: '1',
children: <div />,
},
key: undefined,
ref: null,
instance: null,
rendered: {
instance: null,
key: undefined,
nodeType: 'host',
props: {},
ref: null,
rendered: null,
type: 'div',
},
});
});

it('works with Array map', () => {
const targets = [<Target a="1"><div /></Target>];
expect(targets.map(elementToTree)).to.eql([{
nodeType: classNodeType,
type: Target,
props: {
a: '1',
children: <div />,
},
key: undefined,
ref: null,
instance: null,
rendered: {
instance: null,
key: undefined,
nodeType: 'host',
props: {},
ref: null,
rendered: null,
type: 'div',
},
}]);
});

it('throws when `dangerouslySetInnerHTML` and `children` are combined on host elements', () => {
/* eslint react/no-danger-with-children: 0 */
expect(() => elementToTree(<div dangerouslySetInnerHTML="hi">nope</div>)).to.throw();
expect(() => elementToTree(<Target dangerouslySetInnerHTML="hi">yep</Target>)).not.to.throw();
});
});

describe('findElement', () => {
class Target extends React.Component { render() { return null; } }
class Other extends React.Component { render() { return null; } }
Expand Down Expand Up @@ -405,4 +466,21 @@ describe('enzyme-adapter-utils', () => {
});
});
});

describe('assertDomAvailable', () => {
describeWithDOM('with DOM', () => {
it('throws', () => {
expect(global).to.have.property('document');
expect(global.document).to.have.property('createElement');
expect(assertDomAvailable).not.to.throw();
});
});

describe('without DOM', () => {
wrap().withGlobal('document', () => null).it('noops', () => {
expect(!!global.document).to.equal(false);
expect(assertDomAvailable).to.throw();
});
});
});
});

0 comments on commit 5d343e2

Please sign in to comment.