From d739ec39e1352ae5877879cb9babace1f8c122fe Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 13:09:26 -0500 Subject: [PATCH 01/19] mixed tests --- .../react-native/__tests__/ts/models/Cat.ts | 20 +++++ .../ts/realm-database/schemas/mixed-test.tsx | 86 +++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 examples/react-native/__tests__/ts/models/Cat.ts create mode 100644 examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx diff --git a/examples/react-native/__tests__/ts/models/Cat.ts b/examples/react-native/__tests__/ts/models/Cat.ts new file mode 100644 index 0000000000..3764c513db --- /dev/null +++ b/examples/react-native/__tests__/ts/models/Cat.ts @@ -0,0 +1,20 @@ +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-dog-schema +class Cat extends Realm.Object { + name!: string; + birthDate?: Realm.Mixed; + + static schema = { + name: 'Dog', + properties: { + name: 'string', + birthDate: 'mixed', + }, + }; +} +// :snippet-end: +export default Cat; + +// name: "string", +// birthDate: "mixed", diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx new file mode 100644 index 0000000000..c46cdc6f10 --- /dev/null +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -0,0 +1,86 @@ +import React, {useEffect, useState} from 'react'; +import {Button, TextInput, Text, View} from 'react-native'; +import {render, fireEvent, waitFor, act} from '@testing-library/react-native'; +import Realm from 'realm'; +import {createRealmContext} from '@realm/react'; +import Cat from '../../models/Cat'; + +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)); + }); + }); + afterAll(() => { + if (!assertionRealm.isClosed) { + assertionRealm.close(); + } + }); + it('should create an object with a mixed value', async () => { + const CreateCatsInput = () => { + const realm = useRealm(); + + useEffect(() => { + realm.write(() => { + // create a Dog with a birthDate value of type string + new Cat(realm, { + name: 'Euler', + birthDate: 'December 25th, 2017', + }); + // create a Dog with a birthDate value of type date + new Cat(realm, { + name: 'Blaise', + birthDate: new Date('August 17, 2020'), + }); + + // create a Dog with a birthDate value of type int + new Cat(realm, {name: 'Euclid', birthDate: 10152021}); + + // create a Dog with a birthDate value of type null + new Cat(realm, {name: 'Pythagoras', birthDate: null}); + }); + }, [realm]); + + const cats = useQuery(Cat); + + return ( + <> + {cats.map(cat => ( + + {cat.name} + {String(cat.birthDate)} + + ))} + + ); + }; + + const App = () => ( + + + + ); + + const {getAllByTestId} = render(); + + const catItems = await waitFor(() => getAllByTestId('catItem')); + + // Test that 4 Cat Items have been added to the UI, and 4 matching Cat objects have been created in the realm + expect(catItems.length).toBe(4); + const cats = assertionRealm.objects(Cat); + expect(cats.length).toBe(4); + }); +}); From e464fc6f046298f67541cdb6d28075c95a3c5517 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 16:48:17 -0500 Subject: [PATCH 02/19] added query tests --- .../react-native/__tests__/ts/models/Cat.ts | 2 +- .../ts/realm-database/schemas/mixed-test.tsx | 47 +++++++++++++++++-- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/examples/react-native/__tests__/ts/models/Cat.ts b/examples/react-native/__tests__/ts/models/Cat.ts index 3764c513db..f95c7db08f 100644 --- a/examples/react-native/__tests__/ts/models/Cat.ts +++ b/examples/react-native/__tests__/ts/models/Cat.ts @@ -6,7 +6,7 @@ class Cat extends Realm.Object { birthDate?: Realm.Mixed; static schema = { - name: 'Dog', + name: 'Cat', properties: { name: 'string', birthDate: 'mixed', diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx index c46cdc6f10..719a9ad23c 100644 --- a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -22,6 +22,11 @@ describe('Mixed Tests', () => { // 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(() => { @@ -52,6 +57,8 @@ describe('Mixed Tests', () => { // create a Dog with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); + + console.log('use effect ran?'); }, [realm]); const cats = useQuery(Cat); @@ -78,9 +85,41 @@ describe('Mixed Tests', () => { const catItems = await waitFor(() => getAllByTestId('catItem')); - // Test that 4 Cat Items have been added to the UI, and 4 matching Cat objects have been created in the realm - expect(catItems.length).toBe(4); - const cats = assertionRealm.objects(Cat); - expect(cats.length).toBe(4); + // 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); + }, 3000); + }); + it('should query for objects with a mixed value', async () => { + 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 ( + <> + {catName} + {String(catBirthDate)} + + ); + } else { + return Cat not found; + } + }; + const App = () => ( + + + + ); + const {getByTestId} = render(); + 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)).toBe( + new Date('January 21, 2016'), + ); }); }); From 92152c9ba17e7a2254c781ead0ab0b727296c2da Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 17:52:13 -0500 Subject: [PATCH 03/19] swap toBe with toStrictEqual --- .../__tests__/ts/realm-database/schemas/mixed-test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx index 719a9ad23c..f6a3adb127 100644 --- a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -118,7 +118,7 @@ describe('Mixed Tests', () => { const {getByTestId} = render(); 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)).toBe( + expect(new Date(catBirthDate.props.children)).toStrictEqual( new Date('January 21, 2016'), ); }); From 13ae55dd4345d9efd9311250476b0167c302c2d3 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 17:57:20 -0500 Subject: [PATCH 04/19] increase timeout --- examples/react-native/testSetup.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/react-native/testSetup.js b/examples/react-native/testSetup.js index d60624c7c8..baec70af03 100644 --- a/examples/react-native/testSetup.js +++ b/examples/react-native/testSetup.js @@ -3,7 +3,9 @@ jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper'); // avoid erro jest.setTimeout(10000); global.console = { ...global.console, - log: jest.fn(), + // log: jest.fn(), error: jest.fn(), warn: jest.fn(), }; + +jest.setTimeout(30000); From 8f60cd106e05d9485cb7c8517de89714f78f7a15 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 17:59:39 -0500 Subject: [PATCH 05/19] increase timeout --- .../js/realm-database/schemas/mixed-test.jsx | 0 .../ts/realm-database/schemas/mixed-test.tsx | 22 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 examples/react-native/__tests__/js/realm-database/schemas/mixed-test.jsx diff --git a/examples/react-native/__tests__/js/realm-database/schemas/mixed-test.jsx b/examples/react-native/__tests__/js/realm-database/schemas/mixed-test.jsx new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx index f6a3adb127..db45036fc9 100644 --- a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -5,6 +5,8 @@ import Realm from 'realm'; import {createRealmContext} from '@realm/react'; import Cat from '../../models/Cat'; +jest.setTimeout(30000); + const realmConfig = { schema: [Cat], deleteRealmIfMigrationNeeded: true, @@ -35,6 +37,12 @@ describe('Mixed Tests', () => { } }); 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(); @@ -57,10 +65,9 @@ describe('Mixed Tests', () => { // create a Dog with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); - - console.log('use effect ran?'); }, [realm]); + // retrieve all cats const cats = useQuery(Cat); return ( @@ -74,6 +81,8 @@ describe('Mixed Tests', () => { ); }; + // :replace-end: + // :snippet-end: const App = () => ( @@ -93,6 +102,12 @@ describe('Mixed Tests', () => { }, 3000); }); 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. @@ -110,6 +125,9 @@ describe('Mixed Tests', () => { return Cat not found; } }; + // :replace-end: + // :snippet-end: + const App = () => ( From 9095dfa504828ea11dc0ce7dc2de71533329c594 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 18:14:08 -0500 Subject: [PATCH 06/19] kept tsx test the same but removed jsx test to get it to pass ideally --- .../__tests__/js/realm-database/schemas/mixed-test.jsx | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 examples/react-native/__tests__/js/realm-database/schemas/mixed-test.jsx diff --git a/examples/react-native/__tests__/js/realm-database/schemas/mixed-test.jsx b/examples/react-native/__tests__/js/realm-database/schemas/mixed-test.jsx deleted file mode 100644 index e69de29bb2..0000000000 From 5eb84084b55c1d9d32d4cf9948ed2428521ec585 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 18:20:18 -0500 Subject: [PATCH 07/19] attempt to fiddle with timeouts --- .../__tests__/ts/realm-database/schemas/mixed-test.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx index db45036fc9..5ff876cb69 100644 --- a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -92,14 +92,16 @@ describe('Mixed Tests', () => { const {getAllByTestId} = render(); - const catItems = await waitFor(() => getAllByTestId('catItem')); + 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); - }, 3000); + }, 5500); }); it('should query for objects with a mixed value', async () => { // :snippet-start: query-mixed-object From 7b2288cac69cd126f3c546a42c8b558c2b25b129 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 18:45:11 -0500 Subject: [PATCH 08/19] add js tests + ran prettier + lint --- .../react-native/__tests__/js/models/Cat.js | 13 ++ .../js/realm-database/schemas/mixed-tests.jsx | 145 ++++++++++++++++++ .../react-native/__tests__/ts/models/Cat.ts | 5 +- .../ts/realm-database/schemas/mixed-test.tsx | 6 +- 4 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 examples/react-native/__tests__/js/models/Cat.js create mode 100644 examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx diff --git a/examples/react-native/__tests__/js/models/Cat.js b/examples/react-native/__tests__/js/models/Cat.js new file mode 100644 index 0000000000..9c7d4ac922 --- /dev/null +++ b/examples/react-native/__tests__/js/models/Cat.js @@ -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; diff --git a/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx new file mode 100644 index 0000000000..6927d1cbd0 --- /dev/null +++ b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx @@ -0,0 +1,145 @@ +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(() => { + realm.write(() => { + // create a Dog with a birthDate value of type string + new Cat(realm, { + name: 'Euler', + birthDate: 'December 25th, 2017', + }); + // create a Dog with a birthDate value of type date + new Cat(realm, { + name: 'Blaise', + birthDate: new Date('August 17, 2020'), + }); + + // create a Dog with a birthDate value of type int + new Cat(realm, {name: 'Euclid', birthDate: 10152021}); + + // create a Dog with a birthDate value of type null + new Cat(realm, {name: 'Pythagoras', birthDate: null}); + }); + }, [realm]); + + // retrieve all cats + const cats = useQuery(Cat); + + return ( + <> + {cats.map(cat => ( + + {cat.name} + {String(cat.birthDate)} + + ))} + + ); + }; + // :replace-end: + // :snippet-end: + + const App = () => ( + + + + ); + + const {getAllByTestId} = render(); + + 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 ( + <> + {catName} + {String(catBirthDate)} + + ); + } else { + return Cat not found; + } + }; + // :replace-end: + // :snippet-end: + + const App = () => ( + + + + ); + const {getByTestId} = render(); + 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'), + ); + }); +}); diff --git a/examples/react-native/__tests__/ts/models/Cat.ts b/examples/react-native/__tests__/ts/models/Cat.ts index f95c7db08f..63e299b30e 100644 --- a/examples/react-native/__tests__/ts/models/Cat.ts +++ b/examples/react-native/__tests__/ts/models/Cat.ts @@ -1,6 +1,6 @@ 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-dog-schema +// :snippet-start: ts-cat-schema class Cat extends Realm.Object { name!: string; birthDate?: Realm.Mixed; @@ -15,6 +15,3 @@ class Cat extends Realm.Object { } // :snippet-end: export default Cat; - -// name: "string", -// birthDate: "mixed", diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx index 5ff876cb69..1e0b300ef5 100644 --- a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -1,6 +1,6 @@ -import React, {useEffect, useState} from 'react'; -import {Button, TextInput, Text, View} from 'react-native'; -import {render, fireEvent, waitFor, act} from '@testing-library/react-native'; +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'; From a7e5ef7c73973898689143036a62152a9fe549ef Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 18:52:40 -0500 Subject: [PATCH 09/19] bluehawked code examples --- .../js/Cat.snippet.js-cat-schema.js | 9 ++++ ...ixed-tests.snippet.create-mixed-object.jsx | 38 +++++++++++++ ...mixed-tests.snippet.query-mixed-object.jsx | 17 ++++++ .../ts/Cat.snippet.ts-cat-schema.ts | 12 +++++ ...mixed-test.snippet.create-mixed-object.tsx | 38 +++++++++++++ .../mixed-test.snippet.query-mixed-object.tsx | 17 ++++++ .../realm-database/schemas/mixed.txt | 54 ++++++++++++++++--- 7 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 source/examples/generated/react-native/js/Cat.snippet.js-cat-schema.js create mode 100644 source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx create mode 100644 source/examples/generated/react-native/js/mixed-tests.snippet.query-mixed-object.jsx create mode 100644 source/examples/generated/react-native/ts/Cat.snippet.ts-cat-schema.ts create mode 100644 source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx create mode 100644 source/examples/generated/react-native/ts/mixed-test.snippet.query-mixed-object.tsx diff --git a/source/examples/generated/react-native/js/Cat.snippet.js-cat-schema.js b/source/examples/generated/react-native/js/Cat.snippet.js-cat-schema.js new file mode 100644 index 0000000000..7b323b3d6a --- /dev/null +++ b/source/examples/generated/react-native/js/Cat.snippet.js-cat-schema.js @@ -0,0 +1,9 @@ +class Cat extends Realm.Object { + static schema = { + name: 'Cat', + properties: { + name: 'string', + birthDate: 'mixed', + }, + }; +} diff --git a/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx b/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx new file mode 100644 index 0000000000..91bc3b7153 --- /dev/null +++ b/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx @@ -0,0 +1,38 @@ +const CreateCatsInput = () => { + const realm = useRealm(); + + useEffect(() => { + realm.write(() => { + // create a Dog with a birthDate value of type string + new Cat(realm, { + name: 'Euler', + birthDate: 'December 25th, 2017', + }); + // create a Dog with a birthDate value of type date + new Cat(realm, { + name: 'Blaise', + birthDate: new Date('August 17, 2020'), + }); + + // create a Dog with a birthDate value of type int + new Cat(realm, {name: 'Euclid', birthDate: 10152021}); + + // create a Dog with a birthDate value of type null + new Cat(realm, {name: 'Pythagoras', birthDate: null}); + }); + }, [realm]); + + // retrieve all cats + const cats = useQuery(Cat); + + return ( + <> + {cats.map(cat => ( + + {cat.name} + {String(cat.birthDate)} + + ))} + + ); +}; diff --git a/source/examples/generated/react-native/js/mixed-tests.snippet.query-mixed-object.jsx b/source/examples/generated/react-native/js/mixed-tests.snippet.query-mixed-object.jsx new file mode 100644 index 0000000000..aca8c8f71f --- /dev/null +++ b/source/examples/generated/react-native/js/mixed-tests.snippet.query-mixed-object.jsx @@ -0,0 +1,17 @@ +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 ( + <> + {catName} + {String(catBirthDate)} + + ); + } else { + return Cat not found; + } +}; diff --git a/source/examples/generated/react-native/ts/Cat.snippet.ts-cat-schema.ts b/source/examples/generated/react-native/ts/Cat.snippet.ts-cat-schema.ts new file mode 100644 index 0000000000..4e79383336 --- /dev/null +++ b/source/examples/generated/react-native/ts/Cat.snippet.ts-cat-schema.ts @@ -0,0 +1,12 @@ +class Cat extends Realm.Object { + name!: string; + birthDate?: Realm.Mixed; + + static schema = { + name: 'Cat', + properties: { + name: 'string', + birthDate: 'mixed', + }, + }; +} diff --git a/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx b/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx new file mode 100644 index 0000000000..91bc3b7153 --- /dev/null +++ b/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx @@ -0,0 +1,38 @@ +const CreateCatsInput = () => { + const realm = useRealm(); + + useEffect(() => { + realm.write(() => { + // create a Dog with a birthDate value of type string + new Cat(realm, { + name: 'Euler', + birthDate: 'December 25th, 2017', + }); + // create a Dog with a birthDate value of type date + new Cat(realm, { + name: 'Blaise', + birthDate: new Date('August 17, 2020'), + }); + + // create a Dog with a birthDate value of type int + new Cat(realm, {name: 'Euclid', birthDate: 10152021}); + + // create a Dog with a birthDate value of type null + new Cat(realm, {name: 'Pythagoras', birthDate: null}); + }); + }, [realm]); + + // retrieve all cats + const cats = useQuery(Cat); + + return ( + <> + {cats.map(cat => ( + + {cat.name} + {String(cat.birthDate)} + + ))} + + ); +}; diff --git a/source/examples/generated/react-native/ts/mixed-test.snippet.query-mixed-object.tsx b/source/examples/generated/react-native/ts/mixed-test.snippet.query-mixed-object.tsx new file mode 100644 index 0000000000..eda7559b6c --- /dev/null +++ b/source/examples/generated/react-native/ts/mixed-test.snippet.query-mixed-object.tsx @@ -0,0 +1,17 @@ +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 ( + <> + {catName} + {String(catBirthDate)} + + ); + } else { + return Cat not found; + } +}; diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index 77e90dfaf8..7ade796f52 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -29,16 +29,43 @@ To :ref:`set a property of your object model ` as ``Mixed``, set the property's type to "``mixed``". -.. literalinclude:: /examples/generated/node/data-types.snippet.define-mixed-in-schema.js - :language: javascript +.. tabs-realm-languages:: + + .. tab:: + :tabid: typescript + + .. literalinclude:: /examples/generated/react-native/ts/Cat.snippet.ts-cat-schema.ts + :language: typescript + + .. tab:: + :tabid: javascript + + .. literalinclude:: /examples/generated/react-native/js/Cat.snippet.js-cat-schema.js + :language: javascript + Create an Object With a Mixed Value ----------------------------------- Create an object with a mixed value by running the :js-sdk:`realm.create() ` method within a write transaction. -.. literalinclude:: /examples/generated/node/data-types.snippet.create-objects-with-mixed-values.js - :language: javascript +.. tabs-realm-languages:: + + .. tab:: + :tabid: typescript + + .. literalinclude:: /examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx + :language: typescript + :emphasize-lines: 2, 5-21 + :linenos: + + .. tab:: + :tabid: javascript + + .. literalinclude:: /examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx + :language: javascript + :emphasize-lines: 2, 5-21 + :linenos: Query for Objects with a Mixed Value ------------------------------------ @@ -47,7 +74,22 @@ Query for objects with a mixed value by running the passing in a :ref:`filter ` for a non-mixed field. You can then print the value of the mixed property or the entire object itself. -.. literalinclude:: /examples/generated/node/data-types.snippet.query-objects-with-mixed-values.js - :language: javascript +.. tabs-realm-languages:: + + .. tab:: + :tabid: typescript + + .. literalinclude:: /examples/generated/react-native/ts/mixed-test.snippet.query-mixed-object.tsx + :language: typescript + :emphasize-lines: 4-5 + :linenos: + + .. tab:: + :tabid: javascript + + .. literalinclude:: /examples/generated/react-native/js/mixed-tests.snippet.query-mixed-object.jsx + :language: javascript + :emphasize-lines: 4-5 + :linenos: From e60ce66dd11587ee5bec005b885081cce769b12f Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 19:35:01 -0500 Subject: [PATCH 10/19] updated dependency array to be empty + updated copy --- examples/react-native/.eslintrc.js | 1 + .../js/realm-database/schemas/mixed-tests.jsx | 2 +- .../ts/realm-database/schemas/mixed-test.tsx | 2 +- ...ixed-tests.snippet.create-mixed-object.jsx | 2 +- ...mixed-test.snippet.create-mixed-object.tsx | 2 +- .../realm-database/schemas/mixed.txt | 30 ++++++++++++++++++- 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/examples/react-native/.eslintrc.js b/examples/react-native/.eslintrc.js index 668216be93..a219170c77 100644 --- a/examples/react-native/.eslintrc.js +++ b/examples/react-native/.eslintrc.js @@ -12,5 +12,6 @@ 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, }, }; diff --git a/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx index 6927d1cbd0..0da22a1ce9 100644 --- a/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx +++ b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx @@ -65,7 +65,7 @@ describe('Mixed Tests', () => { // create a Dog with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); - }, [realm]); + }, []); // retrieve all cats const cats = useQuery(Cat); diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx index 1e0b300ef5..24592c5bc2 100644 --- a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -65,7 +65,7 @@ describe('Mixed Tests', () => { // create a Dog with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); - }, [realm]); + }, []); // retrieve all cats const cats = useQuery(Cat); diff --git a/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx b/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx index 91bc3b7153..5fadb496b7 100644 --- a/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx +++ b/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx @@ -20,7 +20,7 @@ const CreateCatsInput = () => { // create a Dog with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); - }, [realm]); + }, []); // retrieve all cats const cats = useQuery(Cat); diff --git a/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx b/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx index 91bc3b7153..5fadb496b7 100644 --- a/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx +++ b/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx @@ -20,7 +20,7 @@ const CreateCatsInput = () => { // create a Dog with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); - }, [realm]); + }, []); // retrieve all cats const cats = useQuery(Cat); diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index 7ade796f52..5ec0ad4ec0 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -47,7 +47,23 @@ To :ref:`set a property of your object model Create an Object With a Mixed Value ----------------------------------- Create an object with a mixed value by running the :js-sdk:`realm.create() -` method within a write transaction. +` method within a :ref:`write transaction +`. + +Example +~~~~~~~ + +In the following ``CreateCatsInput`` example we create several ``Cat`` realm +objects that have a ``mixed`` type for the ``birthDate`` field. + +The ``CreateCatsInput`` component does the following: + +- Get access to the opened realm instance by calling the ``useRealm()`` hook. +- Use React's `useEffect `__ hook to call an anonymous function only once, by passing ``useEffect`` an `empty dependency array `__ as a second argument. +- Within the anonymous function, we create four different ``Cat`` objects by using the :mdn:`new ` operator to create a new realm object within a write transaction. Each of the ``Cat`` objects use a different data type for the ``birthDate`` property. +- Use the ``useQuery()`` hook to retrieve all ``Cat`` objects. +- `Map `__ through the cats to render a list of ``Text`` components displaying each cat's ``name`` and ``birthDate``. + .. tabs-realm-languages:: @@ -74,6 +90,18 @@ Query for objects with a mixed value by running the passing in a :ref:`filter ` for a non-mixed field. You can then print the value of the mixed property or the entire object itself. +Example +~~~~~~~ + +In the following ``CatInfoCard`` example we query for a ``Cat`` object using the +cat's name. + +The ``CatInfoCard`` component does the following: + +- Get all ``Cat`` objects by passing the ``Cat`` class to the ``useQuery()`` hook, and then use :js-sdk:`filtered() ` to filter the results to receive only the cats whose name match the name passed as a prop. We then get the first matching cat, and store it as a const variable. +- Use dot notation to retrieve the mixed property, ``birthDate``. +- Display the cat's name and birthdate in the render method if the cat was found. If there is no cat that matches the name passed in to the component as a prop, we render text that says "Cat not found". + .. tabs-realm-languages:: .. tab:: From 91292b86d9a648780ecac2a91f47b71e97ea50d4 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 19:43:18 -0500 Subject: [PATCH 11/19] wording improvements + grammar fixes --- .../realm-database/schemas/mixed.txt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index 5ec0ad4ec0..40fa6b50fc 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -53,14 +53,14 @@ Create an object with a mixed value by running the :js-sdk:`realm.create() Example ~~~~~~~ -In the following ``CreateCatsInput`` example we create several ``Cat`` realm -objects that have a ``mixed`` type for the ``birthDate`` field. +In the following ``CreateCatsInput`` example, we create several ``Cat`` realm +objects with a ``mixed`` type for the ``birthDate`` field. The ``CreateCatsInput`` component does the following: - Get access to the opened realm instance by calling the ``useRealm()`` hook. -- Use React's `useEffect `__ hook to call an anonymous function only once, by passing ``useEffect`` an `empty dependency array `__ as a second argument. -- Within the anonymous function, we create four different ``Cat`` objects by using the :mdn:`new ` operator to create a new realm object within a write transaction. Each of the ``Cat`` objects use a different data type for the ``birthDate`` property. +- Use React's `useEffect `__ hook to call an anonymous function only once by passing ``useEffect`` an `empty dependency array `__ as a second argument. +- Within the anonymous function, we create four different ``Cat`` objects by using the :mdn:`new ` operator to create a new realm object within a write transaction. Each of the ``Cat`` objects uses a different data type for the ``birthDate`` property. - Use the ``useQuery()`` hook to retrieve all ``Cat`` objects. - `Map `__ through the cats to render a list of ``Text`` components displaying each cat's ``name`` and ``birthDate``. @@ -85,7 +85,7 @@ The ``CreateCatsInput`` component does the following: Query for Objects with a Mixed Value ------------------------------------ -Query for objects with a mixed value by running the +To query for objects with a mixed value, run the :js-sdk:`Collection.filtered() ` method and passing in a :ref:`filter ` for a non-mixed field. You can then print the value of the mixed property or the entire object itself. @@ -93,14 +93,14 @@ then print the value of the mixed property or the entire object itself. Example ~~~~~~~ -In the following ``CatInfoCard`` example we query for a ``Cat`` object using the +In the following ``CatInfoCard`` example, we query for a ``Cat`` object using the cat's name. The ``CatInfoCard`` component does the following: -- Get all ``Cat`` objects by passing the ``Cat`` class to the ``useQuery()`` hook, and then use :js-sdk:`filtered() ` to filter the results to receive only the cats whose name match the name passed as a prop. We then get the first matching cat, and store it as a const variable. +- Get all ``Cat`` objects by passing the ``Cat`` class to the ``useQuery()`` hook, and then use :js-sdk:`filtered() ` to filter the results to receive only the cats whose name match the name passed as a prop. We then get the first matching cat and store it as a const variable. - Use dot notation to retrieve the mixed property, ``birthDate``. -- Display the cat's name and birthdate in the render method if the cat was found. If there is no cat that matches the name passed in to the component as a prop, we render text that says "Cat not found". +- Display the cat's name and birthdate in the render method if the cat was found. If there is no cat that matches the name passed into the component as a prop, we render text that says "Cat not found". .. tabs-realm-languages:: From bd7fa88eb7ea62bbb22277ad6f4b2710d3ae0e6c Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 19:45:25 -0500 Subject: [PATCH 12/19] reduce depth --- source/sdk/react-native/realm-database/schemas/mixed.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index 40fa6b50fc..9c3f72f83a 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -6,7 +6,7 @@ Mixed - React Native SDK .. contents:: On this page :local: :backlinks: none - :depth: 2 + :depth: 1 :class: singlecol .. versionadded:: 10.5.0 @@ -85,9 +85,9 @@ The ``CreateCatsInput`` component does the following: Query for Objects with a Mixed Value ------------------------------------ -To query for objects with a mixed value, run the +To query for objects with a mixed value, run the :js-sdk:`Collection.filtered() ` method and -passing in a :ref:`filter ` for a non-mixed field. You can +pass in a :ref:`filter ` for a non-mixed field. You can then print the value of the mixed property or the entire object itself. Example From 21732461b1ecec864ffabe956a7f39025ebc967a Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 19:46:54 -0500 Subject: [PATCH 13/19] remove redundant link --- source/sdk/react-native/realm-database/schemas/mixed.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index 9c3f72f83a..934b45db55 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -98,7 +98,7 @@ cat's name. The ``CatInfoCard`` component does the following: -- Get all ``Cat`` objects by passing the ``Cat`` class to the ``useQuery()`` hook, and then use :js-sdk:`filtered() ` to filter the results to receive only the cats whose name match the name passed as a prop. We then get the first matching cat and store it as a const variable. +- Get all ``Cat`` objects by passing the ``Cat`` class to the ``useQuery()`` hook, and then use ``filtered()`` to filter the results to receive only the cats whose name match the name passed as a prop. We then get the first matching cat and store it as a const variable. - Use dot notation to retrieve the mixed property, ``birthDate``. - Display the cat's name and birthdate in the render method if the cat was found. If there is no cat that matches the name passed into the component as a prop, we render text that says "Cat not found". From 9d2009edbaed9a19667f775c33c952b6860d9fa0 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 20:02:42 -0500 Subject: [PATCH 14/19] remove references to realm.create --- source/sdk/react-native/realm-database/schemas/mixed.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index 934b45db55..ee47eb8dcd 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -46,9 +46,9 @@ To :ref:`set a property of your object model Create an Object With a Mixed Value ----------------------------------- -Create an object with a mixed value by running the :js-sdk:`realm.create() -` method within a :ref:`write transaction -`. +Create an object with a mixed value by using the :mdn:`new +` operator within a :ref:`write +transaction `. Example ~~~~~~~ @@ -60,7 +60,7 @@ The ``CreateCatsInput`` component does the following: - Get access to the opened realm instance by calling the ``useRealm()`` hook. - Use React's `useEffect `__ hook to call an anonymous function only once by passing ``useEffect`` an `empty dependency array `__ as a second argument. -- Within the anonymous function, we create four different ``Cat`` objects by using the :mdn:`new ` operator to create a new realm object within a write transaction. Each of the ``Cat`` objects uses a different data type for the ``birthDate`` property. +- Within the anonymous function, we create four different ``Cat`` objects by using the new operator to create a new realm object within a write transaction. Each of the ``Cat`` objects uses a different data type for the ``birthDate`` property. - Use the ``useQuery()`` hook to retrieve all ``Cat`` objects. - `Map `__ through the cats to render a list of ``Text`` components displaying each cat's ``name`` and ``birthDate``. From 4716cf0296e02332012047e9aaaab36ac12bccc6 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Thu, 15 Dec 2022 20:24:04 -0500 Subject: [PATCH 15/19] improve wording --- .../sdk/react-native/realm-database/schemas/mixed.txt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index ee47eb8dcd..a17ccd67a6 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -13,9 +13,10 @@ Mixed - React Native SDK Overview -------- -The mixed data type is a realm property type that can hold any valid Realm data type except a collection. -You can create collections (lists, sets, and dictionaries) of type ``mixed``, but a ``mixed`` itself -cannot be a collection. Properties using the mixed data type can also hold null values. +The mixed data type is a realm property type that can hold any valid Realm data +type except a collection. You can create collections (lists, sets, and +dictionaries) of type ``mixed``, but a ``mixed`` type itself cannot be a +collection. Properties using the mixed data type can also hold null values. .. note:: @@ -98,9 +99,9 @@ cat's name. The ``CatInfoCard`` component does the following: -- Get all ``Cat`` objects by passing the ``Cat`` class to the ``useQuery()`` hook, and then use ``filtered()`` to filter the results to receive only the cats whose name match the name passed as a prop. We then get the first matching cat and store it as a const variable. +- Get all ``Cat`` objects by passing the ``Cat`` class to the ``useQuery()`` hook, and then use ``filtered()`` to filter the results to receive only the cats whose names match the name passed as a prop. We then get the first matching cat and store it as a const variable. - Use dot notation to retrieve the mixed property, ``birthDate``. -- Display the cat's name and birthdate in the render method if the cat was found. If there is no cat that matches the name passed into the component as a prop, we render text that says "Cat not found". +- Display the cat's name and birthdate in the render method if Realm finds the cat. If there is no cat that matches the name passed into the component as a prop, we render text that says "Cat not found". .. tabs-realm-languages:: From 4090a428e966014becc8b8024a049a7f7a6985d9 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Tue, 3 Jan 2023 16:19:41 -0500 Subject: [PATCH 16/19] fix line emphasis + added comment for what useEffect does --- .../__tests__/js/realm-database/schemas/mixed-tests.jsx | 1 + .../__tests__/ts/realm-database/schemas/mixed-test.tsx | 1 + .../js/mixed-tests.snippet.create-mixed-object.jsx | 1 + .../ts/mixed-test.snippet.create-mixed-object.tsx | 1 + source/sdk/react-native/realm-database/schemas/mixed.txt | 4 ++-- 5 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx index 0da22a1ce9..caabc6f958 100644 --- a/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx +++ b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx @@ -47,6 +47,7 @@ describe('Mixed Tests', () => { const realm = useRealm(); useEffect(() => { + // Add data to the Realm when the component mounts realm.write(() => { // create a Dog with a birthDate value of type string new Cat(realm, { diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx index 24592c5bc2..8ccdb5bf01 100644 --- a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -47,6 +47,7 @@ describe('Mixed Tests', () => { const realm = useRealm(); useEffect(() => { + // Add data to the Realm when the component mounts realm.write(() => { // create a Dog with a birthDate value of type string new Cat(realm, { diff --git a/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx b/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx index 5fadb496b7..30cc594afd 100644 --- a/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx +++ b/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx @@ -2,6 +2,7 @@ const CreateCatsInput = () => { const realm = useRealm(); useEffect(() => { + // Add data to the Realm when the component mounts realm.write(() => { // create a Dog with a birthDate value of type string new Cat(realm, { diff --git a/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx b/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx index 5fadb496b7..30cc594afd 100644 --- a/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx +++ b/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx @@ -2,6 +2,7 @@ const CreateCatsInput = () => { const realm = useRealm(); useEffect(() => { + // Add data to the Realm when the component mounts realm.write(() => { // create a Dog with a birthDate value of type string new Cat(realm, { diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index a17ccd67a6..54a91f491f 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -73,7 +73,7 @@ The ``CreateCatsInput`` component does the following: .. literalinclude:: /examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx :language: typescript - :emphasize-lines: 2, 5-21 + :emphasize-lines: 2, 6-23 :linenos: .. tab:: @@ -81,7 +81,7 @@ The ``CreateCatsInput`` component does the following: .. literalinclude:: /examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx :language: javascript - :emphasize-lines: 2, 5-21 + :emphasize-lines: 2, 6-23 :linenos: Query for Objects with a Mixed Value From 011d14770f79c7eae1e58e2d2331a573da3d2274 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Tue, 3 Jan 2023 16:40:37 -0500 Subject: [PATCH 17/19] address Pr comments --- .../__tests__/js/realm-database/schemas/mixed-tests.jsx | 5 ++++- .../__tests__/ts/realm-database/schemas/mixed-test.tsx | 5 ++++- examples/react-native/testSetup.js | 2 +- source/sdk/react-native/realm-database/schemas/mixed.txt | 9 ++++----- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx index caabc6f958..044c5e75ec 100644 --- a/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx +++ b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx @@ -97,7 +97,10 @@ describe('Mixed Tests', () => { 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 + // 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); diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx index 8ccdb5bf01..f2388ad9eb 100644 --- a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -97,7 +97,10 @@ describe('Mixed Tests', () => { 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 + // 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); diff --git a/examples/react-native/testSetup.js b/examples/react-native/testSetup.js index baec70af03..ff2f65729d 100644 --- a/examples/react-native/testSetup.js +++ b/examples/react-native/testSetup.js @@ -3,7 +3,7 @@ jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper'); // avoid erro jest.setTimeout(10000); global.console = { ...global.console, - // log: jest.fn(), + log: jest.fn(), error: jest.fn(), warn: jest.fn(), }; diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index 54a91f491f..13da529bbf 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -16,13 +16,12 @@ Overview The mixed data type is a realm property type that can hold any valid Realm data type except a collection. You can create collections (lists, sets, and dictionaries) of type ``mixed``, but a ``mixed`` type itself cannot be a -collection. Properties using the mixed data type can also hold null values. +collection. -.. note:: +The mixed data type is indexable, but you can't use it as a primary key. - The mixed data type is indexable, but you can't use it as a primary key. - Because null is a permitted value, you can't declare a Mixed property as - optional. +Properties using the mixed data type can also hold null values. You can't +declare a Mixed property as optional. Realm Object Models ------------------- From 5ee84bb76ca306f2aefedc71bca3c0eea901a7fe Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Tue, 3 Jan 2023 16:48:49 -0500 Subject: [PATCH 18/19] fix typo 'Dog' -> 'Cat' --- .../__tests__/js/realm-database/schemas/mixed-tests.jsx | 8 ++++---- .../__tests__/ts/realm-database/schemas/mixed-test.tsx | 8 ++++---- .../js/mixed-tests.snippet.create-mixed-object.jsx | 8 ++++---- .../ts/mixed-test.snippet.create-mixed-object.tsx | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx index 044c5e75ec..504b6d243d 100644 --- a/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx +++ b/examples/react-native/__tests__/js/realm-database/schemas/mixed-tests.jsx @@ -49,21 +49,21 @@ describe('Mixed Tests', () => { useEffect(() => { // Add data to the Realm when the component mounts realm.write(() => { - // create a Dog with a birthDate value of type string + // create a Cat with a birthDate value of type string new Cat(realm, { name: 'Euler', birthDate: 'December 25th, 2017', }); - // create a Dog with a birthDate value of type date + // create a Cat with a birthDate value of type date new Cat(realm, { name: 'Blaise', birthDate: new Date('August 17, 2020'), }); - // create a Dog with a birthDate value of type int + // create a Cat with a birthDate value of type int new Cat(realm, {name: 'Euclid', birthDate: 10152021}); - // create a Dog with a birthDate value of type null + // create a Cat with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); }, []); diff --git a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx index f2388ad9eb..4139a8a929 100644 --- a/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx +++ b/examples/react-native/__tests__/ts/realm-database/schemas/mixed-test.tsx @@ -49,21 +49,21 @@ describe('Mixed Tests', () => { useEffect(() => { // Add data to the Realm when the component mounts realm.write(() => { - // create a Dog with a birthDate value of type string + // create a Cat with a birthDate value of type string new Cat(realm, { name: 'Euler', birthDate: 'December 25th, 2017', }); - // create a Dog with a birthDate value of type date + // create a Cat with a birthDate value of type date new Cat(realm, { name: 'Blaise', birthDate: new Date('August 17, 2020'), }); - // create a Dog with a birthDate value of type int + // create a Cat with a birthDate value of type int new Cat(realm, {name: 'Euclid', birthDate: 10152021}); - // create a Dog with a birthDate value of type null + // create a Cat with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); }, []); diff --git a/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx b/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx index 30cc594afd..73f910be56 100644 --- a/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx +++ b/source/examples/generated/react-native/js/mixed-tests.snippet.create-mixed-object.jsx @@ -4,21 +4,21 @@ const CreateCatsInput = () => { useEffect(() => { // Add data to the Realm when the component mounts realm.write(() => { - // create a Dog with a birthDate value of type string + // create a Cat with a birthDate value of type string new Cat(realm, { name: 'Euler', birthDate: 'December 25th, 2017', }); - // create a Dog with a birthDate value of type date + // create a Cat with a birthDate value of type date new Cat(realm, { name: 'Blaise', birthDate: new Date('August 17, 2020'), }); - // create a Dog with a birthDate value of type int + // create a Cat with a birthDate value of type int new Cat(realm, {name: 'Euclid', birthDate: 10152021}); - // create a Dog with a birthDate value of type null + // create a Cat with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); }, []); diff --git a/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx b/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx index 30cc594afd..73f910be56 100644 --- a/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx +++ b/source/examples/generated/react-native/ts/mixed-test.snippet.create-mixed-object.tsx @@ -4,21 +4,21 @@ const CreateCatsInput = () => { useEffect(() => { // Add data to the Realm when the component mounts realm.write(() => { - // create a Dog with a birthDate value of type string + // create a Cat with a birthDate value of type string new Cat(realm, { name: 'Euler', birthDate: 'December 25th, 2017', }); - // create a Dog with a birthDate value of type date + // create a Cat with a birthDate value of type date new Cat(realm, { name: 'Blaise', birthDate: new Date('August 17, 2020'), }); - // create a Dog with a birthDate value of type int + // create a Cat with a birthDate value of type int new Cat(realm, {name: 'Euclid', birthDate: 10152021}); - // create a Dog with a birthDate value of type null + // create a Cat with a birthDate value of type null new Cat(realm, {name: 'Pythagoras', birthDate: null}); }); }, []); From 3c5f8076b42019a8f09a6d0c98744a2b0e7576ca Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Wed, 4 Jan 2023 11:44:39 -0500 Subject: [PATCH 19/19] Update source/sdk/react-native/realm-database/schemas/mixed.txt Co-authored-by: Ben Perlmutter <90647379+mongodben@users.noreply.github.com> --- source/sdk/react-native/realm-database/schemas/mixed.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/sdk/react-native/realm-database/schemas/mixed.txt b/source/sdk/react-native/realm-database/schemas/mixed.txt index 13da529bbf..659c1f5a82 100644 --- a/source/sdk/react-native/realm-database/schemas/mixed.txt +++ b/source/sdk/react-native/realm-database/schemas/mixed.txt @@ -60,7 +60,7 @@ The ``CreateCatsInput`` component does the following: - Get access to the opened realm instance by calling the ``useRealm()`` hook. - Use React's `useEffect `__ hook to call an anonymous function only once by passing ``useEffect`` an `empty dependency array `__ as a second argument. -- Within the anonymous function, we create four different ``Cat`` objects by using the new operator to create a new realm object within a write transaction. Each of the ``Cat`` objects uses a different data type for the ``birthDate`` property. +- Within the anonymous function, we create four different ``Cat`` objects by using the ``new`` operator to create a new realm object within a write transaction. Each of the ``Cat`` objects uses a different data type for the ``birthDate`` property. - Use the ``useQuery()`` hook to retrieve all ``Cat`` objects. - `Map `__ through the cats to render a list of ``Text`` components displaying each cat's ``name`` and ``birthDate``.