|
| 1 | +/** |
| 2 | + * Copyright 2025 Adobe |
| 3 | + * All Rights Reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +define([ |
| 7 | + 'jquery', |
| 8 | + 'mage/adminhtml/form' |
| 9 | +], function ($) { |
| 10 | + 'use strict'; |
| 11 | + |
| 12 | + describe('mage/adminhtml/form', function () { |
| 13 | + var id = 'edit_form', |
| 14 | + elementId = '#' + id; |
| 15 | + |
| 16 | + beforeEach(function () { |
| 17 | + var element = $('<form id="' + id + '" action="action/url" method="GET" target="_self" ></form>'); |
| 18 | + |
| 19 | + element.appendTo('body'); |
| 20 | + }); |
| 21 | + afterEach(function () { |
| 22 | + $(elementId).remove(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not enable inputs that have the disabled CSS class when dependencies are satisfied', function () { |
| 26 | + var container = document.createElement('div'), |
| 27 | + target = document.createElement('input'), |
| 28 | + cssDisabled = document.createElement('input'), |
| 29 | + normalInput = document.createElement('input'), |
| 30 | + source = document.createElement('input'), |
| 31 | + originalObserve = window.Event && window.Event.observe, |
| 32 | + originalDollar = window.$, |
| 33 | + fakeTarget = { |
| 34 | + id: 'test_input_dependent', |
| 35 | + type: 'input', |
| 36 | + tagName: 'INPUT', |
| 37 | + getAttribute: function () { |
| 38 | + return null; |
| 39 | + }, |
| 40 | + show: function () {}, |
| 41 | + hide: function () {}, |
| 42 | + up: function () { |
| 43 | + return { |
| 44 | + show: function () {}, |
| 45 | + hide: function () {}, |
| 46 | + select: function () { |
| 47 | + return [cssDisabled, normalInput]; |
| 48 | + } |
| 49 | + }; |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + document.body.appendChild(container); |
| 54 | + target.id = 'test_input_dependent'; |
| 55 | + container.appendChild(target); |
| 56 | + cssDisabled.id = 'css_disabled_input'; |
| 57 | + cssDisabled.className = 'disabled'; |
| 58 | + cssDisabled.disabled = true; |
| 59 | + container.appendChild(cssDisabled); |
| 60 | + normalInput.id = 'normal_input'; |
| 61 | + normalInput.disabled = true; |
| 62 | + container.appendChild(normalInput); |
| 63 | + source.id = 'dep_source'; |
| 64 | + source.value = '1'; |
| 65 | + document.body.appendChild(source); |
| 66 | + |
| 67 | + if (window.Event) { |
| 68 | + window.Event.observe = function () {}; |
| 69 | + } |
| 70 | + |
| 71 | + window.$ = function (elemId) { |
| 72 | + if (elemId === 'test_input_dependent') { |
| 73 | + return fakeTarget; |
| 74 | + } |
| 75 | + return document.getElementById(elemId); |
| 76 | + }; |
| 77 | + /* eslint-disable no-new */ |
| 78 | + new window.FormElementDependenceController({ |
| 79 | + 'test_input_dependent': { |
| 80 | + 'dep_source': { values: ['1'] } |
| 81 | + } |
| 82 | + }, { |
| 83 | + levels_up: 1 |
| 84 | + }); |
| 85 | + /* eslint-enable no-new */ |
| 86 | + |
| 87 | + // The element with CSS class "disabled" must remain disabled |
| 88 | + expect(cssDisabled.disabled).toBe(true); |
| 89 | + // The normal input should be enabled because dependencies are satisfied |
| 90 | + expect(normalInput.disabled).toBe(false); |
| 91 | + |
| 92 | + // Cleanup |
| 93 | + if (window.Event && originalObserve) { |
| 94 | + window.Event.observe = originalObserve; |
| 95 | + } |
| 96 | + if (originalDollar) { |
| 97 | + window.$ = originalDollar; |
| 98 | + } |
| 99 | + document.body.removeChild(container); |
| 100 | + document.body.removeChild(source); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments