Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔄 synced file(s) with circlefin/w3s-pw-web-sdk-internal #40

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading