14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
- import { expect } from 'chai' ;
17
+ import { expect , use } from 'chai' ;
18
+ import { match , restore , SinonStub , stub } from 'sinon' ;
19
+ import sinonChai from 'sinon-chai' ;
18
20
import {
19
21
getDefaultEmulatorHost ,
20
22
getDefaultEmulatorHostnameAndPort
21
23
} from '../src/defaults' ;
22
- import { getGlobal } from '../src/environment' ;
24
+ import * as environment from '../src/environment' ;
25
+
26
+ use ( sinonChai ) ;
23
27
24
28
describe ( 'getDefaultEmulatorHost' , ( ) => {
25
29
after ( ( ) => {
26
- delete getGlobal ( ) . __FIREBASE_DEFAULTS__ ;
30
+ delete environment . getGlobal ( ) . __FIREBASE_DEFAULTS__ ;
27
31
} ) ;
28
32
29
33
context ( 'with no config' , ( ) => {
@@ -32,9 +36,54 @@ describe('getDefaultEmulatorHost', () => {
32
36
} ) ;
33
37
} ) ;
34
38
39
+ context ( 'with no config and process.env undefined' , ( ) => {
40
+ before ( ( ) => {
41
+ stub ( process , 'env' ) . value ( undefined ) ;
42
+ } ) ;
43
+ after ( ( ) => {
44
+ restore ( ) ;
45
+ } ) ;
46
+ it ( 'returns undefined and does not throw' , ( ) => {
47
+ expect ( getDefaultEmulatorHost ( 'firestore' ) ) . to . be . undefined ;
48
+ expect ( getDefaultEmulatorHost ( 'firestore' ) ) . to . not . throw ;
49
+ } ) ;
50
+ } ) ;
51
+
52
+ context ( 'with no config and no document or document.cookie throws' , ( ) => {
53
+ before ( ( ) => {
54
+ // In Node tests document will not exist
55
+ if ( typeof document !== 'undefined' ) {
56
+ stub ( document , 'cookie' ) . get ( ( ) => new Error ( 'aaaah' ) ) ;
57
+ }
58
+ } ) ;
59
+ after ( ( ) => {
60
+ restore ( ) ;
61
+ } ) ;
62
+ it ( 'returns undefined and does not throw' , ( ) => {
63
+ expect ( getDefaultEmulatorHost ( 'firestore' ) ) . to . be . undefined ;
64
+ expect ( getDefaultEmulatorHost ( 'firestore' ) ) . to . not . throw ;
65
+ } ) ;
66
+ } ) ;
67
+
68
+ context ( 'with no config and something unexpected throws' , ( ) => {
69
+ let consoleInfoStub : SinonStub ;
70
+ before ( ( ) => {
71
+ stub ( environment , 'getGlobal' ) . throws ( new Error ( 'getGlobal threw!' ) ) ;
72
+ consoleInfoStub = stub ( console , 'info' ) ;
73
+ } ) ;
74
+ after ( ( ) => {
75
+ delete process . env . __FIREBASE_DEFAULTS__ ;
76
+ restore ( ) ;
77
+ } ) ;
78
+ it ( 'returns undefined and calls console.info with the error' , ( ) => {
79
+ expect ( getDefaultEmulatorHost ( 'firestore' ) ) . to . be . undefined ;
80
+ expect ( consoleInfoStub ) . to . be . calledWith ( match ( 'getGlobal threw!' ) ) ;
81
+ } ) ;
82
+ } ) ;
83
+
35
84
context ( 'with global config not listing the emulator' , ( ) => {
36
85
before ( ( ) => {
37
- getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
86
+ environment . getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
38
87
emulatorHosts : {
39
88
/* no firestore */
40
89
database : '127.0.0.1:8080'
@@ -49,7 +98,7 @@ describe('getDefaultEmulatorHost', () => {
49
98
50
99
context ( 'with IPv4 hostname in global config' , ( ) => {
51
100
before ( ( ) => {
52
- getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
101
+ environment . getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
53
102
emulatorHosts : {
54
103
firestore : '127.0.0.1:8080'
55
104
}
@@ -63,7 +112,7 @@ describe('getDefaultEmulatorHost', () => {
63
112
64
113
context ( 'with quoted IPv6 hostname in global config' , ( ) => {
65
114
before ( ( ) => {
66
- getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
115
+ environment . getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
67
116
emulatorHosts : {
68
117
firestore : '[::1]:8080'
69
118
}
@@ -78,7 +127,7 @@ describe('getDefaultEmulatorHost', () => {
78
127
79
128
describe ( 'getDefaultEmulatorHostnameAndPort' , ( ) => {
80
129
after ( ( ) => {
81
- delete getGlobal ( ) . __FIREBASE_DEFAULTS__ ;
130
+ delete environment . getGlobal ( ) . __FIREBASE_DEFAULTS__ ;
82
131
} ) ;
83
132
84
133
context ( 'with no config' , ( ) => {
@@ -89,7 +138,7 @@ describe('getDefaultEmulatorHostnameAndPort', () => {
89
138
90
139
context ( 'with global config not listing the emulator' , ( ) => {
91
140
before ( ( ) => {
92
- getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
141
+ environment . getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
93
142
emulatorHosts : {
94
143
/* no firestore */
95
144
database : '127.0.0.1:8080'
@@ -104,7 +153,7 @@ describe('getDefaultEmulatorHostnameAndPort', () => {
104
153
105
154
context ( 'with IPv4 hostname in global config' , ( ) => {
106
155
before ( ( ) => {
107
- getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
156
+ environment . getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
108
157
emulatorHosts : {
109
158
firestore : '127.0.0.1:8080'
110
159
}
@@ -121,7 +170,7 @@ describe('getDefaultEmulatorHostnameAndPort', () => {
121
170
122
171
context ( 'with quoted IPv6 hostname in global config' , ( ) => {
123
172
before ( ( ) => {
124
- getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
173
+ environment . getGlobal ( ) . __FIREBASE_DEFAULTS__ = {
125
174
emulatorHosts : {
126
175
firestore : '[::1]:8080'
127
176
}
0 commit comments