|
| 1 | +import { PaymentRequest } from '../../models/api/api_requests/payment_request'; |
| 2 | +import { GeneralError, NetworkError } from '../../models/errors/moyasar_errors'; |
| 3 | +import { createPayment } from '../../services/payment_service'; |
| 4 | +import { SamsungPayRequestSource } from '../../models/api/sources/samsung_pay/samsung_pay_request_source'; |
| 5 | +import { onSamsungPayResponse } from '../../views/samsung_pay/samsung_pay'; |
| 6 | +import { paymentConfigWithoutSaveOnlyFixture } from '../__fixtures__/payment_config_fixture'; |
| 7 | +import { SamsungPayConfig } from '../../models/samsung_pay_config'; |
| 8 | +import { paymentResponseWithPaidSamsungFixture } from '../__fixtures__/payment_response_fixture'; |
| 9 | + |
| 10 | +jest.mock('../../services/payment_service'); |
| 11 | +jest.mock('../../localizations/i18n', () => ({ |
| 12 | + getCurrentLang: jest.fn(() => 'en'), |
| 13 | +})); |
| 14 | + |
| 15 | +describe('onSamsungPayResponse', () => { |
| 16 | + const mockSamsungPayToken = 'mockSamsungPayToken'; |
| 17 | + const mockOrderNumber = 'order-number-123'; |
| 18 | + const onPaymentResult = jest.fn(); |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + jest.resetAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should create a payment and call onPaymentResult with the response', async () => { |
| 25 | + (createPayment as jest.Mock).mockResolvedValue( |
| 26 | + paymentResponseWithPaidSamsungFixture |
| 27 | + ); |
| 28 | + |
| 29 | + await onSamsungPayResponse( |
| 30 | + mockSamsungPayToken, |
| 31 | + mockOrderNumber, |
| 32 | + paymentConfigWithoutSaveOnlyFixture, |
| 33 | + onPaymentResult |
| 34 | + ); |
| 35 | + |
| 36 | + expect(createPayment).toHaveBeenCalledWith( |
| 37 | + expect.any(PaymentRequest), |
| 38 | + paymentConfigWithoutSaveOnlyFixture.publishableApiKey |
| 39 | + ); |
| 40 | + expect(onPaymentResult).toHaveBeenCalledWith( |
| 41 | + paymentResponseWithPaidSamsungFixture |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should pass manual and save_card from samsung config to source payload', async () => { |
| 46 | + (createPayment as jest.Mock).mockResolvedValue( |
| 47 | + paymentResponseWithPaidSamsungFixture |
| 48 | + ); |
| 49 | + |
| 50 | + const configWithSamsungTokenization = { |
| 51 | + ...paymentConfigWithoutSaveOnlyFixture, |
| 52 | + samsungPay: new SamsungPayConfig({ |
| 53 | + serviceId: 'ea810dafb758408fa530b1', |
| 54 | + merchantName: 'Test Samsung', |
| 55 | + orderNumber: mockOrderNumber, |
| 56 | + manual: true, |
| 57 | + saveCard: true, |
| 58 | + }), |
| 59 | + }; |
| 60 | + |
| 61 | + await onSamsungPayResponse( |
| 62 | + mockSamsungPayToken, |
| 63 | + mockOrderNumber, |
| 64 | + configWithSamsungTokenization, |
| 65 | + onPaymentResult |
| 66 | + ); |
| 67 | + |
| 68 | + const paymentRequest = (createPayment as jest.Mock).mock |
| 69 | + .calls[0][0] as PaymentRequest; |
| 70 | + const samsungSource = paymentRequest.source as SamsungPayRequestSource; |
| 71 | + |
| 72 | + expect(samsungSource.toJson()).toEqual( |
| 73 | + expect.objectContaining({ |
| 74 | + type: 'samsungpay', |
| 75 | + token: mockSamsungPayToken, |
| 76 | + manual: 'true', |
| 77 | + save_card: true, |
| 78 | + }) |
| 79 | + ); |
| 80 | + expect(paymentRequest.metadata).toEqual( |
| 81 | + expect.objectContaining({ samsungpay_order_id: mockOrderNumber }) |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should handle MoyasarError and call onPaymentResult with the error', async () => { |
| 86 | + const mockError = new GeneralError('Test Error'); |
| 87 | + |
| 88 | + (createPayment as jest.Mock).mockRejectedValue(mockError); |
| 89 | + |
| 90 | + await onSamsungPayResponse( |
| 91 | + mockSamsungPayToken, |
| 92 | + mockOrderNumber, |
| 93 | + paymentConfigWithoutSaveOnlyFixture, |
| 94 | + onPaymentResult |
| 95 | + ); |
| 96 | + |
| 97 | + expect(onPaymentResult).toHaveBeenCalledWith(mockError); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should handle non-MoyasarError and call onPaymentResult with NetworkError', async () => { |
| 101 | + const mockError = new Error('Network Error'); |
| 102 | + (createPayment as jest.Mock).mockRejectedValue(mockError); |
| 103 | + |
| 104 | + await onSamsungPayResponse( |
| 105 | + mockSamsungPayToken, |
| 106 | + mockOrderNumber, |
| 107 | + paymentConfigWithoutSaveOnlyFixture, |
| 108 | + onPaymentResult |
| 109 | + ); |
| 110 | + |
| 111 | + expect(onPaymentResult).toHaveBeenCalledWith(expect.any(NetworkError)); |
| 112 | + }); |
| 113 | +}); |
0 commit comments