From a40af023bf615a9c54e4007c9bb00c2632e1f4d4 Mon Sep 17 00:00:00 2001 From: Andrew Meyer Date: Fri, 1 Sep 2023 13:43:08 +0200 Subject: [PATCH] Make useObject compatible with undefined objects * If somehow an object returns undefined, re-renders would be skipped * Added tests for proving that re-renders occur when the database is cleared * Removed 11.0.0-rc from peer-dependency --- packages/realm-react/package.json | 4 ++-- .../src/__tests__/useObjectRender.test.tsx | 18 ++++++++++++++++++ .../src/__tests__/useQueryRender.test.tsx | 19 +++++++++++++++++++ packages/realm-react/src/useObject.tsx | 6 +++--- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/packages/realm-react/package.json b/packages/realm-react/package.json index 93a6d68527..baf224c639 100644 --- a/packages/realm-react/package.json +++ b/packages/realm-react/package.json @@ -70,13 +70,13 @@ "react": "18.2.0", "react-native": "0.71.7", "react-test-renderer": "18.2.0", - "realm": "*", + "realm": "^11.0.0-rc.0", "rollup-plugin-dts": "^5.0.0", "ts-jest": "^29.0.5" }, "peerDependencies": { "react": ">=17.0.2", - "realm": "^12.0.0-browser || ^12.0.0 || ^12.0.0-rc || ^11.0.0-rc || ^11.0.0" + "realm": "^12.0.0-browser || ^12.0.0 || ^12.0.0-rc || ^11.0.0" }, "optionalDependencies": { "@babel/runtime": ">=7", diff --git a/packages/realm-react/src/__tests__/useObjectRender.test.tsx b/packages/realm-react/src/__tests__/useObjectRender.test.tsx index fc138fcc07..d17230eb0b 100644 --- a/packages/realm-react/src/__tests__/useObjectRender.test.tsx +++ b/packages/realm-react/src/__tests__/useObjectRender.test.tsx @@ -438,6 +438,24 @@ describe("useObject: rendering objects with a Realm.List property", () => { fireEvent.press(rerenderButton); expect(objectChangeCounter).toHaveBeenCalledTimes(expectedCount); }); + it("will rerender when the realm database is cleared", async () => { + await setupTest(); + + const initialCount = 2; + + expect(objectChangeCounter).toHaveBeenCalledTimes(initialCount); + + await act(async () => { + testRealm.write(() => { + testRealm.deleteAll(); + }); + forceSynchronousNotifications(testRealm); + }); + + await new Promise((resolve) => setTimeout(resolve, 1000)); + + expect(objectChangeCounter).toHaveBeenCalledTimes(initialCount + 1); + }); }); describe("rendering objects with a Realm.List property", () => { diff --git a/packages/realm-react/src/__tests__/useQueryRender.test.tsx b/packages/realm-react/src/__tests__/useQueryRender.test.tsx index 9b391b27d0..839ab8d349 100644 --- a/packages/realm-react/src/__tests__/useQueryRender.test.tsx +++ b/packages/realm-react/src/__tests__/useQueryRender.test.tsx @@ -443,4 +443,23 @@ describe.each` fireEvent.press(rerenderButton); expect(queryObjectChangeCounter).toHaveBeenCalledTimes(1); }); + + it("will rerender when the realm database is cleared", async () => { + await setupTest({ queryType }); + + const initialCount = 1; + + expect(queryObjectChangeCounter).toHaveBeenCalledTimes(initialCount); + + await act(async () => { + testRealm.write(() => { + testRealm.deleteAll(); + }); + forceSynchronousNotifications(testRealm); + }); + + await new Promise((resolve) => setTimeout(resolve, 1000)); + + expect(queryObjectChangeCounter).toHaveBeenCalledTimes(initialCount + 1); + }); }); diff --git a/packages/realm-react/src/useObject.tsx b/packages/realm-react/src/useObject.tsx index 985de52352..c9e89b5021 100644 --- a/packages/realm-react/src/useObject.tsx +++ b/packages/realm-react/src/useObject.tsx @@ -56,7 +56,7 @@ export function createUseObject(useRealm: () => Realm) { // Ref: https://github.com/facebook/react/issues/14490 const cachedObjectRef = useRef(null); - if (cachedObjectRef.current === null) { + if (!cachedObjectRef.current) { cachedObjectRef.current = createCachedObject({ object: originalObject ?? null, realm, @@ -81,7 +81,7 @@ export function createUseObject(useRealm: () => Realm) { // Re-instantiate the cachedObject if the primaryKey has changed or the originalObject has gone from null to not null if ( !arePrimaryKeysIdentical(primaryKey, primaryKeyRef.current) || - (originalObjectRef.current === null && originalObject !== null) + (!originalObjectRef.current && originalObject) ) { cachedObjectRef.current = createCachedObject({ object: originalObject ?? null, @@ -138,7 +138,7 @@ export function createUseObject(useRealm: () => Realm) { }, [realm, type, forceRerender]); // If the object has been deleted or doesn't exist for the given primary key, just return null - if (object === null || object?.isValid() === false) { + if (!object || object?.isValid() === false) { return null; }