Skip to content

Commit

Permalink
Add test & fix missing dep in RealmProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
bimusiek committed Nov 23, 2023
1 parent a69da12 commit 110815a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/realm-react/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"react-hooks/exhaustive-deps": [
"warn",
{
"additionalHooks": "(^useQuery$)"
}
],
"jsdoc/check-tag-names": "off",
"jsdoc/require-jsdoc": "off",
"jsdoc/require-returns": "off",
Expand Down
2 changes: 1 addition & 1 deletion packages/realm-react/src/RealmProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function createRealmProvider(
if (realmRef) {
realmRef.current = realm;
}
}, [realm]);
}, [realm, realmRef]);

useEffect(() => {
const realmRef = currentRealm.current;
Expand Down
18 changes: 18 additions & 0 deletions packages/realm-react/src/__tests__/useQueryHook.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,22 @@ describe("useQueryHook", () => {

expect(collection[99]).toBe(undefined);
});

it("should support reversed order of arguments for exhausive deps", () => {
// Eslint will trigger warning here with
// 120:92 warning React Hook useQuery has a missing dependency: 'gender'. Either include it or remove the dependency array react-hooks/exhaustive-deps
// which is correct behaviour.
const { result } = renderHook(
({ gender }) => useQuery<IDog>((objects) => objects.filtered("gender = $0", gender), [], "dog"),
{
initialProps: { gender: "male" },
},
);
const collection = result.current;

expect(collection).not.toBeNull();
expect(collection.length).toBe(2);
expect(collection[0]).toMatchObject(testDataSet[0]);
expect(collection[1]).toMatchObject(testDataSet[3]);
});
});
1 change: 1 addition & 0 deletions packages/realm-react/src/useQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function createUseQuery(useRealm: () => Realm) {
// We want the user of this hook to be able pass in the `query` function inline (without the need to `useCallback` on it)
// This means that the query function is unstable and will be a redefined on each render of the component where `useQuery` is used
// Therefore we use the `deps` array to memoize the query function internally, and only use the returned `queryCallback`
// eslint-disable-next-line react-hooks/exhaustive-deps
const queryCallback = useCallback(query, [...deps, ...requiredDeps]);

// If the query function changes, we need to update the cachedCollection
Expand Down

0 comments on commit 110815a

Please sign in to comment.