|
| 1 | +import $ from 'jquery' |
| 2 | +import utils from '@bigcommerce/stencil-utils'; |
| 3 | +import Cart from '../../theme/cart.js'; |
| 4 | +import * as SweetAlert from 'sweetalert2'; |
| 5 | + |
| 6 | +var dataSpy; |
| 7 | +var swalSpy; |
| 8 | +var UpdateSpy; |
| 9 | +var c = new Cart(); |
| 10 | +beforeEach(function() { |
| 11 | + UpdateSpy = spyOn(utils.api.cart, 'itemUpdate'); |
| 12 | + |
| 13 | + dataSpy = function(requestedAction = null) { |
| 14 | + spyOn(jQuery.fn, 'data').and.callFake(function() { |
| 15 | + var param = arguments[0]; |
| 16 | + switch (param) { |
| 17 | + case 'action': |
| 18 | + return requestedAction; |
| 19 | + case 'cartItemid': |
| 20 | + return '11111'; |
| 21 | + case 'quantityMax': |
| 22 | + return 5; |
| 23 | + case 'quantityMin': |
| 24 | + return 1; |
| 25 | + case 'quantityMinError': |
| 26 | + return 'min error'; |
| 27 | + case 'quantityMaxError': |
| 28 | + return ' max error'; |
| 29 | + default: |
| 30 | + return null; |
| 31 | + } |
| 32 | + }) |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +var $dom = $('<table class="cart" data-cart-quantity="2">\ |
| 37 | + <thead class="cart-header">\ |
| 38 | + <tr>\ |
| 39 | + <th class="cart-header-item" colspan="2">Item</th>\ |
| 40 | + <th class="cart-header-item">Price</th>\ |
| 41 | + <th class="cart-header-item cart-header-quantity">Quantity</th>\ |
| 42 | + <th class="cart-header-item">Total</th>\ |
| 43 | + </tr>\ |
| 44 | + </thead>\ |
| 45 | + <tbody class="cart-list">\ |
| 46 | + <tr class="cart-item" data-item-row="">\ |
| 47 | + <td class="cart-item-block cart-item-figure">\ |
| 48 | + <img class="cart-item-image lazyautosizes lazyloaded" data-sizes="auto" src="www.example.com" data-src="www.example.com" alt="[Sample] product" title="[Sample] product">\ |
| 49 | + </td>\ |
| 50 | + <td class="cart-item-block cart-item-title">\ |
| 51 | + <h4 class="cart-item-name"><a href="/fog-linen-chambray-towel-beige-stripe/">[Sample] Fog Linen Chambray Towel - Beige Stripe</a></h4>\ |
| 52 | + <dl class="definitionList">\ |
| 53 | + <dt class="definitionList-key">Size:</dt>\ |
| 54 | + <dd class="definitionList-value">\ |
| 55 | + XS\ |
| 56 | + </dd>\ |
| 57 | + <dt class="definitionList-key">Color:</dt>\ |
| 58 | + <dd class="definitionList-value">\ |
| 59 | + Silver\ |
| 60 | + </dd>\ |
| 61 | + </dl>\ |
| 62 | + <a href="#" data-item-edit="item-id">Change</a>\ |
| 63 | + </td>\ |
| 64 | + <td class="cart-item-block cart-item-info">\ |
| 65 | + <span class="cart-item-label">Price</span>\ |
| 66 | + <span class="cart-item-value ">$49.00</span>\ |
| 67 | + </td>\ |
| 68 | + <td class="cart-item-block cart-item-info cart-item-quantity">\ |
| 69 | + <label class="form-label cart-item-label" for="qty-item-id">Quantity:</label>\ |
| 70 | + <div class="form-increment">\ |
| 71 | + <button class="button button--icon" data-cart-update="" data-cart-itemid="item-id" data-action="dec">\ |
| 72 | + <span class="is-srOnly">Decrease Quantity:</span>\ |
| 73 | + <i class="icon" aria-hidden="true"><svg><use xmlns:xlink="www.ccc.com"></use></svg></i>\ |
| 74 | + </button>\ |
| 75 | + <input class="form-input form-input--incrementTotal cart-item-qty-input" id="item-id" name="qty-item-id" type="tel" value="2" data-quantity-min="0" data-quantity-max="" data-quantity-min-error="The minimum purchasable quantity is 0" data-quantity-max-error="The maximum purchasable quantity is null" min="1" pattern="[0-9]*" data-cart-itemid="item-id" data-action="manualQtyChange" aria-live="polite">\ |
| 76 | + <button class="button button--icon" data-cart-update="" data-cart-itemid="item-id" data-action="inc">\ |
| 77 | + <span class="is-srOnly">Increase Quantity:</span>\ |
| 78 | + <i class="icon" aria-hidden="true"><svg><use xmlns:xlink="www.ddd.com"></use></svg></i>\ |
| 79 | + </button>\ |
| 80 | + </div>\ |
| 81 | + </td>\ |
| 82 | + <td class="cart-item-block cart-item-info">\ |
| 83 | + <span class="cart-item-label">Total</span>\ |
| 84 | + <strong class="cart-item-value ">$98.00</strong>\ |
| 85 | + <a class="cart-remove icon" data-cart-itemid="item-id" href="#" data-confirm-delete="Are you sure you want to delete this item?">\ |
| 86 | + <svg><use xmlns:xlink="www.eee.com" xlink:href="#icon-close"></use></svg>\ |
| 87 | + </a>\ |
| 88 | + </td>\ |
| 89 | + </tr>\ |
| 90 | + </tbody>\ |
| 91 | +</table>') |
| 92 | + |
| 93 | +c.onReady(); |
| 94 | + |
| 95 | +describe('cartUpdate', () => { |
| 96 | + it('should INCRIMENT qty', () => { |
| 97 | + dataSpy |
| 98 | + dataSpy('inc'); |
| 99 | + spyOn(jQuery.fn, 'val').and.returnValue(2); |
| 100 | + c.cartUpdate($dom); |
| 101 | + |
| 102 | + expect(UpdateSpy).toHaveBeenCalledWith('11111', 3, jasmine.any(Function)); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should DECREMENT qty', () => { |
| 106 | + dataSpy |
| 107 | + dataSpy('dec'); |
| 108 | + spyOn(jQuery.fn, 'val').and.returnValue(2); |
| 109 | + c.cartUpdate($dom); |
| 110 | + |
| 111 | + expect(UpdateSpy).toHaveBeenCalledWith('11111', 1, jasmine.any(Function)); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('cartUpdateQtyTextChange', () => { |
| 116 | + it('should CHANGE qty completly based on the cart-item-qty-input', () => { |
| 117 | + dataSpy |
| 118 | + dataSpy('manualQtyChange'); |
| 119 | + spyOn(jQuery.fn, 'attr').and.returnValue(5); |
| 120 | + spyOn(jQuery.fn, 'val').and.returnValue(2); |
| 121 | + c.cartUpdateQtyTextChange($dom); |
| 122 | + |
| 123 | + expect(UpdateSpy).toHaveBeenCalledWith('11111', 5, jasmine.any(Function)); |
| 124 | + }); |
| 125 | +}); |
0 commit comments