|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +/* eslint-disable max-nested-callbacks */ |
| 7 | +define([ |
| 8 | + 'squire', |
| 9 | + 'ko', |
| 10 | + 'jquery', |
| 11 | + 'uiComponent' |
| 12 | +], function (Squire, ko, $, Component) { |
| 13 | + 'use strict'; |
| 14 | + |
| 15 | + describe('paypal/js/view/payment/method-renderer/paypal-express-abstract', function () { |
| 16 | + var injector = new Squire(), |
| 17 | + mocks = { |
| 18 | + 'Magento_Paypal/js/action/set-payment-method': jasmine.createSpy(), |
| 19 | + 'Magento_Paypal/js/view/payment/method-renderer/paypal-express-abstract': Component, |
| 20 | + 'Magento_Checkout/js/model/quote': { |
| 21 | + billingAddress: ko.observable(), |
| 22 | + shippingAddress: ko.observable(), |
| 23 | + paymentMethod: ko.observable() |
| 24 | + }, |
| 25 | + 'Magento_Checkout/js/model/payment/additional-validators': { |
| 26 | + validate: jasmine.createSpy().and.returnValue(true) |
| 27 | + }, |
| 28 | + 'Magento_Ui/js/model/messageList': { |
| 29 | + addErrorMessage: jasmine.createSpy(), |
| 30 | + addSuccessMessage: jasmine.createSpy() |
| 31 | + }, |
| 32 | + 'paypalInContextExpressCheckout': { |
| 33 | + checkout: { |
| 34 | + initXO: jasmine.createSpy(), |
| 35 | + closeFlow: jasmine.createSpy(), |
| 36 | + startFlow: jasmine.createSpy() |
| 37 | + } |
| 38 | + }, |
| 39 | + 'Magento_Customer/js/customer-data': { |
| 40 | + invalidate: jasmine.createSpy() |
| 41 | + } |
| 42 | + }, |
| 43 | + obj; |
| 44 | + |
| 45 | + beforeAll(function (done) { |
| 46 | + injector.mock(mocks); |
| 47 | + injector.require( |
| 48 | + ['Magento_Paypal/js/view/payment/method-renderer/in-context/checkout-express'], |
| 49 | + function (Constr) { |
| 50 | + obj = new Constr({ |
| 51 | + provider: 'provName', |
| 52 | + name: 'test', |
| 53 | + index: 'test', |
| 54 | + selectPaymentMethod: jasmine.createSpy() |
| 55 | + }); |
| 56 | + |
| 57 | + done(); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('"click" method checks', function () { |
| 62 | + it('check success request', function () { |
| 63 | + mocks['Magento_Paypal/js/action/set-payment-method'].and.callFake(function () { |
| 64 | + var promise = $.Deferred(); |
| 65 | + |
| 66 | + promise.resolve(); |
| 67 | + |
| 68 | + return promise; |
| 69 | + }); |
| 70 | + spyOn(jQuery, 'get').and.callFake(function () { |
| 71 | + var promise = $.Deferred(); |
| 72 | + |
| 73 | + promise.resolve({ |
| 74 | + url: 'url' |
| 75 | + }); |
| 76 | + |
| 77 | + return promise; |
| 78 | + }); |
| 79 | + |
| 80 | + obj.clientConfig.click(new Event('event')); |
| 81 | + |
| 82 | + expect(mocks.paypalInContextExpressCheckout.checkout.initXO).toHaveBeenCalled(); |
| 83 | + expect(mocks.paypalInContextExpressCheckout.checkout.startFlow).toHaveBeenCalledWith('url'); |
| 84 | + expect(mocks.paypalInContextExpressCheckout.checkout.closeFlow).not.toHaveBeenCalled(); |
| 85 | + expect(mocks['Magento_Customer/js/customer-data'].invalidate).toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('check request with error message', function () { |
| 89 | + mocks['Magento_Paypal/js/action/set-payment-method'].and.callFake(function () { |
| 90 | + var promise = $.Deferred(); |
| 91 | + |
| 92 | + promise.resolve(); |
| 93 | + |
| 94 | + return promise; |
| 95 | + }); |
| 96 | + spyOn(jQuery, 'get').and.callFake(function () { |
| 97 | + var promise = $.Deferred(); |
| 98 | + |
| 99 | + promise.resolve({ |
| 100 | + message: { |
| 101 | + text: 'Text', |
| 102 | + type: 'error' |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + return promise; |
| 107 | + }); |
| 108 | + |
| 109 | + obj.clientConfig.click(new Event('event')); |
| 110 | + |
| 111 | + expect(mocks['Magento_Ui/js/model/messageList'].addErrorMessage).toHaveBeenCalledWith({ |
| 112 | + message: 'Text' |
| 113 | + }); |
| 114 | + expect(mocks.paypalInContextExpressCheckout.checkout.initXO).toHaveBeenCalled(); |
| 115 | + expect(mocks.paypalInContextExpressCheckout.checkout.closeFlow).toHaveBeenCalled(); |
| 116 | + expect(mocks['Magento_Customer/js/customer-data'].invalidate).toHaveBeenCalled(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('check on fail request', function () { |
| 120 | + mocks['Magento_Paypal/js/action/set-payment-method'].and.callFake(function () { |
| 121 | + var promise = $.Deferred(); |
| 122 | + |
| 123 | + promise.reject(); |
| 124 | + |
| 125 | + return promise; |
| 126 | + }); |
| 127 | + spyOn(jQuery, 'get'); |
| 128 | + |
| 129 | + obj.clientConfig.click(new Event('event')); |
| 130 | + |
| 131 | + expect(jQuery.get).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments