@@ -23,6 +23,7 @@ import * as sinon from 'sinon';
23
23
import sinonChai from 'sinon-chai' ;
24
24
import { Auth } from './auth' ;
25
25
import { CompatPopupRedirectResolver } from './popup_redirect' ;
26
+ import * as platform from './platform' ;
26
27
27
28
use ( sinonChai ) ;
28
29
@@ -45,7 +46,7 @@ describe('auth compat', () => {
45
46
} ) ;
46
47
47
48
afterEach ( ( ) => {
48
- sinon . restore ;
49
+ sinon . restore ( ) ;
49
50
} ) ;
50
51
51
52
it ( 'saves the persistence into session storage if available' , async ( ) => {
@@ -75,6 +76,36 @@ describe('auth compat', () => {
75
76
}
76
77
} ) ;
77
78
79
+ it ( 'does not save persistence if property throws DOMException' , async ( ) => {
80
+ if ( typeof self !== 'undefined' ) {
81
+ sinon . stub ( platform , '_getSelfWindow' ) . returns ( {
82
+ get sessionStorage ( ) : Storage {
83
+ throw new DOMException ( 'Nope!' ) ;
84
+ }
85
+ } as unknown as Window ) ;
86
+ const setItemSpy = sinon . spy ( sessionStorage , 'setItem' ) ;
87
+ sinon . stub ( underlyingAuth , '_getPersistence' ) . returns ( 'TEST' ) ;
88
+ sinon
89
+ . stub ( underlyingAuth , '_initializationPromise' )
90
+ . value ( Promise . resolve ( ) ) ;
91
+ sinon . stub (
92
+ exp . _getInstance < exp . PopupRedirectResolverInternal > (
93
+ CompatPopupRedirectResolver
94
+ ) ,
95
+ '_openRedirect'
96
+ ) ;
97
+ providerStub . isInitialized . returns ( true ) ;
98
+ providerStub . getImmediate . returns ( underlyingAuth ) ;
99
+ const authCompat = new Auth (
100
+ app ,
101
+ providerStub as unknown as Provider < 'auth' >
102
+ ) ;
103
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
104
+ await authCompat . signInWithRedirect ( new exp . GoogleAuthProvider ( ) ) ;
105
+ expect ( setItemSpy ) . not . to . have . been . calledWith ( 'firebase:persistence:api-key:undefined' , 'TEST' ) ;
106
+ }
107
+ } ) ;
108
+
78
109
it ( 'pulls the persistence and sets as the main persitsence if set' , ( ) => {
79
110
if ( typeof self !== 'undefined' ) {
80
111
sessionStorage . setItem (
@@ -98,5 +129,34 @@ describe('auth compat', () => {
98
129
} ) ;
99
130
}
100
131
} ) ;
132
+
133
+ it ( 'does not die if sessionStorage errors' , ( ) => {
134
+ if ( typeof self !== 'undefined' ) {
135
+ sinon . stub ( platform , '_getSelfWindow' ) . returns ( {
136
+ get sessionStorage ( ) : Storage {
137
+ throw new DOMException ( 'Nope!' ) ;
138
+ }
139
+ } as unknown as Window ) ;
140
+ sessionStorage . setItem (
141
+ 'firebase:persistence:api-key:undefined' ,
142
+ 'none'
143
+ ) ;
144
+ providerStub . isInitialized . returns ( false ) ;
145
+ providerStub . initialize . returns ( underlyingAuth ) ;
146
+ new Auth ( app , providerStub as unknown as Provider < 'auth' > ) ;
147
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
148
+ expect ( providerStub . initialize ) . to . have . been . calledWith ( {
149
+ options : {
150
+ popupRedirectResolver : CompatPopupRedirectResolver ,
151
+ persistence : [
152
+ exp . indexedDBLocalPersistence ,
153
+ exp . browserLocalPersistence ,
154
+ exp . browserSessionPersistence ,
155
+ exp . inMemoryPersistence ,
156
+ ]
157
+ }
158
+ } ) ;
159
+ }
160
+ } ) ;
101
161
} ) ;
102
162
} ) ;
0 commit comments