|
| 1 | +/** |
| 2 | + * Copyright 2024 Adobe |
| 3 | + * All Rights Reserved. |
| 4 | + */ |
| 5 | +define([ |
| 6 | + 'jquery', |
| 7 | + 'mage/validation/validation', |
| 8 | + 'mage/translate' |
| 9 | +], function ($) { |
| 10 | + 'use strict'; |
| 11 | + |
| 12 | + describe('Custom Validation: validate-grouped-qty', function () { |
| 13 | + let element, params; |
| 14 | + |
| 15 | + beforeEach(function () { |
| 16 | + // Create the current element to simulate individual input |
| 17 | + element = $('<input>', { |
| 18 | + type: 'text', |
| 19 | + 'data-validate': '{"validate-grouped-qty": true}', |
| 20 | + value: '1' |
| 21 | + }); |
| 22 | + |
| 23 | + // Create a container for grouped inputs with only two inputs |
| 24 | + params = $('<div>').append( |
| 25 | + $('<input>', { type: 'text', value: '1', 'data-validate': '{"validate-grouped-qty": true}' }), |
| 26 | + $('<input>', { type: 'text', value: '0', 'data-validate': '{"validate-grouped-qty": true}' }) |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(function () { |
| 31 | + element.remove(); |
| 32 | + params.remove(); |
| 33 | + }); |
| 34 | + |
| 35 | + function setCurrentElement(val) { |
| 36 | + element.val(val); |
| 37 | + } |
| 38 | + |
| 39 | + it('should return true when total grouped quantity is greater than 0', function () { |
| 40 | + let isValid = $.validator.methods['validate-grouped-qty']( |
| 41 | + element.val(), |
| 42 | + element[0], |
| 43 | + params |
| 44 | + ); |
| 45 | + |
| 46 | + expect(isValid).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should return false when the total grouped quantity is 0', function () { |
| 50 | + setCurrentElement('0'); // Set the current input to 0 |
| 51 | + params.find('input').each(function () { |
| 52 | + $(this).val('0'); // Set both grouped inputs to 0 |
| 53 | + }); |
| 54 | + |
| 55 | + let isValid = $.validator.methods['validate-grouped-qty']( |
| 56 | + element.val(), |
| 57 | + element[0], |
| 58 | + params |
| 59 | + ); |
| 60 | + |
| 61 | + expect(isValid).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return false if any input has a negative value', function () { |
| 65 | + setCurrentElement(-1);// Set the current input to -1 |
| 66 | + params.find('input').first().val('-1'); // Set the first input to a negative value |
| 67 | + |
| 68 | + let isValid = $.validator.methods['validate-grouped-qty']( |
| 69 | + element.val(), |
| 70 | + element[0], |
| 71 | + params |
| 72 | + ); |
| 73 | + |
| 74 | + expect(isValid).toBe(false); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return true when one input is empty and total is valid', function () { |
| 78 | + params.find('input').eq(1).val(''); // Leave one input empty |
| 79 | + |
| 80 | + let isValid = $.validator.methods['validate-grouped-qty']( |
| 81 | + element.val(), |
| 82 | + element[0], |
| 83 | + params |
| 84 | + ); |
| 85 | + |
| 86 | + expect(isValid).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return false when both inputs are empty', function () { |
| 90 | + setCurrentElement(''); |
| 91 | + params.find('input').each(function () { |
| 92 | + $(this).val(''); // Set both inputs to empty |
| 93 | + }); |
| 94 | + |
| 95 | + let isValid = $.validator.methods['validate-grouped-qty']( |
| 96 | + element.val(), |
| 97 | + element[0], |
| 98 | + params |
| 99 | + ); |
| 100 | + |
| 101 | + expect(isValid).toBe(false); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should return false if one input is negative and the other is 0', function () { |
| 105 | + setCurrentElement(-1); |
| 106 | + params.find('input').first().val('-1'); // Set one input to negative |
| 107 | + params.find('input').eq(1).val('0'); // Set second input to 0 |
| 108 | + |
| 109 | + let isValid = $.validator.methods['validate-grouped-qty']( |
| 110 | + element.val(), |
| 111 | + element[0], |
| 112 | + params |
| 113 | + ); |
| 114 | + |
| 115 | + expect(isValid).toBe(false); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments