@@ -33,7 +33,115 @@ const BusinessSchema = {
33
33
} ,
34
34
} ;
35
35
// :code-block-end:
36
+
36
37
describe ( "Node.js Data Types" , ( ) => {
38
+ test ( "should create, update and query Realm dictionaries" , async ( ) => {
39
+ // :code-block-start: define-dictionary-in-schema
40
+ const PersonSchema = {
41
+ name : "Person" ,
42
+ properties : {
43
+ name : "string" ,
44
+ home : "{}" ,
45
+ } ,
46
+ } ;
47
+ // :code-block-end:
48
+
49
+ const realm = await Realm . open ( {
50
+ schema : [ PersonSchema ] ,
51
+ } ) ;
52
+
53
+ // :code-block-start: create-realm-obj-with-dictionary
54
+ let johnDoe ;
55
+ let janeSmith ;
56
+ realm . write ( ( ) => {
57
+ johnDoe = realm . create ( "Person" , {
58
+ name : "John Doe" ,
59
+ home : {
60
+ windows : 5 ,
61
+ doors : 3 ,
62
+ color : "red" ,
63
+ address : "Summerhill St." ,
64
+ price : 400123 ,
65
+ } ,
66
+ } ) ;
67
+ janeSmith = realm . create ( "Person" , {
68
+ name : "Jane Smith" ,
69
+ home : {
70
+ address : "100 northroad st." ,
71
+ yearBuilt : 1990 ,
72
+ } ,
73
+ } ) ;
74
+ } ) ;
75
+ // :code-block-end:
76
+
77
+ // :code-block-start: query-a-dictionary
78
+ // query for all Person objects
79
+ const persons = realm . objects ( "Person" ) ;
80
+
81
+ // run the `.filtered()` method on all the returned persons to find the house with the address "Summerhill St."
82
+ const summerHillHouse = persons . filtered (
83
+ `home['address'] = "Summerhill St."`
84
+ ) [ 0 ] . home ;
85
+
86
+ // Find all people that have a house with a listed price
87
+ const peopleWithHousesWithAListedPrice = persons . filtered (
88
+ `home.@keys = "price" `
89
+ ) ;
90
+ // find a house that has any field with a value of 'red'
91
+ const redHouse = persons . filtered ( `home.@values = "red" ` ) [ 0 ] . home ;
92
+ // :code-block-end:
93
+
94
+ // the following assertion tests both creation of a dictionary + querying a dictionary
95
+ expect ( peopleWithHousesWithAListedPrice . length ) . toBe ( 1 ) ; // there should only be one house with a listed price
96
+ expect ( redHouse . doors ) . toBe ( 3 ) ; // the red house should have 3 doors
97
+
98
+ let dictionaryListenerHasBeenCalled = false ;
99
+ // :code-block-start: add-a-listener-to-a-dictionary
100
+ summerHillHouse . addListener ( ( changedHouse , changes ) => {
101
+ // :hide-start:
102
+ dictionaryListenerHasBeenCalled = true ;
103
+ // :hide-end:
104
+ console . log ( "A change has occurred to the Summer Hill House object" ) ;
105
+ } ) ;
106
+ // :code-block-end:
107
+
108
+ // :code-block-start: update-a-dictionary
109
+ realm . write ( ( ) => {
110
+ // use the `put()` method to update a field of a dictionary
111
+ summerHillHouse . put ( { price : 400100 } ) ;
112
+ // alternatively, update a field of a dictionary through dot notation
113
+ summerHillHouse . color = "brown" ;
114
+ // update a dictionary by adding a field
115
+ summerHillHouse . yearBuilt = 2004 ;
116
+ } ) ;
117
+ // :code-block-end:
118
+
119
+ expect ( dictionaryListenerHasBeenCalled ) . toBe ( true ) ; // a home (dictionary inside a realm object) should be able to have a change listener
120
+ expect ( summerHillHouse . price ) . toBe ( 400100 ) ; // the summerHillHouse should be $400,100 now
121
+ expect ( summerHillHouse . color ) . toBe ( "brown" ) ; // the summerHillHouse should be brown now
122
+ expect ( summerHillHouse . yearBuilt ) . toBe ( 2004 ) ; // the summerHillHouse should've been built in 2004
123
+
124
+ console . log ( summerHillHouse ) ;
125
+ // :code-block-start: remove-fields-of-the-dictionary
126
+ realm . write ( ( ) => {
127
+ // remove the 'windows' and 'doors' field of the Summerhill House.
128
+ // :uncomment-start:
129
+ // summerHillHouse.remove(["windows", "doors"]);
130
+ // :uncomment-end:
131
+ } ) ;
132
+ // :code-block-end:
133
+
134
+ // expect(summerHillHouse.windows).toBe(undefined); // since windows has been removed as a field, it should be undefined
135
+ // expect(summerHillHouse.doors).toBe(undefined); // since doors has been removed as a field, it should be undefined
136
+
137
+ // delete the objects to keep the test idempotent
138
+ realm . write ( ( ) => {
139
+ realm . delete ( johnDoe ) ;
140
+ realm . delete ( janeSmith ) ;
141
+ } ) ;
142
+ // close the realm to avoid memory leaks
143
+ realm . close ( ) ;
144
+ } ) ;
37
145
test ( "should work with Mixed Type" , async ( ) => {
38
146
// :code-block-start: define-mixed-in-schema
39
147
const DogSchema = {
@@ -92,6 +200,9 @@ describe("Node.js Data Types", () => {
92
200
realm . delete ( Euclid ) ;
93
201
realm . delete ( Pythagoras ) ;
94
202
} ) ;
203
+ // close the realm
204
+ realm . close ( ) ;
205
+ } ) ;
95
206
test ( "should create and read and delete an embedded object" , async ( ) => {
96
207
const realm = await Realm . open ( {
97
208
schema : [ AddressSchema , ContactSchema ] ,
@@ -131,7 +242,6 @@ describe("Node.js Data Types", () => {
131
242
) ;
132
243
} ) ;
133
244
// :code-block-end:
134
-
135
245
// close the realm
136
246
realm . close ( ) ;
137
247
} ) ;
@@ -182,5 +292,11 @@ describe("Node.js Data Types", () => {
182
292
// :code-block-end:
183
293
184
294
expect ( harryPotter . address . city ) . toBe ( "London" ) ;
295
+ // delete the object specifically created in this test to keep tests idempotent
296
+ realm . write ( ( ) => {
297
+ realm . delete ( harryPotter ) ;
298
+ } ) ;
299
+ // close the realm
300
+ realm . close ( ) ;
185
301
} ) ;
186
302
} ) ;
0 commit comments