-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4331406
commit 7be420a
Showing
3 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
packages/lib/src/components/PayPalFastlane/FastlaneSDK.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { mockDeep, mock, mockReset } from 'jest-mock-extended'; | ||
import initializeFastlane from './initializeFastlane'; | ||
import { httpPost } from '../../core/Services/http'; | ||
import Script from '../../utils/Script'; | ||
import type { Fastlane, FastlaneProfile, FastlaneShipping } from './types'; | ||
|
||
const fastlaneMock = mockDeep<Fastlane>(); | ||
const fastlaneConstructorMock = jest.fn().mockResolvedValue(fastlaneMock); | ||
|
||
const mockScriptLoaded = jest.fn().mockImplementation(() => { | ||
window.paypal = {}; | ||
window.paypal.Fastlane = fastlaneConstructorMock; | ||
return Promise.resolve(); | ||
}); | ||
|
||
jest.mock('../../core/Services/http'); | ||
jest.mock('../../utils/Script', () => { | ||
return jest.fn().mockImplementation(() => { | ||
return { load: mockScriptLoaded }; | ||
}); | ||
}); | ||
|
||
const httpPostMock = (httpPost as jest.Mock).mockResolvedValue({ | ||
id: 'RANDOM-ID', | ||
clientId: 'CLIENT-ID', | ||
merchantId: 'XXXYYYZZZ', | ||
value: 'TOKEN-VALUE', | ||
expiresAt: '2024-11-01T13:34:01.804+00:00' | ||
}); | ||
|
||
describe('FastlaneSDK', () => { | ||
beforeEach(() => { | ||
mockReset(fastlaneMock); | ||
}); | ||
|
||
test('should initialize the Fastlane SDK', async () => { | ||
await initializeFastlane({ | ||
clientKey: 'test_xxx', | ||
environment: 'test' | ||
}); | ||
|
||
expect(fastlaneConstructorMock).toHaveBeenCalledTimes(1); | ||
expect(fastlaneConstructorMock).toHaveBeenCalledWith({}); | ||
expect(fastlaneMock.setLocale).toHaveBeenCalledWith('en_us'); | ||
expect(httpPostMock).toHaveBeenCalledWith({ | ||
loadingContext: 'https://checkoutshopper-test.adyen.com/checkoutshopper/', | ||
path: 'utility/v1/payPalFastlane/tokens?clientKey=test_xxx', | ||
errorLevel: 'fatal' | ||
}); | ||
expect(Script).toHaveBeenCalledWith( | ||
'https://www.paypal.com/sdk/js?client-id=CLIENT-ID&components=buttons,fastlane', | ||
'body', | ||
{}, | ||
{ sdkClientToken: 'TOKEN-VALUE' } | ||
); | ||
}); | ||
|
||
test('should return not_found if email is not recognized', async () => { | ||
fastlaneMock.identity.lookupCustomerByEmail.mockResolvedValue({ | ||
customerContextId: null | ||
}); | ||
|
||
const fastlane = await initializeFastlane({ | ||
clientKey: 'test_xxx', | ||
environment: 'test' | ||
}); | ||
|
||
const authResult = await fastlane.authenticate('[email protected]'); | ||
|
||
expect(authResult.authenticationState).toBe('not_found'); | ||
expect(authResult.profileData).toBeUndefined(); | ||
}); | ||
|
||
test('should authenticate the user with email', async () => { | ||
const customerContextId = 'customer-context-id'; | ||
const mockedFastlaneProfile = mock<FastlaneProfile>(); | ||
|
||
fastlaneMock.identity.lookupCustomerByEmail.mockResolvedValue({ | ||
customerContextId | ||
}); | ||
|
||
fastlaneMock.identity.triggerAuthenticationFlow.mockResolvedValue({ | ||
authenticationState: 'succeeded', | ||
profileData: mockedFastlaneProfile | ||
}); | ||
|
||
const fastlane = await initializeFastlane({ | ||
clientKey: 'test_xxx', | ||
environment: 'test' | ||
}); | ||
|
||
const authResult = await fastlane.authenticate('[email protected]'); | ||
|
||
expect(fastlaneMock.identity.lookupCustomerByEmail).toHaveBeenCalledWith('[email protected]'); | ||
expect(fastlaneMock.identity.triggerAuthenticationFlow).toHaveBeenCalledWith(customerContextId); | ||
expect(authResult.authenticationState).toBe('succeeded'); | ||
expect(authResult.profileData).toBeDefined(); | ||
}); | ||
|
||
test('should call Fastlane shipping address selector method', async () => { | ||
const customerContextId = 'customer-context-id'; | ||
const mockedFastlaneProfile = mock<FastlaneProfile>(); | ||
const mockedFastlaneShipping = mock<FastlaneShipping>(); | ||
|
||
const fastlane = await initializeFastlane({ | ||
clientKey: 'test_xxx', | ||
environment: 'test' | ||
}); | ||
|
||
fastlaneMock.profile.showShippingAddressSelector.mockResolvedValue({ | ||
selectionChanged: false, | ||
selectedAddress: mockedFastlaneShipping | ||
}); | ||
|
||
fastlaneMock.identity.lookupCustomerByEmail.mockResolvedValue({ | ||
customerContextId | ||
}); | ||
|
||
fastlaneMock.identity.triggerAuthenticationFlow.mockResolvedValue({ | ||
authenticationState: 'succeeded', | ||
profileData: mockedFastlaneProfile | ||
}); | ||
|
||
await fastlane.authenticate('[email protected]'); | ||
const addressSelectorResult = await fastlane.showShippingAddressSelector(); | ||
|
||
expect(fastlaneMock.profile.showShippingAddressSelector).toHaveBeenCalledTimes(1); | ||
expect(addressSelectorResult.selectionChanged).toBeFalsy(); | ||
}); | ||
|
||
test('should mount Fastlane watermark', async () => { | ||
const componentMock = { | ||
render: jest.fn() | ||
}; | ||
fastlaneMock.FastlaneWatermarkComponent.mockResolvedValue(componentMock); | ||
|
||
const fastlane = await initializeFastlane({ | ||
clientKey: 'test_xxx', | ||
environment: 'test' | ||
}); | ||
|
||
await fastlane.mountWatermark('.my-div'); | ||
|
||
expect(fastlaneMock.FastlaneWatermarkComponent).toHaveBeenCalledTimes(1); | ||
expect(componentMock.render).toHaveBeenCalledTimes(1); | ||
expect(componentMock.render).toHaveBeenCalledWith('.my-div'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters