diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index dca3686..b34e3f8 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -13,39 +13,13 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 - with: - # We must fetch at least the immediate parents so that if this is - # a pull request then we can checkout the head. - fetch-depth: 2 + uses: actions/checkout@v4 - # If this run was triggered by a pull request event, then checkout - # the head of the pull request instead of the merge commit. - - run: git checkout HEAD^2 - if: ${{ github.event_name == 'pull_request' }} - - # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL uses: github/codeql-action/init@v2 - # Override language selection by uncommenting this and choosing your languages - # with: - # languages: go, javascript, csharp, python, cpp, java - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild uses: github/codeql-action/autobuild@v2 - # ℹī¸ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl - - # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language - - #- run: | - # make bootstrap - # make release - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v2 \ No newline at end of file diff --git a/.husky/pre-push b/.husky/pre-push new file mode 100755 index 0000000..d5505aa --- /dev/null +++ b/.husky/pre-push @@ -0,0 +1,5 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +cd packages/adyen-salesforce-pwa +npm run test diff --git a/packages/adyen-retail-react-app/build/loadable-stats.json b/packages/adyen-retail-react-app/build/loadable-stats.json index c08936c..6fcf9c8 100644 --- a/packages/adyen-retail-react-app/build/loadable-stats.json +++ b/packages/adyen-retail-react-app/build/loadable-stats.json @@ -1,6 +1,6 @@ { "name": "client", - "hash": "84e140465b7193b7b54a", + "hash": "31d19a4495175b725991", "publicPath": "/mobify/bundle/development/", "outputPath": "/Users/aleksandarm/Repositories/adyen-salesforce-headless-commerce-pwa/packages/adyen-retail-react-app/build", "assetsByChunkName": { @@ -54,7 +54,7 @@ { "type": "asset", "name": "vendor.js", - "size": 8676138, + "size": 8692636, "emitted": false, "comparedForEmit": false, "cached": true, @@ -81,7 +81,7 @@ { "type": "asset", "name": "main.js", - "size": 1221022, + "size": 1221139, "emitted": false, "comparedForEmit": false, "cached": true, diff --git a/packages/adyen-retail-react-app/package-lock.json b/packages/adyen-retail-react-app/package-lock.json index 21c8a00..73beb73 100644 --- a/packages/adyen-retail-react-app/package-lock.json +++ b/packages/adyen-retail-react-app/package-lock.json @@ -1,16 +1,16 @@ { "name": "adyen-retail-react-app", - "version": "1.0.0-beta.2", + "version": "1.0.0-beta.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "adyen-retail-react-app", - "version": "1.0.0-beta.2", + "version": "1.0.0-beta.3", "hasInstallScript": true, "license": "See license in LICENSE", "dependencies": { - "@adyen/adyen-salesforce-pwa": "1.0.0-beta.2" + "@adyen/adyen-salesforce-pwa": "1.0.0-beta.3" }, "devDependencies": { "@salesforce/retail-react-app": "2.0.0", @@ -37,9 +37,9 @@ "integrity": "sha512-DA5a1C0gD/pLOvhv33YMrbf2FK3oUzwNl9oOJqE4XVjuEtt6XIakRcsd7eLiOSPkp1kTRQGICTA8cKra/vFbjw==" }, "node_modules/@adyen/adyen-salesforce-pwa": { - "version": "1.0.0-beta.2", - "resolved": "https://registry.npmjs.org/@adyen/adyen-salesforce-pwa/-/adyen-salesforce-pwa-1.0.0-beta.2.tgz", - "integrity": "sha512-qyI2uuRiII6haWheTDmvYtDakSZ1vqoO49OSoHRejoVeChxAiCA2aE2mK5/RXf6eIcOPXuuw4oiUheOyt8edmQ==", + "version": "1.0.0-beta.3", + "resolved": "https://registry.npmjs.org/@adyen/adyen-salesforce-pwa/-/adyen-salesforce-pwa-1.0.0-beta.3.tgz", + "integrity": "sha512-5c2cmfTWf6qXRIZ2wFJ2zWM/fNgKLfg8paT/GWUH5DJy3K/ZhaE9mgnOMAhBL5FL6M4weoWAcbOtzhyQea5kAg==", "dependencies": { "@adyen/adyen-web": "^5.51.0", "@adyen/api-library": "^14.3.0", diff --git a/packages/adyen-retail-react-app/package.json b/packages/adyen-retail-react-app/package.json index 0ea1673..243d9bd 100644 --- a/packages/adyen-retail-react-app/package.json +++ b/packages/adyen-retail-react-app/package.json @@ -17,7 +17,7 @@ "style-loader": "^3.3.3" }, "dependencies": { - "@adyen/adyen-salesforce-pwa": "1.0.0-beta.3" + "@adyen/adyen-salesforce-pwa": "file:../adyen-salesforce-pwa" }, "scripts": { "analyze-build": "cross-env MOBIFY_ANALYZE=true npm run build", diff --git a/packages/adyen-salesforce-pwa/jest.config.js b/packages/adyen-salesforce-pwa/jest.config.js index da0e0fa..27b3a2d 100644 --- a/packages/adyen-salesforce-pwa/jest.config.js +++ b/packages/adyen-salesforce-pwa/jest.config.js @@ -15,9 +15,10 @@ module.exports = { }, collectCoverageFrom: [ 'lib/**/*.{js,jsx,mjs,cjs}', + '!lib/scripts/**/*{js,jsx,mjs,cjs}', '!**/node_modules/**', '!**/dist/**', - '!**/mocks/**' + '!**/mocks/**', ], clearMocks: true, restoreMocks: true, diff --git a/packages/adyen-salesforce-pwa/lib/services/tests/api.test.js b/packages/adyen-salesforce-pwa/lib/services/tests/api.test.js new file mode 100644 index 0000000..c03f8e0 --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/services/tests/api.test.js @@ -0,0 +1,81 @@ +import {ApiClient} from '../api' +import {ADYEN_API_BASEPATH} from '../../../mocks/adyenApi/constants' + +describe('ApiClient', () => { + let apiClient + + beforeEach(() => { + apiClient = new ApiClient(ADYEN_API_BASEPATH, 'mockToken') + }) + + afterEach(() => { + jest.restoreAllMocks() + }) + + it('should construct ApiClient with url and token', () => { + expect(apiClient.url).toBe(ADYEN_API_BASEPATH) + expect(apiClient.token).toBe('mockToken') + }) + + it('should call fetch with correct parameters for GET request', async () => { + const mockResponse = {data: 'some data'} + const mockJsonPromise = Promise.resolve(mockResponse) + const mockFetchPromise = Promise.resolve({ + json: () => mockJsonPromise, + ok: true + }) + + jest.spyOn(global, 'fetch').mockImplementation(() => mockFetchPromise) + + const options = { + queryParams: {param: 'value'}, + headers: {customHeader: 'customValue'} + } + + await apiClient.get(options) + + expect(global.fetch).toHaveBeenCalledWith( + `${ADYEN_API_BASEPATH}?param=value`, + expect.objectContaining({ + method: 'get', + body: null, + headers: { + 'Content-Type': 'application/json', + authorization: `Bearer mockToken`, + customHeader: 'customValue' + } + }) + ) + }) + + it('should call fetch with correct parameters for POST request', async () => { + const mockResponse = {success: true} + const mockJsonPromise = Promise.resolve(mockResponse) + const mockFetchPromise = Promise.resolve({ + json: () => mockJsonPromise, + ok: true + }) + + jest.spyOn(global, 'fetch').mockImplementation(() => mockFetchPromise) + + const options = { + body: JSON.stringify({key: 'value'}), + headers: {customHeader: 'customValue'} + } + + await apiClient.post(options) + + expect(global.fetch).toHaveBeenCalledWith( + ADYEN_API_BASEPATH, + expect.objectContaining({ + method: 'post', + body: JSON.stringify({key: 'value'}), + headers: { + 'Content-Type': 'application/json', + authorization: `Bearer mockToken`, + customHeader: 'customValue' + } + }) + ) + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/services/tests/environment.test.js b/packages/adyen-salesforce-pwa/lib/services/tests/environment.test.js new file mode 100644 index 0000000..7533895 --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/services/tests/environment.test.js @@ -0,0 +1,54 @@ +import {AdyenEnvironmentService} from '../environment' +import {ApiClient} from '../api' + +jest.mock('../api', () => { + return { + ApiClient: jest.fn().mockImplementation(() => ({ + get: jest.fn() + })) + } +}) + +describe('AdyenEnvironmentService', () => { + let adyenService + let mockToken = 'mockToken' + + beforeEach(() => { + adyenService = new AdyenEnvironmentService(mockToken) + }) + + afterEach(() => { + jest.clearAllMocks() + }) + + it('should create an instance of AdyenEnvironmentService with ApiClient', () => { + expect(ApiClient).toHaveBeenCalledWith('/api/adyen/environment', mockToken) + }) + + it('should fetch environment successfully', async () => { + const mockResponse = {environmentData: 'some data'} + const mockJsonPromise = Promise.resolve(mockResponse) + const mockFetchPromise = Promise.resolve({ + json: () => mockJsonPromise, + status: 200 + }) + + adyenService.apiClient.get.mockResolvedValueOnce(mockFetchPromise) + + const environmentData = await adyenService.fetchEnvironment() + + expect(adyenService.apiClient.get).toHaveBeenCalled() + expect(environmentData).toEqual(mockResponse) + }) + + it('should throw an error when fetchEnvironment gets a status >= 300', async () => { + const mockFetchPromise = Promise.resolve({ + status: 400, + statusText: 'Bad Request' + }) + + adyenService.apiClient.get.mockResolvedValueOnce(mockFetchPromise) + + await expect(adyenService.fetchEnvironment()).rejects.toThrow('[object Object]') + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/services/tests/payment-methods.test.js b/packages/adyen-salesforce-pwa/lib/services/tests/payment-methods.test.js new file mode 100644 index 0000000..04a50e7 --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/services/tests/payment-methods.test.js @@ -0,0 +1,65 @@ +import {AdyenPaymentMethodsService} from '../payment-methods' +import {ApiClient} from '../api' +import {CUSTOMER_ID_MOCK, LOCALE_MOCK} from '../../../mocks/adyenApi/constants' + +jest.mock('../api', () => { + return { + ApiClient: jest.fn().mockImplementation(() => ({ + get: jest.fn() + })) + } +}) + +describe('AdyenPaymentMethodsService', () => { + let paymentMethodsService + let mockToken = 'mockToken' + let mockCustomerId = CUSTOMER_ID_MOCK + let mockLocale = {id: LOCALE_MOCK} + + beforeEach(() => { + paymentMethodsService = new AdyenPaymentMethodsService(mockToken) + }) + + afterEach(() => { + jest.clearAllMocks() + }) + + it('should create an instance of AdyenPaymentMethodsService with ApiClient', () => { + expect(ApiClient).toHaveBeenCalledWith('/api/adyen/paymentMethods', mockToken) + }) + + it('should fetch payment methods successfully', async () => { + const mockResponse = {paymentMethods: ['visa', 'paypal']} + const mockJsonPromise = Promise.resolve(mockResponse) + const mockFetchPromise = Promise.resolve({ + json: () => mockJsonPromise, + status: 200 + }) + + paymentMethodsService.apiClient.get.mockResolvedValueOnce(mockFetchPromise) + + const paymentMethods = await paymentMethodsService.fetchPaymentMethods( + mockCustomerId, + mockLocale + ) + + expect(paymentMethodsService.apiClient.get).toHaveBeenCalledWith({ + queryParams: {locale: mockLocale.id}, + headers: {customerid: mockCustomerId} + }) + expect(paymentMethods).toEqual(mockResponse) + }) + + it('should throw an error when fetchPaymentMethods gets a status >= 300', async () => { + const mockFetchPromise = Promise.resolve({ + status: 400, + statusText: 'Bad Request' + }) + + paymentMethodsService.apiClient.get.mockResolvedValueOnce(mockFetchPromise) + + await expect( + paymentMethodsService.fetchPaymentMethods(mockCustomerId, mockLocale) + ).rejects.toThrow('[object Object]') + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/services/tests/payments-details.test.js b/packages/adyen-salesforce-pwa/lib/services/tests/payments-details.test.js new file mode 100644 index 0000000..bf2337d --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/services/tests/payments-details.test.js @@ -0,0 +1,67 @@ +import {AdyenPaymentsDetailsService} from '../payments-details' +import {ApiClient} from '../api' + +jest.mock('../api', () => { + return { + ApiClient: jest.fn().mockImplementation(() => ({ + post: jest.fn() + })) + } +}) + +describe('AdyenPaymentsDetailsService', () => { + let paymentsDetailsService + let mockToken = 'mockTokenHere' + let mockData = {someData: 'mockData'} + let mockCustomerId = 'mockCustomerId' + + beforeEach(() => { + paymentsDetailsService = new AdyenPaymentsDetailsService(mockToken) + }) + + afterEach(() => { + jest.clearAllMocks() + }) + + it('should create an instance of AdyenPaymentsDetailsService with ApiClient', () => { + expect(paymentsDetailsService).toBeInstanceOf(AdyenPaymentsDetailsService) + expect(ApiClient).toHaveBeenCalledWith('/api/adyen/payments/details', mockToken) + }) + + it('should submit payment details successfully', async () => { + const mockResponse = {paymentDetailsResult: 'success'} + const mockJsonPromise = Promise.resolve(mockResponse) + const mockFetchPromise = Promise.resolve({ + json: () => mockJsonPromise, + status: 200 + }) + + paymentsDetailsService.apiClient.post.mockResolvedValueOnce(mockFetchPromise) + + const paymentDetailsResult = await paymentsDetailsService.submitPaymentsDetails( + mockData, + mockCustomerId + ) + + expect(paymentsDetailsService.apiClient.post).toHaveBeenCalledWith({ + body: JSON.stringify({data: mockData}), + headers: { + customerid: mockCustomerId + } + }) + expect(paymentDetailsResult).toEqual(mockResponse) + }) + + it('should throw an error when submitPaymentsDetails gets a status >= 300', async () => { + const mockFetchPromise = Promise.resolve({ + status: 400, + statusText: 'Bad Request' + }) + + paymentsDetailsService.apiClient.post.mockResolvedValueOnce(mockFetchPromise) + + await expect( + paymentsDetailsService.submitPaymentsDetails(mockData, mockCustomerId) + ).rejects.toThrow('[object Object]') + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/services/tests/payments.test.js b/packages/adyen-salesforce-pwa/lib/services/tests/payments.test.js new file mode 100644 index 0000000..3ccc2a8 --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/services/tests/payments.test.js @@ -0,0 +1,71 @@ +import {AdyenPaymentsService} from '../payments' +import {ApiClient} from '../api' +import {CUSTOMER_ID_MOCK} from '../../../mocks/adyenApi/constants' + +jest.mock('../api', () => { + return { + ApiClient: jest.fn().mockImplementation(() => ({ + post: jest.fn() + })) + } +}) + +describe('AdyenPaymentsService', () => { + let paymentsService + let mockToken = 'mockToken' + let mockAdyenStateData = {someData: 'mockData'} + let mockBasketId = 'mockBasketId' + let mockCustomerId = CUSTOMER_ID_MOCK + + beforeEach(() => { + paymentsService = new AdyenPaymentsService(mockToken) + }) + + afterEach(() => { + jest.clearAllMocks() + }) + + it('should create an instance of AdyenPaymentsService with ApiClient', () => { + expect(paymentsService).toBeInstanceOf(AdyenPaymentsService) + expect(ApiClient).toHaveBeenCalledWith('/api/adyen/payments', mockToken) + }) + + it('should submit payment successfully', async () => { + const mockResponse = {paymentResult: 'success'} + const mockJsonPromise = Promise.resolve(mockResponse) + const mockFetchPromise = Promise.resolve({ + json: () => mockJsonPromise, + status: 200 + }) + + paymentsService.apiClient.post.mockResolvedValueOnce(mockFetchPromise) + + const paymentResult = await paymentsService.submitPayment( + mockAdyenStateData, + mockBasketId, + mockCustomerId + ) + + expect(paymentsService.apiClient.post).toHaveBeenCalledWith({ + body: JSON.stringify({data: mockAdyenStateData}), + headers: { + customerid: mockCustomerId, + basketid: mockBasketId + } + }) + expect(paymentResult).toEqual(mockResponse) + }) + + it('should throw an error when submitPayment gets a status >= 300', async () => { + const mockFetchPromise = Promise.resolve({ + status: 400, + statusText: 'Bad Request' + }) + + paymentsService.apiClient.post.mockResolvedValueOnce(mockFetchPromise) + + await expect( + paymentsService.submitPayment(mockAdyenStateData, mockBasketId, mockCustomerId) + ).rejects.toThrow('[object Object]') + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/utils/tests/countryList.test.js b/packages/adyen-salesforce-pwa/lib/utils/tests/countryList.test.js new file mode 100644 index 0000000..6039ef8 --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/utils/tests/countryList.test.js @@ -0,0 +1,11 @@ +import countryList from '../countryList.mjs' + +describe('Country List', () => { + it('should export an array of countries', () => { + expect(Array.isArray(countryList)).toBe(true) + }) + + it('should contain a specific number of countries', () => { + expect(countryList).toHaveLength(244) + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/utils/tests/createCheckoutResponse.test.js b/packages/adyen-salesforce-pwa/lib/utils/tests/createCheckoutResponse.test.js new file mode 100644 index 0000000..56fbf38 --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/utils/tests/createCheckoutResponse.test.js @@ -0,0 +1,43 @@ +import {createCheckoutResponse} from '../createCheckoutResponse.mjs' +import {RESULT_CODES} from '../constants.mjs' + +describe('createCheckoutResponse', () => { + it('should return final response for AUTHORISED or RECEIVED resultCode', () => { + const response = { + resultCode: RESULT_CODES.AUTHORISED, + merchantReference: 'reference123' + } + + const checkoutResponse = createCheckoutResponse(response) + expect(checkoutResponse).toEqual({ + isFinal: true, + isSuccessful: true, + merchantReference: 'reference123' + }) + }) + + it('should return non-final response for REDIRECTSHOPPER resultCode', () => { + const response = { + resultCode: RESULT_CODES.REDIRECTSHOPPER, + action: 'redirectAction' + } + + const checkoutResponse = createCheckoutResponse(response) + expect(checkoutResponse).toEqual({ + isFinal: false, + action: 'redirectAction' + }) + }) + + it('should return default response for an unknown resultCode', () => { + const response = { + resultCode: 'UNKNOWN_RESULT_CODE' + } + + const checkoutResponse = createCheckoutResponse(response) + expect(checkoutResponse).toEqual({ + isFinal: true, + isSuccessful: false + }) + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/utils/tests/createErrorResponse.test.js b/packages/adyen-salesforce-pwa/lib/utils/tests/createErrorResponse.test.js new file mode 100644 index 0000000..bd2eedb --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/utils/tests/createErrorResponse.test.js @@ -0,0 +1,20 @@ +import {createErrorResponse} from '../createErrorResponse.mjs' + +describe('createErrorResponse', () => { + it('should create an error response with default message', () => { + const errorResponse = createErrorResponse() + expect(errorResponse).toEqual({ + error: true, + errorMessage: 'Technical error!' + }) + }) + + it('should create an error response with custom message', () => { + const customErrorMessage = 'Custom error message' + const errorResponse = createErrorResponse(customErrorMessage) + expect(errorResponse).toEqual({ + error: true, + errorMessage: customErrorMessage + }) + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/utils/tests/currencyList.test.js b/packages/adyen-salesforce-pwa/lib/utils/tests/currencyList.test.js new file mode 100644 index 0000000..cc925fa --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/utils/tests/currencyList.test.js @@ -0,0 +1,11 @@ +import currencyList from '../currencyList.mjs' + +describe('Country List', () => { + it('should export an array of countries', () => { + expect(Array.isArray(currencyList)).toBe(true) + }) + + it('should contain a specific number of countries', () => { + expect(currencyList).toHaveLength(139) + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/utils/tests/executeCallbacks.test.js b/packages/adyen-salesforce-pwa/lib/utils/tests/executeCallbacks.test.js new file mode 100644 index 0000000..e976fd8 --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/utils/tests/executeCallbacks.test.js @@ -0,0 +1,32 @@ +import {executeCallbacks} from '../executeCallbacks' + +describe('executeCallbacks', () => { + it('should execute all callbacks successfully and return aggregated data', async () => { + const callback1 = jest.fn().mockResolvedValue({data1: 'result1'}) + const callback2 = jest.fn().mockResolvedValue({data2: 'result2'}) + const callbacks = [callback1, callback2] + const props = {prop1: 'value1'} + const onError = jest.fn() + + const execute = executeCallbacks(callbacks, props, onError) + await execute('param1', 'param2') + + expect(callback1).toHaveBeenCalledWith('param1', 'param2', props, {}) + expect(onError).not.toHaveBeenCalled() + }) + + it('should stop executing callbacks on error and call onError callback', async () => { + const callback1 = jest.fn().mockResolvedValue({data1: 'result1'}) + const callback2 = jest.fn().mockRejectedValue(new Error('Some error')) + const callback3 = jest.fn() + const callbacks = [callback1, callback2, callback3] + const props = {prop1: 'value1'} + const onError = jest.fn() + + const execute = executeCallbacks(callbacks, props, onError) + await execute('param1', 'param2') + + expect(callback1).toHaveBeenCalledWith('param1', 'param2', props, {}) + expect(callback3).not.toHaveBeenCalled() + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/utils/tests/formatAddress.test.js b/packages/adyen-salesforce-pwa/lib/utils/tests/formatAddress.test.js new file mode 100644 index 0000000..e00ce7e --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/utils/tests/formatAddress.test.js @@ -0,0 +1,65 @@ +import {formatAddressInAdyenFormat} from '../formatAddress.mjs' + +describe('formatAddressInAdyenFormat', () => { + it('should format address in Adyen format with provided address object', () => { + const address = { + city: 'New York', + countryCode: 'US', + address2: 'Apt 123', + postalCode: '10001', + stateCode: 'NY', + address1: '123 Main St' + } + + const formattedAddress = formatAddressInAdyenFormat(address) + + expect(formattedAddress).toEqual({ + city: 'New York', + country: 'US', + houseNumberOrName: 'Apt 123', + postalCode: '10001', + stateOrProvince: 'NY', + street: '123 Main St' + }) + }) + + it('should format address in Adyen format with missing or empty fields', () => { + const address = { + city: 'Los Angeles' + } + + const formattedAddress = formatAddressInAdyenFormat(address) + + expect(formattedAddress).toEqual({ + city: 'Los Angeles', + country: '', + houseNumberOrName: '', + postalCode: '', + stateOrProvince: '', + street: '' + }) + }) + + it('should return empty fields when provided address object is null or undefined', () => { + const formattedAddressNull = formatAddressInAdyenFormat(null) + const formattedAddressUndefined = formatAddressInAdyenFormat(undefined) + + expect(formattedAddressNull).toEqual({ + city: '', + country: '', + houseNumberOrName: '', + postalCode: '', + stateOrProvince: '', + street: '' + }) + + expect(formattedAddressUndefined).toEqual({ + city: '', + country: '', + houseNumberOrName: '', + postalCode: '', + stateOrProvince: '', + street: '' + }) + }) +}) diff --git a/packages/adyen-salesforce-pwa/lib/utils/tests/parsers.test.js b/packages/adyen-salesforce-pwa/lib/utils/tests/parsers.test.js new file mode 100644 index 0000000..d1d680a --- /dev/null +++ b/packages/adyen-salesforce-pwa/lib/utils/tests/parsers.test.js @@ -0,0 +1,35 @@ +import {getCurrencyValueForApi} from '../parsers.mjs' + +describe('getCurrencyValueForApi', () => { + it('should correctly convert amount to API currency value for a valid currency', () => { + const amount = 10 + const currencyCode = 'USD' + const expectedValue = 1000 + const result = getCurrencyValueForApi(amount, currencyCode) + expect(result).toBe(expectedValue) + }) + + it('should throw an error for an invalid currency code', () => { + const amount = 20 + const currencyCode = 'INVALID' + expect(() => { + getCurrencyValueForApi(amount, currencyCode) + }).toThrow('invalid currency!') + }) + + it('should handle decimal conversion for different currency decimals', () => { + const amount = 15 + const currencyCode = 'EUR' + const expectedValue = 1500 + const result = getCurrencyValueForApi(amount, currencyCode) + expect(result).toBe(expectedValue) + }) + + it('should handle zero amount correctly', () => { + const amount = 0 + const currencyCode = 'USD' + const expectedValue = 0 + const result = getCurrencyValueForApi(amount, currencyCode) + expect(result).toBe(expectedValue) + }) +}) diff --git a/packages/adyen-salesforce-pwa/mocks/adyenApi/constants.js b/packages/adyen-salesforce-pwa/mocks/adyenApi/constants.js index 6ab1708..7c22454 100644 --- a/packages/adyen-salesforce-pwa/mocks/adyenApi/constants.js +++ b/packages/adyen-salesforce-pwa/mocks/adyenApi/constants.js @@ -1 +1,5 @@ export const ADYEN_API_BASEPATH = 'https://checkout-test.adyen.com/v70' + +export const CUSTOMER_ID_MOCK = 'customerIdMock' + +export const LOCALE_MOCK = 'en-EN' diff --git a/packages/adyen-salesforce-pwa/package-lock.json b/packages/adyen-salesforce-pwa/package-lock.json index ffc0996..491df91 100644 --- a/packages/adyen-salesforce-pwa/package-lock.json +++ b/packages/adyen-salesforce-pwa/package-lock.json @@ -1,12 +1,12 @@ { "name": "@adyen/adyen-salesforce-pwa", - "version": "1.0.0-beta.2", + "version": "1.0.0-beta.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@adyen/adyen-salesforce-pwa", - "version": "1.0.0-beta.2", + "version": "1.0.0-beta.3", "license": "MIT", "dependencies": { "@adyen/adyen-web": "^5.51.0",