@@ -49,6 +49,12 @@ describe('DatabaseBuilder', () => {
4949 expect ( resource ) . to . eq ( 'projects/_/instances/subdomain/refs/foo' ) ;
5050 } ) ;
5151
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+
5258 it ( 'should return a handler that emits events with a proper DeltaSnapshot' , ( ) => {
5359 let handler = database . ref ( '/users/{id}' ) . onWrite ( event => {
5460 expect ( event . data . val ( ) ) . to . deep . equal ( { foo : 'bar' } ) ;
@@ -75,6 +81,12 @@ describe('DatabaseBuilder', () => {
7581 expect ( resource ) . to . eq ( 'projects/_/instances/subdomain/refs/foo' ) ;
7682 } ) ;
7783
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+
7890 it ( 'should return a handler that emits events with a proper DeltaSnapshot' , ( ) => {
7991 let handler = database . ref ( '/users/{id}' ) . onCreate ( event => {
8092 expect ( event . data . val ( ) ) . to . deep . equal ( { foo : 'bar' } ) ;
@@ -101,6 +113,12 @@ describe('DatabaseBuilder', () => {
101113 expect ( resource ) . to . eq ( 'projects/_/instances/subdomain/refs/foo' ) ;
102114 } ) ;
103115
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+
104122 it ( 'should return a handler that emits events with a proper DeltaSnapshot' , ( ) => {
105123 let handler = database . ref ( '/users/{id}' ) . onUpdate ( event => {
106124 expect ( event . data . val ( ) ) . to . deep . equal ( { foo : 'bar' } ) ;
@@ -127,6 +145,12 @@ describe('DatabaseBuilder', () => {
127145 expect ( resource ) . to . eq ( 'projects/_/instances/subdomain/refs/foo' ) ;
128146 } ) ;
129147
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+
130154 it ( 'should return a handler that emits events with a proper DeltaSnapshot' , ( ) => {
131155 let handler = database . ref ( '/users/{id}' ) . onDelete ( event => {
132156 expect ( event . data . val ( ) ) . to . deep . equal ( { foo : 'bar' } ) ;
@@ -144,20 +168,44 @@ describe('DatabaseBuilder', () => {
144168
145169} ) ;
146170
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+
147179describe ( 'DeltaSnapshot' , ( ) => {
148180 let subject ;
149181 const apps = new appsNamespace . Apps ( fakeConfig ( ) ) ;
150182
151183 let populate = ( old : any , change : any ) => {
184+ let [ instance , path ] = database . resourceToInstanceAndPath ( 'projects/_/instances/other-subdomain/refs/foo' ) ;
152185 subject = new database . DeltaSnapshot (
153186 apps . admin ,
154187 apps . admin ,
188+ instance ,
155189 old ,
156190 change ,
157- database . resourceToPath ( 'projects/_/instances/mySubdomain/refs/foo' )
191+ path
158192 ) ;
159193 } ;
160194
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+
161209 describe ( '#val(): any' , ( ) => {
162210 it ( 'should return child values based on the child path' , ( ) => {
163211 populate ( { a : { b : 'c' } } , { a : { d : 'e' } } ) ;
@@ -372,23 +420,27 @@ describe('DeltaSnapshot', () => {
372420 } ) ;
373421
374422 it ( 'should return null for the root' , ( ) => {
423+ let [ instance , path ] = database . resourceToInstanceAndPath ( 'projects/_/instances/refs/refs' ) ;
375424 const snapshot = new database . DeltaSnapshot (
376425 apps . admin ,
377426 apps . admin ,
427+ instance ,
378428 null ,
379429 null ,
380- database . resourceToPath ( 'projects/_/instances/foo/refs' )
430+ path
381431 ) ;
382432 expect ( snapshot . key ) . to . be . null ;
383433 } ) ;
384434
385435 it ( 'should return null for explicit root' , ( ) => {
436+ let [ instance , path ] = database . resourceToInstanceAndPath ( 'projects/_/instances/refs/refs' ) ;
386437 expect ( new database . DeltaSnapshot (
387438 apps . admin ,
388439 apps . admin ,
440+ instance ,
389441 null ,
390442 { } ,
391- database . resourceToPath ( 'projects/_/instances/foo/refs' )
443+ path
392444 ) . key ) . to . be . null ;
393445 } ) ;
394446
0 commit comments