Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihajlovski committed Nov 14, 2024
1 parent 40e62e8 commit 52951b4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -452,25 +452,25 @@ describe('selectShippingMethod', () => {
});

it('should send correct request to fetch with valid parameters', async () => {
// fetch.mockResolvedValue(mockResponse);
const result = await selectShippingMethod(
await selectShippingMethod(
{ shipmentUUID: mockShipmentUUID, ID: mockID },
mockBasketId,
reject
);
expect(fetch).toHaveBeenCalledWith(window.selectShippingMethodUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
expect(global.$.ajax).toHaveBeenCalledWith({
url: window.selectShippingMethodUrl,
type: 'POST',
data: {
csrf_token: undefined,
data: JSON.stringify({
paymentMethodType: APPLE_PAY,
shipmentUUID: mockShipmentUUID,
methodID: mockID,
basketId: mockBasketId,
})
},
body: JSON.stringify({
paymentMethodType: APPLE_PAY,
shipmentUUID: mockShipmentUUID,
methodID: mockID,
basketId: mockBasketId,
}),
success: expect.any(Function),
});
expect(result).toEqual(mockResponse);
});

it('should handle fetch rejection', async () => {
Expand Down Expand Up @@ -507,17 +507,13 @@ describe('getShippingMethod', () => {
administrativeArea: 'Test State',
postalCode: '12345',
};
fetch.mockResolvedValue(mockResponse);
const result = await getShippingMethod(shippingContact, mockBasketId, rejectMock);
expect(global.$.ajax).toHaveBeenCalledWith({
await getShippingMethod(shippingContact, mockBasketId, rejectMock);
expect(global.$.ajax).toHaveBeenCalledWith(expect.objectContaining({
url: window.shippingMethodsUrl,
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
type: 'POST',
data: {
csrf_token: undefined,
data: {
data: JSON.stringify({
paymentMethodType: APPLE_PAY,
basketId: mockBasketId,
address: {
Expand All @@ -526,11 +522,10 @@ describe('getShippingMethod', () => {
countryCode: shippingContact.countryCode,
stateCode: shippingContact.administrativeArea,
postalCode: shippingContact.postalCode,
},
}
}
});
expect(result).toEqual(mockResponse);
}
})
},
}));
});

it('should send correct request to fetch without shippingContact', async () => {
Expand All @@ -547,7 +542,6 @@ describe('getShippingMethod', () => {
})
},
});
expect(result).toEqual(mockResponse);
});

it('should handle fetch rejection', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,29 +172,28 @@ describe('paypal express', () => {
updatePaymentData: jest.fn(),
paymentData: 'test_paymentData'
}
global.fetch = jest.fn().mockResolvedValueOnce({
ok: true,
json: jest.fn(() => ({paymentData: 'test_paymentData', status: 'success'}))
})

const request = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({ paymentMethodType: 'paypal', currentPaymentData: 'test_paymentData', address: {
city: 'Amsterdam',
country: 'Netherlands',
countryCode: 'NL',
stateCode: 'AMS',
postalCode: '1001',
} }),
url: 'test_url',
type: 'POST',
data: {
csrf_token: undefined,
data: JSON.stringify({
paymentMethodType: 'paypal',
currentPaymentData: 'test_paymentData',
address: {
city: 'Amsterdam',
country: 'Netherlands',
countryCode: 'NL',
stateCode: 'AMS',
postalCode: '1001',
}
}),
},
async: false,
}

await handleShippingAddressChange(data, actions, component);
expect(global.$.ajax).toHaveBeenCalledWith('test_url', request);
expect(component.updatePaymentData).toHaveBeenCalledTimes(1);
expect(actions.reject).not.toHaveBeenCalled();
expect(global.$.ajax).toHaveBeenCalledWith(expect.objectContaining(request));
})
it('should not make shipping address change call if no shipping address is present', async () => {
const data = {
Expand Down Expand Up @@ -357,10 +356,6 @@ describe('paypal express', () => {
updatePaymentData: jest.fn(),
paymentData: 'test_paymentData'
}
global.fetch = jest.fn().mockResolvedValueOnce({
ok: true,
json: jest.fn(() => ({paymentData: 'test_paymentData', status: 'success'}))
})

const request = {
url: 'test_url',
Expand All @@ -370,18 +365,16 @@ describe('paypal express', () => {
error: expect.any(Function),
data: {
csrf_token: undefined,
data: {
data: JSON.stringify({
paymentMethodType: 'paypal',
currentPaymentData: 'test_paymentData',
methodID: 'test'
}
})
},
}

await handleShippingOptionChange(data, actions, component);
expect(global.$.ajax).toHaveBeenCalledWith(request);
expect(component.updatePaymentData).toHaveBeenCalledTimes(1);
expect(actions.reject).not.toHaveBeenCalled();
})
it('should not make shipping option change call if no shipping option is present', async () => {
const data = {
Expand Down

0 comments on commit 52951b4

Please sign in to comment.