Skip to content

Commit 1a9ae3f

Browse files
🔄 synced file(s) with circlefin/w3s-pw-web-sdk-internal (#40)
synced local file(s) with [circlefin/w3s-pw-web-sdk-internal](https://github.com/circlefin/w3s-pw-web-sdk-internal). <details> <summary>Changed files</summary> <ul> <li>synced local <code>package.json</code> with remote <code>package.json</code></li><li>synced local directory <code>src/</code> with remote directory <code>src/</code></li> </ul> </details> --- This PR was created automatically by the [repo-file-sync-action](https://github.com/BetaHuhn/repo-file-sync-action) workflow run [#12592116726](https://github.com/circlefin/w3s-pw-web-sdk-internal/actions/runs/12592116726)
2 parents 655fb0a + 35209fb commit 1a9ae3f

File tree

3 files changed

+66
-40
lines changed

3 files changed

+66
-40
lines changed

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@circle-fin/w3s-pw-web-sdk",
3-
"version": "1.1.8",
3+
"version": "1.1.9",
44
"description": "Javascript/Typescript SDK for Circle Programmable Wallets",
55
"main": "dist/src/index.js",
66
"types": "dist/src/index.d.ts",

‎src/index.test.ts‎

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ describe('W3SSdk > Apple OAuth', () => {
137137
}
138138
})()
139139

140-
Object.defineProperty(window, 'localStorage', { value: localStorageMock })
141-
142140
let sdk: W3SSdk
143141
const configs: Configs = {
144142
appSettings: {
@@ -159,20 +157,24 @@ describe('W3SSdk > Apple OAuth', () => {
159157
}
160158

161159
beforeEach(() => {
162-
jest.clearAllMocks()
160+
jest.resetAllMocks()
161+
Object.defineProperty(window, 'localStorage', { value: localStorageMock })
163162
window.localStorage.clear()
164-
})
165163

166-
it('should perform Apple login successfully', async () => {
167164
const onLoginComplete = jest.fn()
168165
sdk = new W3SSdk(configs, onLoginComplete)
169166

167+
// Simulate firebaseApp being initialized
170168
const mockFirebaseApp = {}
171169
Object.defineProperty(sdk, 'firebaseApp', {
172170
get: jest.fn(() => mockFirebaseApp),
173171
configurable: true,
174172
})
175173

174+
sdk = new W3SSdk(configs, onLoginComplete)
175+
})
176+
177+
it('should perform Apple login successfully', async () => {
176178
// Mock signInWithPopup to resolve to a UserCredential
177179
const userCredentialMock = {
178180
user: { uid: 'test-uid' },
@@ -202,16 +204,6 @@ describe('W3SSdk > Apple OAuth', () => {
202204
})
203205

204206
it('should handle signInWithPopup error during Apple login', async () => {
205-
const onLoginComplete = jest.fn()
206-
sdk = new W3SSdk(configs, onLoginComplete)
207-
208-
// Simulate firebaseApp being initialized
209-
const mockFirebaseApp = {}
210-
Object.defineProperty(sdk, 'firebaseApp', {
211-
get: jest.fn(() => mockFirebaseApp),
212-
configurable: true,
213-
})
214-
215207
// Mock getAuth
216208
const mockAuth = { getProvider: jest.fn() }
217209
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)
@@ -232,16 +224,6 @@ describe('W3SSdk > Apple OAuth', () => {
232224
})
233225

234226
it('should not handle signInWithPopup auth/cancelled-popup-request error during Apple login', async () => {
235-
const onLoginComplete = jest.fn()
236-
sdk = new W3SSdk(configs, onLoginComplete)
237-
238-
// Simulate firebaseApp being initialized
239-
const mockFirebaseApp = {}
240-
Object.defineProperty(sdk, 'firebaseApp', {
241-
get: jest.fn(() => mockFirebaseApp),
242-
configurable: true,
243-
})
244-
245227
// Mock getAuth
246228
const mockAuth = { getProvider: jest.fn() }
247229
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)
@@ -265,16 +247,6 @@ describe('W3SSdk > Apple OAuth', () => {
265247
})
266248

267249
it('should not handle signInWithPopup auth/popup-closed-by-user error during Apple login', async () => {
268-
const onLoginComplete = jest.fn()
269-
sdk = new W3SSdk(configs, onLoginComplete)
270-
271-
// Simulate firebaseApp being initialized
272-
const mockFirebaseApp = {}
273-
Object.defineProperty(sdk, 'firebaseApp', {
274-
get: jest.fn(() => mockFirebaseApp),
275-
configurable: true,
276-
})
277-
278250
// Mock getAuth
279251
const mockAuth = { getProvider: jest.fn() }
280252
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)
@@ -296,4 +268,47 @@ describe('W3SSdk > Apple OAuth', () => {
296268
expect(firebaseAuth.signInWithPopup).toHaveBeenCalled()
297269
expect(handleLoginFailureSpy).toHaveBeenCalledTimes(0)
298270
})
271+
272+
it('should handle other signInWithPopup Firebase errors during Apple login gracefully', async () => {
273+
// Mock getAuth
274+
const mockAuth = { getProvider: jest.fn() }
275+
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)
276+
277+
// Mock signInWithPopup to reject
278+
const error = new FirebaseError(
279+
'auth/user-cancelled',
280+
'Firebase: Error (auth/user-cancelled).',
281+
)
282+
;(firebaseAuth.signInWithPopup as jest.Mock).mockRejectedValueOnce(error)
283+
284+
// Mock handleLoginFailure
285+
const handleLoginFailureSpy = jest
286+
.spyOn(sdk as any, 'handleFirebaseFailure')
287+
.mockImplementation(() => {})
288+
289+
await sdk.performLogin(SocialLoginProvider.APPLE)
290+
291+
expect(firebaseAuth.signInWithPopup).toHaveBeenCalled()
292+
expect(handleLoginFailureSpy).toHaveBeenCalledWith(error)
293+
})
294+
295+
it('should handle general errors during Apple login', async () => {
296+
// Mock getAuth
297+
const mockAuth = { getProvider: jest.fn() }
298+
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)
299+
300+
// Mock signInWithPopup to reject
301+
const error = new Error('general error')
302+
;(firebaseAuth.signInWithPopup as jest.Mock).mockRejectedValueOnce(error)
303+
304+
// Mock handleLoginFailure
305+
const handleLoginFailureSpy = jest
306+
.spyOn(sdk as any, 'handleLoginFailure')
307+
.mockImplementation(() => {})
308+
309+
await sdk.performLogin(SocialLoginProvider.APPLE)
310+
311+
expect(firebaseAuth.signInWithPopup).toHaveBeenCalled()
312+
expect(handleLoginFailureSpy).toHaveBeenCalledTimes(1)
313+
})
299314
})

‎src/index.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,12 @@ export class W3SSdk {
410410
this.window.localStorage.setItem('socialLoginProvider', '')
411411
} catch (error) {
412412
if (
413-
(error instanceof FirebaseError &&
414-
error.code !== 'auth/cancelled-popup-request' &&
415-
error.code !== 'auth/popup-closed-by-user') ||
416-
!(error instanceof FirebaseError)
413+
error instanceof FirebaseError &&
414+
error.code !== 'auth/cancelled-popup-request' &&
415+
error.code !== 'auth/popup-closed-by-user'
417416
) {
417+
await this.handleFirebaseFailure(error)
418+
} else if (!(error instanceof FirebaseError)) {
418419
this.handleLoginFailure()
419420
}
420421
}
@@ -623,6 +624,16 @@ export class W3SSdk {
623624
return false
624625
}
625626

627+
private async handleFirebaseFailure(error: FirebaseError): Promise<void> {
628+
await this.onLoginComplete?.(
629+
{
630+
code: -1,
631+
message: error.message,
632+
},
633+
undefined,
634+
)
635+
}
636+
626637
private handleLoginFailure(): void {
627638
void this.onLoginComplete?.(
628639
{

0 commit comments

Comments
 (0)