baruchvlz / resq

React Element Selector Query (RESQ) - Query React components and children by component name or HTML selector
MIT License
174 stars 27 forks source link

byProps/byState should match all keys #61

Closed ooorayray closed 3 years ago

ooorayray commented 3 years ago

Current behavior

When passing a matcher with multiple keys, .byProps/.byState returns nodes that match any one of the given keys.

Example:

const filtered = myComponent.byProps({ prop1: 123, prop2: 'abc' })
console.log(filtered)
/*
{
    name: 'MyComponent',
    props: {
        prop1: 123,
        prop2: 'def',
    },
    // ...
}
*/

Expected behavior

.byProps/.byState returns nodes that match ALL of the given keys.

Example:

const filtered = myComponent.byProps({ prop1: 123, prop2: 'abc' })
console.log(filtered)
/*
{
    name: 'MyComponent',
    props: {
        prop1: 123,
        prop2: 'abc',
    },
    // ...
}
*/

Failing test case: https://github.com/ooorayray/resq/commit/b502b830105736a80b2fccbd37d9b31ed9e55672

baruchvlz commented 3 years ago

Thanks for reporting and for the test case. I was able to pin point the error, I'm on my birthday holidays at the moment, but will fix this ASAP early next week along with #60 .

baruchvlz commented 3 years ago

Should be fixed in v1.9.0.

rousku commented 2 years ago

@baruchvlz I still find current behavior unexpected. By writing const filtered = myComponent.byProps({ prop1: 123, prop2: 'abc' }) I expect all filtered nodes have props prop1: 123 AND prop2: 'abc'. I think this test case should fail because b doesn't have prop foo.

baruchvlz commented 2 years ago

@rousku The current match returns true if at least one of the properties match. There could be an optional flag to make it a strict match.

If you like you could open a PR and I would gladly review it :)