-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: tests for api client * feat: added test for environment service * feat: added payment methods test * feat: tests for payments service * feat: payments details tests * feat: added test for utils function * feat: added tests for error response function * feat: added tests for executing callbacks * feat: added tests for formating address and parsers * feat: lint * feat: changed package dependency to point to local pacakge * feat: added pre-push script * feat: added pre-push script * fix: remove token mock from constants * fix: exclude tests from codeql * fix: no need for output files * fix: codequl yml file * chore: upload artifacts * feat: basic codeql setup * fix: remove matrix language * chore: exclude scripts from testing * fix: remove unused variable
- Loading branch information
1 parent
40619e6
commit b34650d
Showing
20 changed files
with
580 additions
and
41 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
cd packages/adyen-salesforce-pwa | ||
npm run test |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
81 changes: 81 additions & 0 deletions
81
packages/adyen-salesforce-pwa/lib/services/tests/api.test.js
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,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' | ||
} | ||
}) | ||
) | ||
}) | ||
}) |
54 changes: 54 additions & 0 deletions
54
packages/adyen-salesforce-pwa/lib/services/tests/environment.test.js
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,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]') | ||
}) | ||
}) |
65 changes: 65 additions & 0 deletions
65
packages/adyen-salesforce-pwa/lib/services/tests/payment-methods.test.js
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,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]') | ||
}) | ||
}) |
67 changes: 67 additions & 0 deletions
67
packages/adyen-salesforce-pwa/lib/services/tests/payments-details.test.js
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,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]') | ||
}) | ||
}) |
Oops, something went wrong.