Skip to content

Commit

Permalink
🔄 synced file(s) with circlefin/w3s-pw-web-sdk-internal (#40)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
superandydong authored Jan 3, 2025
2 parents 655fb0a + 35209fb commit 1a9ae3f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@circle-fin/w3s-pw-web-sdk",
"version": "1.1.8",
"version": "1.1.9",
"description": "Javascript/Typescript SDK for Circle Programmable Wallets",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
85 changes: 50 additions & 35 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ describe('W3SSdk > Apple OAuth', () => {
}
})()

Object.defineProperty(window, 'localStorage', { value: localStorageMock })

let sdk: W3SSdk
const configs: Configs = {
appSettings: {
Expand All @@ -159,20 +157,24 @@ describe('W3SSdk > Apple OAuth', () => {
}

beforeEach(() => {
jest.clearAllMocks()
jest.resetAllMocks()
Object.defineProperty(window, 'localStorage', { value: localStorageMock })
window.localStorage.clear()
})

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

// Simulate firebaseApp being initialized
const mockFirebaseApp = {}
Object.defineProperty(sdk, 'firebaseApp', {
get: jest.fn(() => mockFirebaseApp),
configurable: true,
})

sdk = new W3SSdk(configs, onLoginComplete)
})

it('should perform Apple login successfully', async () => {
// Mock signInWithPopup to resolve to a UserCredential
const userCredentialMock = {
user: { uid: 'test-uid' },
Expand Down Expand Up @@ -202,16 +204,6 @@ describe('W3SSdk > Apple OAuth', () => {
})

it('should handle signInWithPopup error during Apple login', async () => {
const onLoginComplete = jest.fn()
sdk = new W3SSdk(configs, onLoginComplete)

// Simulate firebaseApp being initialized
const mockFirebaseApp = {}
Object.defineProperty(sdk, 'firebaseApp', {
get: jest.fn(() => mockFirebaseApp),
configurable: true,
})

// Mock getAuth
const mockAuth = { getProvider: jest.fn() }
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)
Expand All @@ -232,16 +224,6 @@ describe('W3SSdk > Apple OAuth', () => {
})

it('should not handle signInWithPopup auth/cancelled-popup-request error during Apple login', async () => {
const onLoginComplete = jest.fn()
sdk = new W3SSdk(configs, onLoginComplete)

// Simulate firebaseApp being initialized
const mockFirebaseApp = {}
Object.defineProperty(sdk, 'firebaseApp', {
get: jest.fn(() => mockFirebaseApp),
configurable: true,
})

// Mock getAuth
const mockAuth = { getProvider: jest.fn() }
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)
Expand All @@ -265,16 +247,6 @@ describe('W3SSdk > Apple OAuth', () => {
})

it('should not handle signInWithPopup auth/popup-closed-by-user error during Apple login', async () => {
const onLoginComplete = jest.fn()
sdk = new W3SSdk(configs, onLoginComplete)

// Simulate firebaseApp being initialized
const mockFirebaseApp = {}
Object.defineProperty(sdk, 'firebaseApp', {
get: jest.fn(() => mockFirebaseApp),
configurable: true,
})

// Mock getAuth
const mockAuth = { getProvider: jest.fn() }
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)
Expand All @@ -296,4 +268,47 @@ describe('W3SSdk > Apple OAuth', () => {
expect(firebaseAuth.signInWithPopup).toHaveBeenCalled()
expect(handleLoginFailureSpy).toHaveBeenCalledTimes(0)
})

it('should handle other signInWithPopup Firebase errors during Apple login gracefully', async () => {
// Mock getAuth
const mockAuth = { getProvider: jest.fn() }
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)

// Mock signInWithPopup to reject
const error = new FirebaseError(
'auth/user-cancelled',
'Firebase: Error (auth/user-cancelled).',
)
;(firebaseAuth.signInWithPopup as jest.Mock).mockRejectedValueOnce(error)

// Mock handleLoginFailure
const handleLoginFailureSpy = jest
.spyOn(sdk as any, 'handleFirebaseFailure')
.mockImplementation(() => {})

await sdk.performLogin(SocialLoginProvider.APPLE)

expect(firebaseAuth.signInWithPopup).toHaveBeenCalled()
expect(handleLoginFailureSpy).toHaveBeenCalledWith(error)
})

it('should handle general errors during Apple login', async () => {
// Mock getAuth
const mockAuth = { getProvider: jest.fn() }
;(firebaseAuth.getAuth as jest.Mock).mockReturnValue(mockAuth)

// Mock signInWithPopup to reject
const error = new Error('general error')
;(firebaseAuth.signInWithPopup as jest.Mock).mockRejectedValueOnce(error)

// Mock handleLoginFailure
const handleLoginFailureSpy = jest
.spyOn(sdk as any, 'handleLoginFailure')
.mockImplementation(() => {})

await sdk.performLogin(SocialLoginProvider.APPLE)

expect(firebaseAuth.signInWithPopup).toHaveBeenCalled()
expect(handleLoginFailureSpy).toHaveBeenCalledTimes(1)
})
})
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,12 @@ export class W3SSdk {
this.window.localStorage.setItem('socialLoginProvider', '')
} catch (error) {
if (
(error instanceof FirebaseError &&
error.code !== 'auth/cancelled-popup-request' &&
error.code !== 'auth/popup-closed-by-user') ||
!(error instanceof FirebaseError)
error instanceof FirebaseError &&
error.code !== 'auth/cancelled-popup-request' &&
error.code !== 'auth/popup-closed-by-user'
) {
await this.handleFirebaseFailure(error)
} else if (!(error instanceof FirebaseError)) {
this.handleLoginFailure()
}
}
Expand Down Expand Up @@ -623,6 +624,16 @@ export class W3SSdk {
return false
}

private async handleFirebaseFailure(error: FirebaseError): Promise<void> {
await this.onLoginComplete?.(
{
code: -1,
message: error.message,
},
undefined,
)
}

private handleLoginFailure(): void {
void this.onLoginComplete?.(
{
Expand Down

0 comments on commit 1a9ae3f

Please sign in to comment.