|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | +/* eslint-disable max-nested-callbacks */ |
| 6 | +/*jscs:disable jsDoc*/ |
| 7 | + |
| 8 | +define([ |
| 9 | + 'squire', |
| 10 | + 'jquery', |
| 11 | + 'ko' |
| 12 | +], function (Squire, $, ko) { |
| 13 | + 'use strict'; |
| 14 | + |
| 15 | + describe('Magento_Checkout/js/sidebar', function () { |
| 16 | + var injector = new Squire(), |
| 17 | + mocks = { |
| 18 | + 'Magento_Customer/js/customer-data': { |
| 19 | + get: function () { |
| 20 | + return ko.observable(); |
| 21 | + } |
| 22 | + } |
| 23 | + }, |
| 24 | + sidebar, |
| 25 | + cartData = { |
| 26 | + 'items': [ |
| 27 | + { |
| 28 | + 'item_id': 1, |
| 29 | + 'product_sku': 'bundle' |
| 30 | + }, |
| 31 | + { |
| 32 | + 'item_id': 5, |
| 33 | + 'product_sku': 'simple' |
| 34 | + }, |
| 35 | + { |
| 36 | + 'item_id': 7, |
| 37 | + 'product_sku': 'configurable' |
| 38 | + } |
| 39 | + ] |
| 40 | + }, |
| 41 | + cart = ko.observable(cartData); |
| 42 | + |
| 43 | + beforeEach(function (done) { |
| 44 | + injector.mock(mocks); |
| 45 | + injector.require(['Magento_Checkout/js/sidebar'], function (Constr) { |
| 46 | + sidebar = new Constr; |
| 47 | + done(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('Check remove mini-cart item callback.', function () { |
| 52 | + beforeEach(function () { |
| 53 | + spyOn(jQuery.fn, 'trigger'); |
| 54 | + spyOn(mocks['Magento_Customer/js/customer-data'], 'get').and.returnValue(cart); |
| 55 | + }); |
| 56 | + |
| 57 | + it('Method "_removeItemAfter" is defined', function () { |
| 58 | + expect(sidebar._removeItemAfter).toBeDefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('Cart item is exists', function () { |
| 62 | + var elem = $('<input>').data('cart-item', 5); |
| 63 | + |
| 64 | + sidebar._removeItemAfter(elem); |
| 65 | + expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart'); |
| 66 | + expect(jQuery('body').trigger).toHaveBeenCalledWith('ajax:removeFromCart', 'simple'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('Cart item doesn\'t exists', function () { |
| 70 | + var elem = $('<input>').data('cart-item', 100); |
| 71 | + |
| 72 | + sidebar._removeItemAfter(elem); |
| 73 | + expect(jQuery('body').trigger).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('Check update item quantity callback.', function () { |
| 78 | + beforeEach(function () { |
| 79 | + spyOn(jQuery.fn, 'trigger'); |
| 80 | + spyOn(mocks['Magento_Customer/js/customer-data'], 'get').and.returnValue(cart); |
| 81 | + }); |
| 82 | + |
| 83 | + it('Method "_updateItemQtyAfter" is defined', function () { |
| 84 | + expect(sidebar._updateItemQtyAfter).toBeDefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('Cart item is exists', function () { |
| 88 | + var elem = $('<input>').data('cart-item', 5); |
| 89 | + |
| 90 | + spyOn(sidebar, '_hideItemButton'); |
| 91 | + |
| 92 | + sidebar._updateItemQtyAfter(elem); |
| 93 | + expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart'); |
| 94 | + expect(jQuery('body').trigger).toHaveBeenCalledWith('ajax:updateCartItemQty', 'simple'); |
| 95 | + expect(sidebar._hideItemButton).toHaveBeenCalledWith(elem); |
| 96 | + }); |
| 97 | + |
| 98 | + it('Cart item doesn\'t exists', function () { |
| 99 | + var elem = $('<input>').data('cart-item', 100); |
| 100 | + |
| 101 | + spyOn(sidebar, '_hideItemButton'); |
| 102 | + |
| 103 | + sidebar._updateItemQtyAfter(elem); |
| 104 | + expect(jQuery('body').trigger).not.toHaveBeenCalled(); |
| 105 | + expect(sidebar._hideItemButton).toHaveBeenCalledWith(elem); |
| 106 | + }); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments