Skip to content

Commit

Permalink
[New] shallow/mount: .find: find HTML elements by their constru…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
kii-dot authored and ljharb committed Dec 8, 2018
1 parent def0737 commit ef6c8b3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"chai": "^4.1.2",
"enzyme": "^3.8.0",
"enzyme-adapter-utils": "^1.9.1",
"html-element-map": "^1.0.0",
"jsdom": "^6.5.1",
"lodash.isequal": "^4.5.0",
"mocha-wrap": "^2.1.2",
Expand Down
24 changes: 24 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { expect } from 'chai';
import sinon from 'sinon';
import wrap from 'mocha-wrap';
import isEqual from 'lodash.isequal';
import getData from 'html-element-map/getData';
import {
mount,
render,
Expand Down Expand Up @@ -1526,6 +1527,29 @@ describeWithDOM('mount', () => {
expect(wrapper.find(Component.displayName)).to.have.lengthOf(2);
});
});

describeWithDOM('find DOM elements by constructor', () => {
const { elements, all } = getData();

elements.filter(({ constructor: C }) => C && C !== all).forEach(({
tag: Tag,
constructorName: name,
}) => {
class Foo extends React.Component {
render() {
return <Tag />;
}
}

it(`${Tag}: finds by constructor “${name}”`, () => {
const wrapper = mount(<Foo />);

expect(wrapper.childAt(0).type()).to.equal(Tag);
expect(wrapper.childAt(0).is(Tag)).to.equal(true);
expect(wrapper.find(Tag)).to.have.lengthOf(1);
});
});
});
});

describe('.findWhere(predicate)', () => {
Expand Down
25 changes: 25 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { expect } from 'chai';
import sinon from 'sinon';
import wrap from 'mocha-wrap';
import isEqual from 'lodash.isequal';
import getData from 'html-element-map/getData';
import {
shallow,
render,
Expand Down Expand Up @@ -32,6 +33,7 @@ import {
} from './_helpers/react-compat';
import {
describeIf,
describeWithDOM,
itIf,
itWithData,
generateEmptyRenderData,
Expand Down Expand Up @@ -1523,6 +1525,29 @@ describe('shallow', () => {
expect(wrapper.find(Component.displayName)).to.have.lengthOf(2);
});
});

describeWithDOM('find DOM elements by constructor', () => {
const { elements, all } = getData();

elements.filter(({ constructor: C }) => C && C !== all).forEach(({
tag: Tag,
constructorName: name,
}) => {
class Foo extends React.Component {
render() {
return <Tag />;
}
}

it(`${Tag}: finds by constructor “${name}”`, () => {
const wrapper = shallow(<Foo />);

expect(wrapper.type()).to.equal(Tag);
expect(wrapper.is(Tag)).to.equal(true);
expect(wrapper.filter(Tag)).to.have.lengthOf(1);
});
});
});
});

describe('.findWhere(predicate)', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"cheerio": "^1.0.0-rc.2",
"function.prototype.name": "^1.1.0",
"has": "^1.0.3",
"html-element-map": "^1.0.0",
"is-boolean-object": "^1.0.0",
"is-callable": "^1.1.4",
"is-number-object": "^1.0.3",
Expand Down
15 changes: 14 additions & 1 deletion packages/enzyme/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import values from 'object.values';
import flat from 'array.prototype.flat';
import is from 'object-is';
import has from 'has';
import elementsByConstructor from 'html-element-map/byConstructor';
import {
treeFilter,
nodeHasId,
Expand Down Expand Up @@ -362,10 +363,22 @@ function matchDescendant(nodes, predicate) {
* the selector. The selector can be a simple selector, which
* is handled by `buildPredicate`, or a complex CSS selector which
* reduceTreeBySelector parses and reduces the tree based on the combinators.
* @param {Function|Object|String} selector
*
* @param {EnzymeSelector} selector
* @param {RSTNode} root
*/
export function reduceTreeBySelector(selector, root) {
if (typeof selector !== 'string') {
const elements = elementsByConstructor(selector);
if (elements.length > 0) {
return flat(elements.map(x => reduceTreeBySelector(x.tag, root)));

// when https://github.com/aweary/rst-selector-parser/issues/15 is resolved
// const htmlTagNames = elements.map(x => x.tag).join(', ');
// return reduceTreeBySelector(htmlTagNames, root);
}
}

if (typeof selector === 'function' || typeof selector === 'object') {
return treeFilter(root, buildPredicate(selector));
}
Expand Down

0 comments on commit ef6c8b3

Please sign in to comment.