Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added filterWhere function to FindWrapper #55

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ Returns a new `FindWrapper` with a subset of the previously selected elements gi

Uses the same possible selectors as [`RenderContext#find(selector)`](#rendercontextfindselector).

### `FindWrapper#filterWhere(predicate)`
Returns a new `FindWrapper` with a subset of the previously selected elements which, when passed into the provided predicate function, return true.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small usage example here would be useful.


### `FindWrapper#find(selector)`
Selects descendents of the elements previously selected. Returns a new `FindWrapper` with the newly selected elements.

Expand Down
8 changes: 7 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export interface FindWrapper<P, S> {
**/
filter<Q, T>(selector: string): FindWrapper<Q, T>;

/**
* Returns a new `FindWrapper` with a subset of the previously selected elements which, when passed into the
* provided predicate function, return true.
**/
filterWhere<Q, T>(predicate:(element: FindWrapper<Q, T>, index: number) => boolean): FindWrapper<Q, T>;

/**
* Selects descendents of the elements previously selected. Returns a new `FindWrapper` with the newly selected
* elements.
Expand Down Expand Up @@ -84,4 +90,4 @@ interface ShallowFunction {
export const deep: DeepFunction;
export const render: DeepFunction;
export const shallow: ShallowFunction;
export default deep;
export default deep;
10 changes: 10 additions & 0 deletions src/preact-render-spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ class FindWrapper {
);
}

filterWhere(predicate) {
verifyFoundNodes(this);
return new FindWrapper(
this.context,
Array.from(this).filter(
(vnode, ...attrs) => predicate(new FindWrapper(this.context, [vnode]), ...attrs)
)
);
}

component() {
if (this.length !== 1) {
throw new Error('preact-render-spy: component method can only be used on a single node');
Expand Down
8 changes: 8 additions & 0 deletions src/shared-render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ const sharedTests = (name, func) => {
expect(context.filter(<span />).length).toBe(0);
});

it(`${name}: filterWhere() filters components using predicates `, () => {
const context = func(<div><NullStateless class="first" /><NullStateless class="second" /></div>);
expect(context.find('NullStateless').length).toBe(2);
expect(context.find('NullStateless').filterWhere(() => true).length).toBe(2);
expect(context.find('NullStateless').filterWhere(() => false).length).toBe(0);
expect(context.find('NullStateless').filterWhere(n => n.attr('class') === 'first').length).toBe(1);
});

it(`${name}: output returns vdom output by a Component`, () => {
const context = func(<DivChildren><span /></DivChildren>);
expect(() => context.find('div').output()).toThrow();
Expand Down