Skip to content

Selecting React Components via CSS prop selector #534

@travisperson

Description

@travisperson

I was looking at this yesterday and it appears that when using mount you can't use CSS prop selectors. I did some digging and found the PR (#70), and after reading through it a bit it appears that it was inertial. Instead of using a string (via CSS prop selector), the go to method was to instead use the object selector. At this point I stopped digging and assumed that the CSS prop selector was not supported in this way. However, when shallow rendering you can use a CSS prop selector with a React Component.

diff --git a/test/ReactWrapper-spec.jsx b/test/ReactWrapper-spec.jsx
index 503eee3..c8d7a65 100644
--- a/test/ReactWrapper-spec.jsx
+++ b/test/ReactWrapper-spec.jsx
@@ -384,6 +384,20 @@ describeWithDOM('mount', () => {

     });

+    it('should find a React Component based on a component props via css selector', () => {
+      class Foo extends React.Component {
+        render() { return <div />; }
+      }
+      const wrapper = mount(
+        <div>
+          <Foo bar="baz" />
+        </div>
+      );
+
+      expect(wrapper.find('Foo[bar="baz"]')).to.have.length(1);
+      expect(wrapper.find('[bar]')).to.have.length(1);
+    });
+
     it('should not find components with invalid attributes', () => {
       // Invalid attributes aren't valid JSX, so manual instantiation is necessary
       const wrapper = mount(
diff --git a/test/ShallowWrapper-spec.jsx b/test/ShallowWrapper-spec.jsx
index 47b570f..faf83b5 100644
--- a/test/ShallowWrapper-spec.jsx
+++ b/test/ShallowWrapper-spec.jsx
@@ -481,6 +481,21 @@ describe('shallow', () => {
       expect(wrapper.find('[data-foo]')).to.have.length(1);
     });

+    it('should find a React Component based on a component props via css selector', () => {
+      class Foo extends React.Component {
+        render() { return <div />; }
+      }
+
+      const wrapper = shallow(
+        <div>
+          <Foo bar="baz" />
+        </div>
+      );
+
+      expect(wrapper.find('Foo[bar="baz"]')).to.have.length(1);
+      expect(wrapper.find('[bar]')).to.have.length(1);
+    });
+
     it('should find components with multiple matching react props', () => {
       function noop() {}
       const wrapper = shallow(
  495 passing (2s)
  10 pending
  1 failing

  1) (uses jsdom) mount .find(selector) should find a React Component based on a component props via css selector:
     AssertionError: expected { Object (component, root, ...) } to have a length of 1 but got 0
      at Context.<anonymous> (ReactWrapper-spec.jsx:397:54)

Is this intentional for shallow but not for mount?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions