|
| 1 | +import test from "ava"; |
| 2 | +import { getComponent } from "../utils.js"; |
| 3 | +import { handleDirty } from "../../../src/django_unicorn/static/unicorn/js/eventListeners.js"; |
| 4 | + |
| 5 | +test("dirtyEls: element with u:dirty.class and no u:model is collected", (t) => { |
| 6 | + const html = ` |
| 7 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 8 | + <input u:model="name" id="nameInput"> |
| 9 | + <div u:dirty.class="is-dirty" u:target="nameInput"></div> |
| 10 | +</div>`; |
| 11 | + const component = getComponent(html); |
| 12 | + |
| 13 | + t.is(component.dirtyEls.length, 1); |
| 14 | + t.deepEqual(component.dirtyEls[0].dirty.classes, ["is-dirty"]); |
| 15 | +}); |
| 16 | + |
| 17 | +test("dirtyEls: element with u:model is NOT added to dirtyEls (self-dirty handled inline)", (t) => { |
| 18 | + const html = ` |
| 19 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 20 | + <input u:model="name" u:dirty.class="is-dirty"> |
| 21 | +</div>`; |
| 22 | + const component = getComponent(html); |
| 23 | + |
| 24 | + t.is(component.dirtyEls.length, 0); |
| 25 | + t.is(component.modelEls.length, 1); |
| 26 | +}); |
| 27 | + |
| 28 | +test("dirtyEls: multiple separate dirty elements are all collected", (t) => { |
| 29 | + const html = ` |
| 30 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 31 | + <input u:model="name" id="nameInput"> |
| 32 | + <div id="wrapper" u:dirty.class="is-dirty" u:target="nameInput"></div> |
| 33 | + <span u:dirty.attr="disabled" u:target="nameInput"></span> |
| 34 | +</div>`; |
| 35 | + const component = getComponent(html); |
| 36 | + |
| 37 | + t.is(component.dirtyEls.length, 2); |
| 38 | +}); |
| 39 | + |
| 40 | +test("dirtyEls: element without u:dirty modifier produces empty dirty object and is NOT collected", (t) => { |
| 41 | + // A bare u:dirty with no modifier like .class or .attr results in an empty |
| 42 | + // dirty object, which should not be added to dirtyEls. |
| 43 | + const html = ` |
| 44 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 45 | + <input u:model="name"> |
| 46 | +</div>`; |
| 47 | + const component = getComponent(html); |
| 48 | + |
| 49 | + t.is(component.dirtyEls.length, 0); |
| 50 | +}); |
| 51 | + |
| 52 | +test("handleDirty: adds dirty class to element that targets the changed model input (by id)", (t) => { |
| 53 | + const html = ` |
| 54 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 55 | + <input u:model="name" id="nameInput"> |
| 56 | + <div u:dirty.class="is-dirty" u:target="nameInput"></div> |
| 57 | +</div>`; |
| 58 | + const component = getComponent(html); |
| 59 | + |
| 60 | + const modelElement = component.modelEls[0]; |
| 61 | + const dirtyElement = component.dirtyEls[0]; |
| 62 | + |
| 63 | + t.is(dirtyElement.el.classList.length, 0); |
| 64 | + |
| 65 | + handleDirty(component, modelElement); |
| 66 | + |
| 67 | + t.is(dirtyElement.el.classList.length, 1); |
| 68 | + t.is(dirtyElement.el.classList[0], "is-dirty"); |
| 69 | +}); |
| 70 | + |
| 71 | +test("handleDirty: reverts dirty class when model input returns to original value", (t) => { |
| 72 | + const html = ` |
| 73 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 74 | + <input u:model="name" id="nameInput"> |
| 75 | + <div u:dirty.class="is-dirty is-already-dirty" u:target="nameInput" class="is-dirty is-already-dirty"></div> |
| 76 | +</div>`; |
| 77 | + const component = getComponent(html); |
| 78 | + |
| 79 | + const modelElement = component.modelEls[0]; |
| 80 | + const dirtyElement = component.dirtyEls[0]; |
| 81 | + |
| 82 | + t.is(dirtyElement.el.classList.length, 2); |
| 83 | + |
| 84 | + handleDirty(component, modelElement, true); |
| 85 | + |
| 86 | + t.is(dirtyElement.el.classList.length, 0); |
| 87 | +}); |
| 88 | + |
| 89 | +test("handleDirty: does NOT touch a dirty element that targets a different id", (t) => { |
| 90 | + const html = ` |
| 91 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 92 | + <input u:model="name" id="nameInput"> |
| 93 | + <input u:model="email" id="emailInput"> |
| 94 | + <div u:dirty.class="is-dirty" u:target="emailInput"></div> |
| 95 | +</div>`; |
| 96 | + const component = getComponent(html); |
| 97 | + |
| 98 | + const nameModelElement = component.modelEls[0]; |
| 99 | + const dirtyElement = component.dirtyEls[0]; |
| 100 | + |
| 101 | + t.is(dirtyElement.el.classList.length, 0); |
| 102 | + |
| 103 | + // Only name changed — dirty element targets emailInput, so it must stay clean |
| 104 | + handleDirty(component, nameModelElement); |
| 105 | + |
| 106 | + t.is(dirtyElement.el.classList.length, 0); |
| 107 | +}); |
| 108 | + |
| 109 | +test("handleDirty: adds dirty class to element that targets the model input by key", (t) => { |
| 110 | + const html = ` |
| 111 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 112 | + <input u:model="name" u:key="nameKey"> |
| 113 | + <div u:dirty.class="is-dirty" u:target="nameKey"></div> |
| 114 | +</div>`; |
| 115 | + const component = getComponent(html); |
| 116 | + |
| 117 | + const modelElement = component.modelEls[0]; |
| 118 | + const dirtyElement = component.dirtyEls[0]; |
| 119 | + |
| 120 | + t.is(dirtyElement.el.classList.length, 0); |
| 121 | + |
| 122 | + handleDirty(component, modelElement); |
| 123 | + |
| 124 | + t.is(dirtyElement.el.classList.length, 1); |
| 125 | + t.is(dirtyElement.el.classList[0], "is-dirty"); |
| 126 | +}); |
| 127 | + |
| 128 | + |
| 129 | +test("handleDirty: sets attr on element that targets the model input", (t) => { |
| 130 | + const html = ` |
| 131 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 132 | + <input u:model="name" id="nameInput"> |
| 133 | + <div u:dirty.attr="readonly" u:target="nameInput"></div> |
| 134 | +</div>`; |
| 135 | + const component = getComponent(html); |
| 136 | + |
| 137 | + const modelElement = component.modelEls[0]; |
| 138 | + const dirtyElement = component.dirtyEls[0]; |
| 139 | + |
| 140 | + // Note: the test walker overrides getAttribute, so use hasAttribute instead |
| 141 | + t.false(dirtyElement.el.hasAttribute("readonly")); |
| 142 | + |
| 143 | + handleDirty(component, modelElement); |
| 144 | + |
| 145 | + t.true(dirtyElement.el.hasAttribute("readonly")); |
| 146 | +}); |
| 147 | + |
| 148 | +test("handleDirty: removes attr when reverting on targeted element", (t) => { |
| 149 | + const html = ` |
| 150 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 151 | + <input u:model="name" id="nameInput"> |
| 152 | + <div u:dirty.attr="readonly" u:target="nameInput" readonly="readonly"></div> |
| 153 | +</div>`; |
| 154 | + const component = getComponent(html); |
| 155 | + |
| 156 | + const modelElement = component.modelEls[0]; |
| 157 | + const dirtyElement = component.dirtyEls[0]; |
| 158 | + |
| 159 | + t.true(dirtyElement.el.hasAttribute("readonly")); |
| 160 | + |
| 161 | + handleDirty(component, modelElement, true); |
| 162 | + |
| 163 | + t.false(dirtyElement.el.hasAttribute("readonly")); |
| 164 | +}); |
| 165 | + |
| 166 | + |
| 167 | +test("handleDirty: untargeted dirty element becomes dirty on any model change", (t) => { |
| 168 | + const html = ` |
| 169 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 170 | + <input u:model="name" id="nameInput"> |
| 171 | + <div u:dirty.class="form-changed"></div> |
| 172 | +</div>`; |
| 173 | + const component = getComponent(html); |
| 174 | + |
| 175 | + const modelElement = component.modelEls[0]; |
| 176 | + const dirtyElement = component.dirtyEls[0]; |
| 177 | + |
| 178 | + t.is(dirtyElement.el.classList.length, 0); |
| 179 | + |
| 180 | + handleDirty(component, modelElement); |
| 181 | + |
| 182 | + t.is(dirtyElement.el.classList.length, 1); |
| 183 | + t.is(dirtyElement.el.classList[0], "form-changed"); |
| 184 | +}); |
| 185 | + |
| 186 | +test("handleDirty: untargeted dirty element is NOT auto-reverted during editing (only after server response)", (t) => { |
| 187 | + const html = ` |
| 188 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 189 | + <input u:model="name" id="nameInput"> |
| 190 | + <div u:dirty.class="form-changed" class="form-changed"></div> |
| 191 | +</div>`; |
| 192 | + const component = getComponent(html); |
| 193 | + |
| 194 | + const modelElement = component.modelEls[0]; |
| 195 | + const dirtyElement = component.dirtyEls[0]; |
| 196 | + |
| 197 | + // Class is already present (element was dirtied earlier) |
| 198 | + t.is(dirtyElement.el.classList.length, 1); |
| 199 | + |
| 200 | + handleDirty(component, modelElement, true); |
| 201 | + |
| 202 | + t.is(dirtyElement.el.classList.length, 1); |
| 203 | + t.is(dirtyElement.el.classList[0], "form-changed"); |
| 204 | +}); |
| 205 | + |
| 206 | +test("handleDirty: removes class on targeted element (class.remove modifier)", (t) => { |
| 207 | + const html = ` |
| 208 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 209 | + <input u:model="name" id="nameInput"> |
| 210 | + <div u:dirty.class.remove="btn-clean" u:target="nameInput" class="btn-clean"></div> |
| 211 | +</div>`; |
| 212 | + const component = getComponent(html); |
| 213 | + |
| 214 | + const modelElement = component.modelEls[0]; |
| 215 | + const dirtyElement = component.dirtyEls[0]; |
| 216 | + |
| 217 | + t.is(dirtyElement.el.classList.length, 1); |
| 218 | + t.is(dirtyElement.el.classList[0], "btn-clean"); |
| 219 | + |
| 220 | + handleDirty(component, modelElement); |
| 221 | + |
| 222 | + t.is(dirtyElement.el.classList.length, 0); |
| 223 | +}); |
| 224 | + |
| 225 | +test("handleDirty: restores removed class when reverting on targeted element", (t) => { |
| 226 | + const html = ` |
| 227 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:meta="GXzew3Km"> |
| 228 | + <input u:model="name" id="nameInput"> |
| 229 | + <div u:dirty.class.remove="btn-clean" u:target="nameInput" class=""></div> |
| 230 | +</div>`; |
| 231 | + const component = getComponent(html); |
| 232 | + |
| 233 | + const modelElement = component.modelEls[0]; |
| 234 | + const dirtyElement = component.dirtyEls[0]; |
| 235 | + |
| 236 | + t.is(dirtyElement.el.classList.length, 0); |
| 237 | + |
| 238 | + handleDirty(component, modelElement, true); |
| 239 | + |
| 240 | + t.is(dirtyElement.el.classList.length, 1); |
| 241 | + t.is(dirtyElement.el.classList[0], "btn-clean"); |
| 242 | +}); |
0 commit comments