@@ -49,6 +49,12 @@ describe('DatabaseBuilder', () => {
49
49
expect ( resource ) . to . eq ( 'projects/_/instances/subdomain/refs/foo' ) ;
50
50
} ) ;
51
51
52
+ it ( 'should let developers choose a database instance' , ( ) => {
53
+ let func = database . instance ( 'custom' ) . ref ( 'foo' ) . onWrite ( ( ) => null ) ;
54
+ let resource = func . __trigger . eventTrigger . resource ;
55
+ expect ( resource ) . to . eq ( 'projects/_/instances/custom/refs/foo' ) ;
56
+ } ) ;
57
+
52
58
it ( 'should return a handler that emits events with a proper DeltaSnapshot' , ( ) => {
53
59
let handler = database . ref ( '/users/{id}' ) . onWrite ( event => {
54
60
expect ( event . data . val ( ) ) . to . deep . equal ( { foo : 'bar' } ) ;
@@ -75,6 +81,12 @@ describe('DatabaseBuilder', () => {
75
81
expect ( resource ) . to . eq ( 'projects/_/instances/subdomain/refs/foo' ) ;
76
82
} ) ;
77
83
84
+ it ( 'should let developers choose a database instance' , ( ) => {
85
+ let func = database . instance ( 'custom' ) . ref ( 'foo' ) . onCreate ( ( ) => null ) ;
86
+ let resource = func . __trigger . eventTrigger . resource ;
87
+ expect ( resource ) . to . eq ( 'projects/_/instances/custom/refs/foo' ) ;
88
+ } ) ;
89
+
78
90
it ( 'should return a handler that emits events with a proper DeltaSnapshot' , ( ) => {
79
91
let handler = database . ref ( '/users/{id}' ) . onCreate ( event => {
80
92
expect ( event . data . val ( ) ) . to . deep . equal ( { foo : 'bar' } ) ;
@@ -101,6 +113,12 @@ describe('DatabaseBuilder', () => {
101
113
expect ( resource ) . to . eq ( 'projects/_/instances/subdomain/refs/foo' ) ;
102
114
} ) ;
103
115
116
+ it ( 'should let developers choose a database instance' , ( ) => {
117
+ let func = database . instance ( 'custom' ) . ref ( 'foo' ) . onUpdate ( ( ) => null ) ;
118
+ let resource = func . __trigger . eventTrigger . resource ;
119
+ expect ( resource ) . to . eq ( 'projects/_/instances/custom/refs/foo' ) ;
120
+ } ) ;
121
+
104
122
it ( 'should return a handler that emits events with a proper DeltaSnapshot' , ( ) => {
105
123
let handler = database . ref ( '/users/{id}' ) . onUpdate ( event => {
106
124
expect ( event . data . val ( ) ) . to . deep . equal ( { foo : 'bar' } ) ;
@@ -127,6 +145,12 @@ describe('DatabaseBuilder', () => {
127
145
expect ( resource ) . to . eq ( 'projects/_/instances/subdomain/refs/foo' ) ;
128
146
} ) ;
129
147
148
+ it ( 'should let developers choose a database instance' , ( ) => {
149
+ let func = database . instance ( 'custom' ) . ref ( 'foo' ) . onDelete ( ( ) => null ) ;
150
+ let resource = func . __trigger . eventTrigger . resource ;
151
+ expect ( resource ) . to . eq ( 'projects/_/instances/custom/refs/foo' ) ;
152
+ } ) ;
153
+
130
154
it ( 'should return a handler that emits events with a proper DeltaSnapshot' , ( ) => {
131
155
let handler = database . ref ( '/users/{id}' ) . onDelete ( event => {
132
156
expect ( event . data . val ( ) ) . to . deep . equal ( { foo : 'bar' } ) ;
@@ -144,20 +168,44 @@ describe('DatabaseBuilder', () => {
144
168
145
169
} ) ;
146
170
171
+ describe ( 'resourceToInstanceAndPath' , ( ) => {
172
+ it ( 'should return the correct instance and path strings' , ( ) => {
173
+ let [ instance , path ] = database . resourceToInstanceAndPath ( 'projects/_/instances/foo/refs/bar' ) ;
174
+ expect ( instance ) . to . equal ( 'https://foo.firebaseio.com' ) ;
175
+ expect ( path ) . to . equal ( '/bar' ) ;
176
+ } ) ;
177
+ } ) ;
178
+
147
179
describe ( 'DeltaSnapshot' , ( ) => {
148
180
let subject ;
149
181
const apps = new appsNamespace . Apps ( fakeConfig ( ) ) ;
150
182
151
183
let populate = ( old : any , change : any ) => {
184
+ let [ instance , path ] = database . resourceToInstanceAndPath ( 'projects/_/instances/other-subdomain/refs/foo' ) ;
152
185
subject = new database . DeltaSnapshot (
153
186
apps . admin ,
154
187
apps . admin ,
188
+ instance ,
155
189
old ,
156
190
change ,
157
- database . resourceToPath ( 'projects/_/instances/mySubdomain/refs/foo' )
191
+ path
158
192
) ;
159
193
} ;
160
194
195
+ describe ( '#ref: firebase.database.Reference' , ( ) => {
196
+ it ( 'should return a ref for correct instance, not the default instance' , ( ) => {
197
+ populate ( { } , { } ) ;
198
+ expect ( subject . ref . toJSON ( ) ) . to . equal ( 'https://other-subdomain.firebaseio.com/foo' ) ;
199
+ } ) ;
200
+ } ) ;
201
+
202
+ describe ( '#adminRef(): firebase.database.Reference' , ( ) => {
203
+ it ( 'should return an adminRef for correct instance, not the default instance' , ( ) => {
204
+ populate ( { } , { } ) ;
205
+ expect ( subject . adminRef . toJSON ( ) ) . to . equal ( 'https://other-subdomain.firebaseio.com/foo' ) ;
206
+ } ) ;
207
+ } ) ;
208
+
161
209
describe ( '#val(): any' , ( ) => {
162
210
it ( 'should return child values based on the child path' , ( ) => {
163
211
populate ( { a : { b : 'c' } } , { a : { d : 'e' } } ) ;
@@ -372,23 +420,27 @@ describe('DeltaSnapshot', () => {
372
420
} ) ;
373
421
374
422
it ( 'should return null for the root' , ( ) => {
423
+ let [ instance , path ] = database . resourceToInstanceAndPath ( 'projects/_/instances/refs/refs' ) ;
375
424
const snapshot = new database . DeltaSnapshot (
376
425
apps . admin ,
377
426
apps . admin ,
427
+ instance ,
378
428
null ,
379
429
null ,
380
- database . resourceToPath ( 'projects/_/instances/foo/refs' )
430
+ path
381
431
) ;
382
432
expect ( snapshot . key ) . to . be . null ;
383
433
} ) ;
384
434
385
435
it ( 'should return null for explicit root' , ( ) => {
436
+ let [ instance , path ] = database . resourceToInstanceAndPath ( 'projects/_/instances/refs/refs' ) ;
386
437
expect ( new database . DeltaSnapshot (
387
438
apps . admin ,
388
439
apps . admin ,
440
+ instance ,
389
441
null ,
390
442
{ } ,
391
- database . resourceToPath ( 'projects/_/instances/foo/refs' )
443
+ path
392
444
) . key ) . to . be . null ;
393
445
} ) ;
394
446
0 commit comments