|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect, use } from 'chai'; |
| 19 | +import * as sinonChai from 'sinon-chai'; |
| 20 | +import * as sinon from 'sinon'; |
| 21 | +import * as exp from '@firebase/auth-exp/internal'; |
| 22 | +import * as platform from './platform'; |
| 23 | +import { CompatPopupRedirectResolver } from './popup_redirect'; |
| 24 | +import { FirebaseApp } from '@firebase/app-compat'; |
| 25 | + |
| 26 | +use(sinonChai); |
| 27 | + |
| 28 | +describe('popup_redirect/CompatPopupRedirectResolver', () => { |
| 29 | + let compatResolver: CompatPopupRedirectResolver; |
| 30 | + let auth: exp.AuthImpl; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + compatResolver = new CompatPopupRedirectResolver(); |
| 34 | + const app = { options: { apiKey: 'api-key' } } as FirebaseApp; |
| 35 | + auth = new exp.AuthImpl(app, { |
| 36 | + apiKey: 'api-key' |
| 37 | + } as exp.Config); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + sinon.restore(); |
| 42 | + }); |
| 43 | + |
| 44 | + context('initialization and resolver selection', () => { |
| 45 | + const browserResolver = exp._getInstance<exp.PopupRedirectResolverInternal>( |
| 46 | + exp.browserPopupRedirectResolver |
| 47 | + ); |
| 48 | + const cordovaResolver = exp._getInstance<exp.PopupRedirectResolverInternal>( |
| 49 | + exp.cordovaPopupRedirectResolver |
| 50 | + ); |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + sinon.stub(browserResolver, '_initialize'); |
| 54 | + sinon.stub(cordovaResolver, '_initialize'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('selects the Cordova resolver if in Cordova', async () => { |
| 58 | + sinon.stub(platform, '_isCordova').returns(Promise.resolve(true)); |
| 59 | + await compatResolver._initialize(auth); |
| 60 | + expect(cordovaResolver._initialize).to.have.been.calledWith(auth); |
| 61 | + expect(browserResolver._initialize).not.to.have.been.called; |
| 62 | + }); |
| 63 | + |
| 64 | + it('selects the Browser resolver if in Browser', async () => { |
| 65 | + sinon.stub(platform, '_isCordova').returns(Promise.resolve(false)); |
| 66 | + await compatResolver._initialize(auth); |
| 67 | + expect(cordovaResolver._initialize).not.to.have.been.called; |
| 68 | + expect(browserResolver._initialize).to.have.been.calledWith(auth); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + context('callthrough methods', () => { |
| 73 | + let underlyingResolver: sinon.SinonStubbedInstance<exp.PopupRedirectResolverInternal>; |
| 74 | + let provider: exp.AuthProvider; |
| 75 | + |
| 76 | + beforeEach(() => { |
| 77 | + underlyingResolver = sinon.createStubInstance(FakeResolver); |
| 78 | + ((compatResolver as unknown) as { |
| 79 | + underlyingResolver: exp.PopupRedirectResolverInternal; |
| 80 | + }).underlyingResolver = underlyingResolver; |
| 81 | + provider = new exp.GoogleAuthProvider(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('_openPopup', async () => { |
| 85 | + await compatResolver._openPopup( |
| 86 | + auth, |
| 87 | + provider, |
| 88 | + exp.AuthEventType.LINK_VIA_POPUP, |
| 89 | + 'eventId' |
| 90 | + ); |
| 91 | + expect(underlyingResolver._openPopup).to.have.been.calledWith( |
| 92 | + auth, |
| 93 | + provider, |
| 94 | + exp.AuthEventType.LINK_VIA_POPUP, |
| 95 | + 'eventId' |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('_openRedirect', async () => { |
| 100 | + await compatResolver._openRedirect( |
| 101 | + auth, |
| 102 | + provider, |
| 103 | + exp.AuthEventType.LINK_VIA_REDIRECT, |
| 104 | + 'eventId' |
| 105 | + ); |
| 106 | + expect(underlyingResolver._openRedirect).to.have.been.calledWith( |
| 107 | + auth, |
| 108 | + provider, |
| 109 | + exp.AuthEventType.LINK_VIA_REDIRECT, |
| 110 | + 'eventId' |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('_isIframeWebStorageSupported', () => { |
| 115 | + const cb = (): void => {}; |
| 116 | + compatResolver._isIframeWebStorageSupported(auth, cb); |
| 117 | + expect( |
| 118 | + underlyingResolver._isIframeWebStorageSupported |
| 119 | + ).to.have.been.calledWith(auth, cb); |
| 120 | + }); |
| 121 | + |
| 122 | + it('_originValidation', async () => { |
| 123 | + await compatResolver._originValidation(auth); |
| 124 | + expect(underlyingResolver._originValidation).to.have.been.calledWith( |
| 125 | + auth |
| 126 | + ); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +class FakeResolver implements exp.PopupRedirectResolverInternal { |
| 132 | + _completeRedirectFn = async (): Promise<null> => null; |
| 133 | + _redirectPersistence = exp.inMemoryPersistence; |
| 134 | + |
| 135 | + _initialize(): Promise<exp.EventManager> { |
| 136 | + throw new Error('Method not implemented.'); |
| 137 | + } |
| 138 | + _openPopup(): Promise<exp.AuthPopup> { |
| 139 | + throw new Error('Method not implemented.'); |
| 140 | + } |
| 141 | + _openRedirect(): Promise<void> { |
| 142 | + throw new Error('Method not implemented.'); |
| 143 | + } |
| 144 | + _isIframeWebStorageSupported(): void { |
| 145 | + throw new Error('Method not implemented.'); |
| 146 | + } |
| 147 | + |
| 148 | + _originValidation(): Promise<void> { |
| 149 | + throw new Error('Method not implemented.'); |
| 150 | + } |
| 151 | +} |
0 commit comments