Skip to content

Commit

Permalink
feat: add isFetching
Browse files Browse the repository at this point in the history
  • Loading branch information
iamchanii committed Nov 23, 2021
1 parent 191671b commit dcaabf1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-otters-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-query-helper': patch
---

Add isFetching
21 changes: 21 additions & 0 deletions src/QueryHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,24 @@ describe('resetQueries', () => {
`);
});
});

describe('isFetching', () => {
it('should return 0 if any queries not fetching', () => {
expect(getPostById.isFetching()).toEqual(0);
});

it('should return 1 if exsists query exists that currently fetching', () => {
getPostById.prefetchQuery(1);

expect(getPostById.isFetching()).toEqual(1);
});

it('should return a count of queries that currently fetching', () => {
getPostById.prefetchQuery(1, { cacheTime: 1 });
getPostById.prefetchQuery(2, { cacheTime: 1 });
getPostById.prefetchQuery(3, { cacheTime: 1 });

const predicate = (query: Query) => query.queryKey[1] !== 1;
expect(getPostById.isFetching({ predicate })).toEqual(2);
});
});
6 changes: 6 additions & 0 deletions src/QueryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,10 @@ export class QueryHelper<

return queryClient.resetQueries(this.baseQueryKey, filters, options);
}

isFetching(filters?: QueryFilters) {
const queryClient = this.getQueryClient();

return queryClient.isFetching(this.baseQueryKey, filters);
}
}

0 comments on commit dcaabf1

Please sign in to comment.