Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/react-native/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'no-undef': 'off',
'no-new': 'off',
'jsx-quotes': 0, // do not remove this line, this removes the requirement for double quotes in jsx/tsx. The single quotes in jsx help bluehawk replace testIDs in the generated snippets for the docs
'react-hooks/exhaustive-deps': 0,
'react/jsx-max-props-per-line': [0, {'maximum': 4, 'when': 'multiline'}],
},
};
13 changes: 13 additions & 0 deletions examples/react-native/__tests__/js/models/Cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Realm from 'realm';
// :snippet-start: js-cat-schema
class Cat extends Realm.Object {
static schema = {
name: 'Cat',
properties: {
name: 'string',
birthDate: 'mixed',
},
};
}
// :snippet-end:
export default Cat;
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import React, {useEffect} from 'react';
import {Text, View} from 'react-native';
import {render, waitFor} from '@testing-library/react-native';
import Realm from 'realm';
import {createRealmContext} from '@realm/react';
import Cat from '../../models/Cat';

jest.setTimeout(30000);

const realmConfig = {
schema: [Cat],
deleteRealmIfMigrationNeeded: true,
};

const {RealmProvider, useRealm, useQuery} = createRealmContext(realmConfig);

let assertionRealm;

describe('Mixed Tests', () => {
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(Cat));

new Cat(assertionRealm, {
name: 'Clover',
birthDate: new Date('January 21, 2016'),
});
});
});
afterAll(() => {
if (!assertionRealm.isClosed) {
assertionRealm.close();
}
});
it('should create an object with a mixed value', async () => {
// :snippet-start: create-mixed-object
// :replace-start: {
// "terms": {
// " testID='catItem'": ""
// }
// }
const CreateCatsInput = () => {
const realm = useRealm();

useEffect(() => {
// Add data to the Realm when the component mounts
Comment on lines +48 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
useEffect(() => {
// Add data to the Realm when the component mounts
// Add data to the Realm when the component mounts
useEffect(() => {

nit: think it'd be clearer if this comment before the hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it where it is because the comment is directly above the write transaction where you add the data

realm.write(() => {
// create a Cat with a birthDate value of type string
new Cat(realm, {
name: 'Euler',
birthDate: 'December 25th, 2017',
});
// create a Cat with a birthDate value of type date
new Cat(realm, {
name: 'Blaise',
birthDate: new Date('August 17, 2020'),
});

// create a Cat with a birthDate value of type int
new Cat(realm, {name: 'Euclid', birthDate: 10152021});

// create a Cat with a birthDate value of type null
new Cat(realm, {name: 'Pythagoras', birthDate: null});
});
}, []);

// retrieve all cats
const cats = useQuery(Cat);

return (
<>
{cats.map(cat => (
<View testID='catItem'>
<Text>{cat.name}</Text>
<Text>{String(cat.birthDate)}</Text>
</View>
))}
</>
);
};
// :replace-end:
// :snippet-end:

const App = () => (
<RealmProvider>
<CreateCatsInput />
</RealmProvider>
);

const {getAllByTestId} = render(<App />);

const catItems = await waitFor(() => getAllByTestId('catItem'), {
timeout: 5000,
});

// Test that 5 Cat Items have been added to the UI,
// and 5 matching Cat objects have been created in the assertionRealm
// (since there was already 1 cat object 'clover' created in the beforeEach)
// + the 4 new Cats
setTimeout(() => {
expect(catItems.length).toBe(5);
const cats = assertionRealm.objects(Cat);
expect(cats.length).toBe(5);
}, 5500);
});
it('should query for objects with a mixed value', async () => {
// :snippet-start: query-mixed-object
// :replace-start: {
// "terms": {
// " testID='catBirthDate'": ""
// }
// }
const CatInfoCard = ({catName}) => {
// To query for the cat's birthDate, filter for their name to retrieve the realm object.
// Use dot notation to access the birthDate property.
const cat = useQuery(Cat).filtered(`name = '${catName}'`)[0];
const catBirthDate = cat.birthDate;

if (cat) {
return (
<>
<Text>{catName}</Text>
<Text testID='catBirthDate'>{String(catBirthDate)}</Text>
</>
);
} else {
return <Text>Cat not found</Text>;
}
};
// :replace-end:
// :snippet-end:

const App = () => (
<RealmProvider>
<CatInfoCard catName='Clover' />
</RealmProvider>
);
const {getByTestId} = render(<App />);
const catBirthDate = await waitFor(() => getByTestId('catBirthDate'));
// Expect catBirthDate in the UI to be the same value we set in the beforeEach (which is clover's birthday 'January 21, 2016')
expect(new Date(catBirthDate.props.children)).toStrictEqual(
new Date('January 21, 2016'),
);
});
});
17 changes: 17 additions & 0 deletions examples/react-native/__tests__/ts/models/Cat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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-cat-schema
class Cat extends Realm.Object<Cat> {
name!: string;
birthDate?: Realm.Mixed;

static schema = {
name: 'Cat',
properties: {
name: 'string',
birthDate: 'mixed',
},
};
}
// :snippet-end:
export default Cat;
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import React, {useEffect} from 'react';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all comments/suggestions on examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx also apply to this file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import {Text, View} from 'react-native';
import {render, waitFor} from '@testing-library/react-native';
import Realm from 'realm';
import {createRealmContext} from '@realm/react';
import Cat from '../../models/Cat';

jest.setTimeout(30000);

const realmConfig = {
schema: [Cat],
deleteRealmIfMigrationNeeded: true,
};

const {RealmProvider, useRealm, useQuery} = createRealmContext(realmConfig);

let assertionRealm: Realm;

describe('Mixed Tests', () => {
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(Cat));

new Cat(assertionRealm, {
name: 'Clover',
birthDate: new Date('January 21, 2016'),
});
});
});
afterAll(() => {
if (!assertionRealm.isClosed) {
assertionRealm.close();
}
});
it('should create an object with a mixed value', async () => {
// :snippet-start: create-mixed-object
// :replace-start: {
// "terms": {
// " testID='catItem'": ""
// }
// }
const CreateCatsInput = () => {
const realm = useRealm();

useEffect(() => {
// Add data to the Realm when the component mounts
realm.write(() => {
// create a Cat with a birthDate value of type string
new Cat(realm, {
name: 'Euler',
birthDate: 'December 25th, 2017',
});
// create a Cat with a birthDate value of type date
new Cat(realm, {
name: 'Blaise',
birthDate: new Date('August 17, 2020'),
});

// create a Cat with a birthDate value of type int
new Cat(realm, {name: 'Euclid', birthDate: 10152021});

// create a Cat with a birthDate value of type null
new Cat(realm, {name: 'Pythagoras', birthDate: null});
});
}, []);

// retrieve all cats
const cats = useQuery(Cat);

return (
<>
{cats.map(cat => (
<View testID='catItem'>
<Text>{cat.name}</Text>
<Text>{String(cat.birthDate)}</Text>
</View>
))}
</>
);
};
// :replace-end:
// :snippet-end:

const App = () => (
<RealmProvider>
<CreateCatsInput />
</RealmProvider>
);

const {getAllByTestId} = render(<App />);

const catItems = await waitFor(() => getAllByTestId('catItem'), {
timeout: 5000,
});

// Test that 5 Cat Items have been added to the UI,
// and 5 matching Cat objects have been created in the assertionRealm
// (since there was already 1 cat object 'clover' created in the beforeEach)
// + the 4 new Cats
setTimeout(() => {
expect(catItems.length).toBe(5);
const cats = assertionRealm.objects(Cat);
expect(cats.length).toBe(5);
}, 5500);
});
it('should query for objects with a mixed value', async () => {
// :snippet-start: query-mixed-object
// :replace-start: {
// "terms": {
// " testID='catBirthDate'": ""
// }
// }
const CatInfoCard = ({catName}: {catName: string}) => {
// To query for the cat's birthDate, filter for their name to retrieve the realm object.
// Use dot notation to access the birthDate property.
const cat = useQuery(Cat).filtered(`name = '${catName}'`)[0];
const catBirthDate = cat.birthDate;

if (cat) {
return (
<>
<Text>{catName}</Text>
<Text testID='catBirthDate'>{String(catBirthDate)}</Text>
</>
);
} else {
return <Text>Cat not found</Text>;
}
};
// :replace-end:
// :snippet-end:

const App = () => (
<RealmProvider>
<CatInfoCard catName='Clover' />
</RealmProvider>
);
const {getByTestId} = render(<App />);
const catBirthDate = await waitFor(() => getByTestId('catBirthDate'));
// Expect catBirthDate in the UI to be the same value we set in the beforeEach (which is clover's birthday 'January 21, 2016')
expect(new Date(catBirthDate.props.children)).toStrictEqual(
new Date('January 21, 2016'),
);
});
});
2 changes: 2 additions & 0 deletions examples/react-native/testSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ global.console = {
error: jest.fn(),
warn: jest.fn(),
};

jest.setTimeout(30000);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Cat extends Realm.Object {
static schema = {
name: 'Cat',
properties: {
name: 'string',
birthDate: 'mixed',
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const CreateCatsInput = () => {
const realm = useRealm();

useEffect(() => {
// Add data to the Realm when the component mounts
realm.write(() => {
// create a Cat with a birthDate value of type string
new Cat(realm, {
name: 'Euler',
birthDate: 'December 25th, 2017',
});
// create a Cat with a birthDate value of type date
new Cat(realm, {
name: 'Blaise',
birthDate: new Date('August 17, 2020'),
});

// create a Cat with a birthDate value of type int
new Cat(realm, {name: 'Euclid', birthDate: 10152021});

// create a Cat with a birthDate value of type null
new Cat(realm, {name: 'Pythagoras', birthDate: null});
});
}, []);

// retrieve all cats
const cats = useQuery(Cat);

return (
<>
{cats.map(cat => (
<View>
<Text>{cat.name}</Text>
<Text>{String(cat.birthDate)}</Text>
</View>
))}
</>
);
};
Loading