|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +define(['squire', 'jquery'], function (Squire, $) { |
| 7 | + 'use strict'; |
| 8 | + |
| 9 | + var injector = new Squire(), |
| 10 | + agreementsBlockSelector = '.payment-method._active div.checkout-agreements', |
| 11 | + agreementsBlock = $('<div class="payment-method _active"><div class="checkout-agreements"></div></div>'), |
| 12 | + obj, |
| 13 | + |
| 14 | + /** |
| 15 | + * @param {Boolean} isChecked |
| 16 | + * @return {Object} |
| 17 | + */ |
| 18 | + getCheckboxMock = function (isChecked) { |
| 19 | + return $('<input type="checkbox" class="required-entry" name="some"/>').prop('checked', isChecked); |
| 20 | + }; |
| 21 | + |
| 22 | + beforeEach(function (done) { |
| 23 | + window.checkoutConfig = { |
| 24 | + checkoutAgreements: { |
| 25 | + isEnabled: true |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + $('body').append(agreementsBlock); |
| 30 | + |
| 31 | + injector.require(['Magento_CheckoutAgreements/js/model/agreement-validator'], function (Validator) { |
| 32 | + obj = Validator; |
| 33 | + done(); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(function () { |
| 38 | + $('.payment-method._active').remove(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('Magento_CheckoutAgreements/js/model/agreement-validator', function () { |
| 42 | + describe('"validate" method', function () { |
| 43 | + it('Check with non existing checkboxes', function () { |
| 44 | + expect(obj.validate()).toBe(true); |
| 45 | + }); |
| 46 | + |
| 47 | + it('Check with unchecked checkboxes', function () { |
| 48 | + $(agreementsBlockSelector).html(getCheckboxMock(false)); |
| 49 | + expect(obj.validate()).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('Check with checked checkboxes', function () { |
| 53 | + $(agreementsBlockSelector).html(getCheckboxMock(true)); |
| 54 | + expect(obj.validate()).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('Check with several checkboxes', function () { |
| 58 | + $(agreementsBlockSelector).html(getCheckboxMock(true)); |
| 59 | + $(agreementsBlockSelector).append(getCheckboxMock(false)); |
| 60 | + expect(obj.validate()).toBe(false); |
| 61 | + |
| 62 | + $(agreementsBlockSelector).html(getCheckboxMock(false)); |
| 63 | + $(agreementsBlockSelector).append(getCheckboxMock(false)); |
| 64 | + expect(obj.validate()).toBe(false); |
| 65 | + |
| 66 | + $(agreementsBlockSelector).html(getCheckboxMock(true)); |
| 67 | + $(agreementsBlockSelector).append(getCheckboxMock(true)); |
| 68 | + expect(obj.validate()).toBe(true); |
| 69 | + }); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments