-
Notifications
You must be signed in to change notification settings - Fork 560
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
@realm/react
key-path filtering on useQuery
and useObject
#6360
Changes from 21 commits
ed6558b
1e96013
74fdcac
056df66
88f53c1
8330c9a
340aec4
e745dfb
67b22af
b12282b
995cf7d
307dc1b
c0008ba
181626d
0a8daef
fa91205
7dd55c8
fc9fe8a
a566a53
8f568f6
804fa6d
2a165ca
0f6eb61
3483dc9
110eaf8
96bfc10
ed9774e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2024 Realm Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
import assert from "node:assert"; | ||
import Realm, { Configuration } from "realm"; | ||
import { act } from "@testing-library/react-native"; | ||
|
||
import { randomRealmPath } from "./helpers"; | ||
|
||
export type RealmTestContext = { | ||
realm: Realm; | ||
useRealm: () => Realm; | ||
write(cb: () => void): void; | ||
openRealm(config?: Configuration): Realm; | ||
cleanup(): void; | ||
}; | ||
|
||
/** | ||
* Opens a test realm at a randomized and temporary path. | ||
* @returns The `realm` and a `write` function, which will wrap `realm.write` with an `act` and prepand a second `realm.write` to force notifications to trigger synchronously. | ||
*/ | ||
export function createRealmTestContext(rootConfig: Configuration = {}): RealmTestContext { | ||
let realm: Realm | null = null; | ||
const context = { | ||
get realm(): Realm { | ||
assert(realm, "Open the Realm first"); | ||
return realm; | ||
}, | ||
useRealm() { | ||
return context.realm; | ||
}, | ||
openRealm(config: Configuration = {}) { | ||
if (realm) { | ||
// Close any realm, previously opened | ||
realm.close(); | ||
} | ||
realm = new Realm({ ...rootConfig, ...config, path: randomRealmPath() }); | ||
return realm; | ||
}, | ||
write(callback: () => void) { | ||
act(() => { | ||
context.realm.write(callback); | ||
// Starting another write transaction will force notifications to fire synchronously | ||
context.realm.beginTransaction(); | ||
context.realm.cancelTransaction(); | ||
}); | ||
}, | ||
cleanup() { | ||
Realm.clearTestState(); | ||
} | ||
}; | ||
return context; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2024 Realm Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
import React, { Profiler, ProfilerOnRenderCallback } from "react"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice usage of the Profiler here 👍🏼 |
||
import { renderHook, RenderHookResult, RenderHookOptions } from "@testing-library/react-native"; | ||
|
||
function generateProfilerId() { | ||
const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); | ||
return `test-${nonce}`; | ||
} | ||
|
||
type RenderEvent = { | ||
phase: "mount" | "update"; | ||
actualDuration: number; | ||
baseDuration: number; | ||
}; | ||
|
||
type ProfileWrapper = { | ||
wrapper: React.ComponentType<React.PropsWithChildren>; | ||
renders: RenderEvent[]; | ||
}; | ||
|
||
function createProfilerWrapper(Parent: undefined | React.ComponentType<React.PropsWithChildren>): ProfileWrapper { | ||
const renders: RenderEvent[] = []; | ||
const id = generateProfilerId(); | ||
const handleRender: ProfilerOnRenderCallback = ( | ||
id, | ||
phase, | ||
actualDuration, | ||
baseDuration, | ||
startTime, | ||
commitTime, | ||
interactions, | ||
) => { | ||
renders.push({ phase, actualDuration, baseDuration }); | ||
}; | ||
|
||
const Wrapper: React.ComponentType<React.PropsWithChildren> = ({ children }) => ( | ||
<Profiler id={id} onRender={handleRender} children={children} /> | ||
); | ||
|
||
return { | ||
wrapper: Parent | ||
? ({ children }) => ( | ||
<Parent> | ||
<Wrapper children={children} /> | ||
</Parent> | ||
) | ||
: Wrapper, | ||
renders, | ||
}; | ||
} | ||
|
||
export function profileHook<Result, Props>( | ||
callback: (props: Props) => Result, | ||
options?: RenderHookOptions<Props>, | ||
): RenderHookResult<Result, Props> & { renders: RenderEvent[] } { | ||
const { wrapper, renders } = createProfilerWrapper(options?.wrapper); | ||
const result = renderHook<Result, Props>(callback, { ...options, wrapper }); | ||
return { ...result, renders }; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,81 +16,124 @@ | |
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
import { useEffect, useState } from "react"; | ||
import Realm from "realm"; | ||
import { renderHook } from "@testing-library/react-native"; | ||
|
||
import { createUseObject } from "../useObject"; | ||
import { createRealmTestContext } from "./createRealmTestContext"; | ||
import { profileHook } from "./profileHook"; | ||
|
||
const dogSchema: Realm.ObjectSchema = { | ||
name: "dog", | ||
primaryKey: "_id", | ||
properties: { | ||
_id: "int", | ||
name: "string", | ||
age: "int", | ||
}, | ||
}; | ||
|
||
interface IDog { | ||
_id: number; | ||
name: string; | ||
age: number; | ||
} | ||
|
||
const configuration = { | ||
schema: [dogSchema], | ||
path: "testArtifacts/use-object-hook.realm", | ||
}; | ||
|
||
const useRealm = () => { | ||
const [realm, setRealm] = useState(new Realm(configuration)); | ||
useEffect(() => { | ||
return () => { | ||
realm.close(); | ||
}; | ||
}, [realm, setRealm]); | ||
|
||
return new Realm(configuration); | ||
}; | ||
|
||
const useObject = createUseObject(useRealm); | ||
const context = createRealmTestContext({ schema: [dogSchema] }); | ||
elle-j marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const useObject = createUseObject(context.useRealm); | ||
|
||
const testDataSet = [ | ||
{ _id: 4, name: "Vincent" }, | ||
{ _id: 5, name: "River" }, | ||
{ _id: 6, name: "Schatzi" }, | ||
{ _id: 4, name: "Vincent", age: 5 }, | ||
{ _id: 5, name: "River", age: 25 }, | ||
{ _id: 6, name: "Schatzi", age: 13 }, | ||
]; | ||
|
||
describe("useObject hook", () => { | ||
describe("useObject", () => { | ||
beforeEach(() => { | ||
const realm = new Realm(configuration); | ||
const realm = context.openRealm(); | ||
realm.write(() => { | ||
realm.deleteAll(); | ||
testDataSet.forEach((data) => { | ||
realm.create("dog", data); | ||
}); | ||
}); | ||
realm.close(); | ||
}); | ||
|
||
afterEach(() => { | ||
Realm.clearTestState(); | ||
context.cleanup(); | ||
}); | ||
|
||
it("can retrieve a single object using useObject", () => { | ||
const [, dog2] = testDataSet; | ||
|
||
const { result } = renderHook(() => useObject<IDog>("dog", dog2._id)); | ||
|
||
const [, river] = testDataSet; | ||
const { result } = renderHook(() => useObject<IDog>("dog", river._id)); | ||
const object = result.current; | ||
|
||
expect(object).toMatchObject(dog2); | ||
expect(object).toMatchObject(river); | ||
}); | ||
|
||
it("object is null", () => { | ||
const { result } = renderHook(() => useObject<IDog>("dog", 12)); | ||
describe("missing objects", () => { | ||
it("return null", () => { | ||
const { result } = renderHook(() => useObject<IDog>("dog", 12)); | ||
expect(result.current).toEqual(null); | ||
}); | ||
|
||
const object = result.current; | ||
it("rerenders and return object once created", () => { | ||
const { write, realm } = context; | ||
const { result, renders } = profileHook(() => useObject<IDog>("dog", 12)); | ||
expect(renders).toHaveLength(1); | ||
expect(result.current).toEqual(null); | ||
write(() => { | ||
realm.create<IDog>("dog", { _id: 12, name: "Lassie", age: 32 }); | ||
}); | ||
expect(renders).toHaveLength(2); | ||
expect(result.current?.name).toEqual("Lassie"); | ||
}); | ||
}); | ||
|
||
describe("key-path filtering", () => { | ||
it("can filter notifications using key-path array", async () => { | ||
const [vincent] = testDataSet; | ||
const { write } = context; | ||
const { result, renders } = profileHook(() => useObject<IDog>("dog", vincent._id, ["name"])); | ||
expect(renders).toHaveLength(1); | ||
expect(result.current).toMatchObject(vincent); | ||
// Update the name and except a re-render | ||
kraenhansen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
write(() => { | ||
if (result.current) { | ||
result.current.name = "Vince!"; | ||
} | ||
}); | ||
expect(renders).toHaveLength(2); | ||
expect(result.current?.name).toEqual("Vince!"); | ||
// Update the age and don't expect a re-render | ||
write(() => { | ||
if (result.current) { | ||
result.current.age = 5; | ||
} | ||
}); | ||
expect(renders).toHaveLength(2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, this should probably just assert |
||
}); | ||
|
||
expect(object).toEqual(null); | ||
it("can filter notifications using key-path string", async () => { | ||
const [vincent] = testDataSet; | ||
const { write } = context; | ||
const { result, renders } = profileHook(() => useObject<IDog>("dog", vincent._id, "age")); | ||
expect(renders).toHaveLength(1); | ||
expect(result.current).toMatchObject(vincent); | ||
// Update the name and except a re-render | ||
kraenhansen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
write(() => { | ||
if (result.current) { | ||
result.current.age = 13; | ||
} | ||
}); | ||
expect(renders).toHaveLength(2); | ||
expect(result.current?.age).toEqual(13); | ||
// Update the age and don't expect a re-render | ||
write(() => { | ||
if (result.current) { | ||
result.current.name = "Vince!"; | ||
} | ||
}); | ||
expect(renders).toHaveLength(2); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added this helper, to make it easier to work with Realms in the tests.