-
Notifications
You must be signed in to change notification settings - Fork 88
(DOCSP-26985): @realm/reactify: UUID - React Native SDK #2443
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
Merged
mohammadhunan-dev
merged 8 commits into
mongodb:realm-react-guidance
from
mohammadhunan-dev:DOCSP-26985-uuid-object
Jan 4, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f100a80
added UUID tests
f1d3029
added printWidth to fix testID spacing + rebluehawked + added copy
e23c5c0
improve grammer + wording
dec6eea
wording update
e21c558
address Pr comments
216da9f
address Pr comments
449cfdb
address Pr comments
fd09182
Merge branch 'realm-react-guidance' into DOCSP-26985-uuid-object
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,14 @@ | ||
import Realm from 'realm'; | ||
// :snippet-start: js-profile-schema | ||
class Profile extends Realm.Object { | ||
static schema = { | ||
name: 'Profile', | ||
primaryKey: '_id', | ||
properties: { | ||
_id: 'uuid', | ||
name: 'string', | ||
}, | ||
}; | ||
} | ||
// :snippet-end: | ||
export default Profile; |
95 changes: 95 additions & 0 deletions
95
examples/react-native/__tests__/js/realm-database/schemas/uuid.test.jsx
This file contains hidden or 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,95 @@ | ||
import React, {useState} from 'react'; | ||
import {Button, TextInput, View} from 'react-native'; | ||
import {render, fireEvent, waitFor, act} from '@testing-library/react-native'; | ||
import Realm from 'realm'; | ||
import {createRealmContext} from '@realm/react'; | ||
import Profile from '../../models/Profile'; | ||
|
||
const realmConfig = { | ||
schema: [Profile], | ||
deleteRealmIfMigrationNeeded: true, | ||
}; | ||
|
||
const {RealmProvider, useRealm} = createRealmContext(realmConfig); | ||
|
||
let assertionRealm; | ||
|
||
// test describe block for the uuid schema | ||
describe('uuid schema', () => { | ||
beforeEach(async () => { | ||
// we will use this Realm for assertions to access Realm Objects outside of a Functional Component (like required by @realm/react) | ||
assertionRealm = await Realm.open(realmConfig); | ||
|
||
// delete every object in the realmConfig in the Realm to make test idempotent | ||
assertionRealm.write(() => { | ||
assertionRealm.delete(assertionRealm.objects(Profile)); | ||
|
||
new Profile(assertionRealm, { | ||
name: 'Tony Stark', | ||
_id: new Realm.BSON.UUID(), | ||
}); | ||
}); | ||
}); | ||
afterAll(() => { | ||
if (!assertionRealm.isClosed) { | ||
assertionRealm.close(); | ||
} | ||
}); | ||
|
||
it('should be able to create a new uuid', async () => { | ||
// :snippet-start: create-uuid-object | ||
// :replace-start: { | ||
// "terms": { | ||
// " testID='nameInput'": "", | ||
// " testID='createProfileButton'": "" | ||
// } | ||
// } | ||
const CreateProfileInput = () => { | ||
const realm = useRealm(); | ||
const [name, setName] = useState(''); | ||
|
||
// createProfile creates a new 'Profile' Realm Object with a new UUID based on user input | ||
const createProfile = () => { | ||
realm.write(() => { | ||
new Profile(realm, { | ||
name, | ||
_id: new Realm.BSON.UUID(), | ||
}); | ||
}); | ||
}; | ||
return ( | ||
<View> | ||
<TextInput testID='nameInput' placeholder='Name' onChangeText={setName} /> | ||
<Button testID='createProfileButton' title='Create Profile' onPress={createProfile} /> | ||
</View> | ||
); | ||
// :replace-end: | ||
// :snippet-end: | ||
}; | ||
const App = () => ( | ||
<RealmProvider> | ||
<CreateProfileInput /> | ||
</RealmProvider> | ||
); | ||
|
||
const {getByTestId} = render(<App />); | ||
|
||
// Test that the createProfileButton's onPress method creates a new Profile Object with a new UUID | ||
const nameInput = await waitFor(() => getByTestId('nameInput'), { | ||
timeout: 5000, | ||
}); | ||
const createProfileButton = await waitFor(() => getByTestId('createProfileButton'), {timeout: 5000}); | ||
|
||
await act(() => { | ||
fireEvent.changeText(nameInput, 'Steve Rogers'); | ||
}); | ||
await act(() => { | ||
fireEvent.press(createProfileButton); | ||
}); | ||
|
||
// Test that the Profile Object with the new UUID was created using the assertionRealm and that the name is correct | ||
const profiles = assertionRealm.objects(Profile); | ||
expect(profiles.length).toBe(2); | ||
expect(profiles[1].name).toBe('Steve Rogers'); | ||
}); | ||
}); |
This file contains hidden or 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,18 @@ | ||
import Realm from 'realm'; | ||
// TODO: Replace `static schema` with TS-first models + realm-babel-plugin (https://www.npmjs.com/package/@realm/babel-plugin) approach once realm-babel-plugin version 0.1.2 releases with bug fixes | ||
// :snippet-start: ts-profile-schema | ||
class Profile extends Realm.Object<Profile> { | ||
_id!: Realm.BSON.UUID; | ||
name!: string; | ||
|
||
static schema = { | ||
name: 'Profile', | ||
primaryKey: '_id', | ||
properties: { | ||
_id: 'uuid', | ||
name: 'string', | ||
}, | ||
}; | ||
} | ||
// :snippet-end: | ||
export default Profile; |
95 changes: 95 additions & 0 deletions
95
examples/react-native/__tests__/ts/realm-database/schemas/uuid.test.tsx
This file contains hidden or 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,95 @@ | ||
import React, {useState} from 'react'; | ||
import {Button, TextInput, View} from 'react-native'; | ||
import {render, fireEvent, waitFor, act} from '@testing-library/react-native'; | ||
import Realm from 'realm'; | ||
import {createRealmContext} from '@realm/react'; | ||
import Profile from '../../models/Profile'; | ||
|
||
const realmConfig = { | ||
schema: [Profile], | ||
deleteRealmIfMigrationNeeded: true, | ||
}; | ||
|
||
const {RealmProvider, useRealm} = createRealmContext(realmConfig); | ||
|
||
let assertionRealm: Realm; | ||
|
||
// test describe block for the uuid schema | ||
describe('uuid schema', () => { | ||
beforeEach(async () => { | ||
// we will use this Realm for assertions to access Realm Objects outside of a Functional Component (like required by @realm/react) | ||
assertionRealm = await Realm.open(realmConfig); | ||
|
||
// delete every object in the realmConfig in the Realm to make test idempotent | ||
assertionRealm.write(() => { | ||
assertionRealm.delete(assertionRealm.objects(Profile)); | ||
|
||
new Profile(assertionRealm, { | ||
name: 'Tony Stark', | ||
_id: new Realm.BSON.UUID(), | ||
}); | ||
}); | ||
}); | ||
afterAll(() => { | ||
if (!assertionRealm.isClosed) { | ||
assertionRealm.close(); | ||
} | ||
}); | ||
|
||
it('should be able to create a new uuid', async () => { | ||
// :snippet-start: create-uuid-object | ||
// :replace-start: { | ||
// "terms": { | ||
// " testID='nameInput'": "", | ||
// " testID='createProfileButton'": "" | ||
// } | ||
// } | ||
const CreateProfileInput = () => { | ||
const realm = useRealm(); | ||
const [name, setName] = useState(''); | ||
|
||
// createProfile creates a new 'Profile' Realm Object with a new UUID based on user input | ||
const createProfile = () => { | ||
realm.write(() => { | ||
new Profile(realm, { | ||
name, | ||
_id: new Realm.BSON.UUID(), | ||
}); | ||
}); | ||
}; | ||
return ( | ||
<View> | ||
<TextInput testID='nameInput' placeholder='Name' onChangeText={setName} /> | ||
<Button testID='createProfileButton' title='Create Profile' onPress={createProfile} /> | ||
</View> | ||
); | ||
// :replace-end: | ||
// :snippet-end: | ||
}; | ||
const App = () => ( | ||
<RealmProvider> | ||
<CreateProfileInput /> | ||
</RealmProvider> | ||
); | ||
|
||
const {getByTestId} = render(<App />); | ||
|
||
// Test that the createProfileButton's onPress method creates a new Profile Object with a new UUID | ||
const nameInput = await waitFor(() => getByTestId('nameInput'), { | ||
timeout: 5000, | ||
}); | ||
const createProfileButton = await waitFor(() => getByTestId('createProfileButton'), {timeout: 5000}); | ||
|
||
await act(() => { | ||
fireEvent.changeText(nameInput, 'Steve Rogers'); | ||
}); | ||
await act(() => { | ||
fireEvent.press(createProfileButton); | ||
}); | ||
|
||
// Test that the Profile Object with the new UUID was created using the assertionRealm and that the name is correct | ||
const profiles = assertionRealm.objects(Profile); | ||
expect(profiles.length).toBe(2); | ||
expect(profiles[1].name).toBe('Steve Rogers'); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
source/examples/generated/react-native/js/Profile.snippet.js-profile-schema.js
This file contains hidden or 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,10 @@ | ||
class Profile extends Realm.Object { | ||
static schema = { | ||
name: 'Profile', | ||
primaryKey: '_id', | ||
properties: { | ||
_id: 'uuid', | ||
name: 'string', | ||
}, | ||
}; | ||
} |
19 changes: 19 additions & 0 deletions
19
source/examples/generated/react-native/js/uuid.test.snippet.create-uuid-object.jsx
This file contains hidden or 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,19 @@ | ||
const CreateProfileInput = () => { | ||
const realm = useRealm(); | ||
const [name, setName] = useState(''); | ||
|
||
// createProfile creates a new 'Profile' Realm Object with a new UUID based on user input | ||
const createProfile = () => { | ||
realm.write(() => { | ||
new Profile(realm, { | ||
name, | ||
_id: new Realm.BSON.UUID(), | ||
}); | ||
}); | ||
}; | ||
return ( | ||
<View> | ||
<TextInput placeholder='Name' onChangeText={setName} /> | ||
<Button title='Create Profile' onPress={createProfile} /> | ||
</View> | ||
); |
13 changes: 13 additions & 0 deletions
13
source/examples/generated/react-native/ts/Profile.snippet.ts-profile-schema.ts
This file contains hidden or 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,13 @@ | ||
class Profile extends Realm.Object<Profile> { | ||
_id!: Realm.BSON.UUID; | ||
name!: string; | ||
|
||
static schema = { | ||
name: 'Profile', | ||
primaryKey: '_id', | ||
properties: { | ||
_id: 'uuid', | ||
name: 'string', | ||
}, | ||
}; | ||
} |
19 changes: 19 additions & 0 deletions
19
source/examples/generated/react-native/ts/uuid.test.snippet.create-uuid-object.tsx
This file contains hidden or 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,19 @@ | ||
const CreateProfileInput = () => { | ||
const realm = useRealm(); | ||
const [name, setName] = useState(''); | ||
|
||
// createProfile creates a new 'Profile' Realm Object with a new UUID based on user input | ||
const createProfile = () => { | ||
realm.write(() => { | ||
new Profile(realm, { | ||
name, | ||
_id: new Realm.BSON.UUID(), | ||
}); | ||
}); | ||
}; | ||
return ( | ||
<View> | ||
<TextInput placeholder='Name' onChangeText={setName} /> | ||
<Button title='Create Profile' onPress={createProfile} /> | ||
</View> | ||
); |
This file contains hidden or 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
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.
[minor] I know you didn't change line 38 in this PR, but we may want to remove "the string". I think readers will understand its a string, and mentioning it here may confuse some readers - make them think that the object type is a string.