-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ed6558b
Seperated types from hook functions
kraenhansen 1e96013
Adding a helper to generate a random realm path
kraenhansen 74fdcac
Use eslint-disable-next-line comment to disable a warning
kraenhansen 056df66
Adding a profileHook utility on-top-of renderHook
kraenhansen 88f53c1
Adding overloads to useQuery
kraenhansen 8330c9a
Adding failing tests
kraenhansen 340aec4
Refactored tests into using a "realm test context"
kraenhansen e745dfb
Passing key-paths through useQuery into the SDK's addListener method
kraenhansen 67b22af
Reusing randomRealmPath
kraenhansen b12282b
Made useObject tests use createRealmTestContext
kraenhansen 995cf7d
adding a useObject test for re-render on object creation
kraenhansen 307dc1b
Implement keyPaths on useObject
kraenhansen c0008ba
Incorporating feedback
kraenhansen 181626d
Allow passing a string as "keyPaths"
kraenhansen 0a8daef
Exporting types
kraenhansen fa91205
Adding @overload doc comments
kraenhansen 7dd55c8
Focusing docs on the desired call-pattern
kraenhansen fc9fe8a
Ran lint --fix
kraenhansen a566a53
Adding a AnyRealmObject utility type
kraenhansen 8f568f6
Using the `options` overload in the readme
kraenhansen 804fa6d
Adding a non-deprecated single argument overload
kraenhansen 2a165ca
Apply suggestions from code review
kraenhansen 0f6eb61
Using asserts instead of if-statements
kraenhansen 3483dc9
Moved RealmClassType, AnyRealmObject and isClassModelConstructor into…
kraenhansen 110eaf8
Implemented options object overload on useObject hook
kraenhansen 96bfc10
Adding a note to the changelog
kraenhansen ed9774e
Update packages/realm-react/CHANGELOG.md
kraenhansen File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/realm-react/src/__tests__/createRealmTestContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.