File tree Expand file tree Collapse file tree 3 files changed +17
-3
lines changed Expand file tree Collapse file tree 3 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,7 @@ export class Database implements FirebaseService {
102
102
* @param port the emulator port (ex: 8080)
103
103
*/
104
104
useEmulator ( host : string , port : number ) : void {
105
+ this . checkDeleted_ ( 'useEmulator' ) ;
105
106
if ( this . instanceStarted_ ) {
106
107
fatal (
107
108
'Cannot call useEmulator() after instance has already been initialized.'
@@ -156,14 +157,15 @@ export class Database implements FirebaseService {
156
157
validateUrl ( apiName , 1 , parsedURL ) ;
157
158
158
159
const repoInfo = parsedURL . repoInfo ;
159
- if ( repoInfo . host !== this . repo_ . repoInfo_ . host ) {
160
+ const expectedHost = this . repo_ . originalHost ;
161
+ if ( repoInfo . host !== expectedHost ) {
160
162
fatal (
161
163
apiName +
162
164
': Host name does not match the current database: ' +
163
165
'(found ' +
164
166
repoInfo . host +
165
167
' but expected ' +
166
- ( this . repo_ . repoInfo_ as RepoInfo ) . host +
168
+ expectedHost +
167
169
')'
168
170
) ;
169
171
}
@@ -175,7 +177,7 @@ export class Database implements FirebaseService {
175
177
* @param {string } apiName
176
178
*/
177
179
private checkDeleted_ ( apiName : string ) {
178
- if ( this . repo_ === null ) {
180
+ if ( this . repoInternal_ === null ) {
179
181
fatal ( 'Cannot call ' + apiName + ' on a deleted database.' ) ;
180
182
}
181
183
}
Original file line number Diff line number Diff line change @@ -57,6 +57,9 @@ export class Repo {
57
57
/** Key for uniquely identifying this repo, used in RepoManager */
58
58
readonly key : string ;
59
59
60
+ /** Record of the original host, which does not change even if useEmulator mutates the repo */
61
+ readonly originalHost : string ;
62
+
60
63
dataUpdateCount = 0 ;
61
64
private infoSyncTree_ : SyncTree ;
62
65
private serverSyncTree_ : SyncTree ;
@@ -90,6 +93,7 @@ export class Repo {
90
93
) {
91
94
// This key is intentionally not updated if RepoInfo is later changed or replaced
92
95
this . key = this . repoInfo_ . toURLString ( ) ;
96
+ this . originalHost = this . repoInfo_ . host ;
93
97
}
94
98
95
99
start ( ) : void {
Original file line number Diff line number Diff line change @@ -278,4 +278,12 @@ describe('Database Tests', () => {
278
278
db . useEmulator ( 'localhost' , 1234 ) ;
279
279
} ) . to . throw ( / C a n n o t c a l l u s e E m u l a t o r / ) ;
280
280
} ) ;
281
+
282
+ it ( 'refFromURL returns an emulated ref with useEmulator' , ( ) => {
283
+ const db = ( firebase as any ) . database ( ) ;
284
+ db . useEmulator ( 'localhost' , 1234 ) ;
285
+
286
+ const ref = db . refFromURL ( DATABASE_ADDRESS + '/path/to/data' ) ;
287
+ expect ( ref . toString ( ) ) . to . equal ( `http://localhost:1234/path/to/data` ) ;
288
+ } ) ;
281
289
} ) ;
You can’t perform that action at this time.
0 commit comments