|
1 | 1 | import test from "ava"; |
| 2 | +import fetchMock from "fetch-mock"; |
| 3 | +import { send } from "../../../django_unicorn/static/unicorn/js/messageSender.js"; |
2 | 4 | import { getComponent, getElement } from "../utils.js"; |
3 | 5 |
|
4 | 6 | test("click", (t) => { |
@@ -313,6 +315,59 @@ test("event action loading class", (t) => { |
313 | 315 | t.is(el.classList[0], "loading"); |
314 | 316 | }); |
315 | 317 |
|
| 318 | +test.cb("event action loading attr 500 reverts", (t) => { |
| 319 | + const html = ` |
| 320 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:checksum="GXzew3Km"> |
| 321 | + <button unicorn:click='test()' u:loading.attr="disabled"></button> |
| 322 | +</div>`; |
| 323 | + const component = getComponent(html); |
| 324 | + |
| 325 | + t.is(component.attachedEventTypes.length, 1); |
| 326 | + t.is(component.actionEvents.click.length, 1); |
| 327 | + |
| 328 | + const { el } = component.actionEvents.click[0].element; |
| 329 | + t.true(typeof el.attributes.disabled === "undefined"); |
| 330 | + |
| 331 | + el.click(); |
| 332 | + t.false(typeof el.attributes.disabled === "undefined"); |
| 333 | + |
| 334 | + // mock the fetch |
| 335 | + global.fetch = fetchMock.sandbox().mock().post("/test/text-inputs", 500); |
| 336 | + |
| 337 | + send(component, (_, __, err) => { |
| 338 | + t.true(typeof el.attributes.disabled === "undefined"); |
| 339 | + fetchMock.reset(); |
| 340 | + t.end(); |
| 341 | + }); |
| 342 | +}); |
| 343 | + |
| 344 | +test.cb("event action loading class 500 reverts", (t) => { |
| 345 | + const html = ` |
| 346 | +<div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:checksum="GXzew3Km"> |
| 347 | + <button unicorn:click='test()' u:loading.class="loading-class"></button> |
| 348 | +</div>`; |
| 349 | + const component = getComponent(html); |
| 350 | + |
| 351 | + t.is(component.attachedEventTypes.length, 1); |
| 352 | + t.is(component.actionEvents.click.length, 1); |
| 353 | + |
| 354 | + const { el } = component.actionEvents.click[0].element; |
| 355 | + t.is(el.classList.length, 0); |
| 356 | + |
| 357 | + el.click(); |
| 358 | + t.is(el.classList.length, 1); |
| 359 | + t.is(el.classList[0], "loading-class"); |
| 360 | + |
| 361 | + // mock the fetch |
| 362 | + global.fetch = fetchMock.sandbox().mock().post("/test/text-inputs", 500); |
| 363 | + |
| 364 | + send(component, (_, __, err) => { |
| 365 | + t.is(el.classList.length, 0); |
| 366 | + fetchMock.reset(); |
| 367 | + t.end(); |
| 368 | + }); |
| 369 | +}); |
| 370 | + |
316 | 371 | test("event action loading remove class", (t) => { |
317 | 372 | const html = ` |
318 | 373 | <div unicorn:id="5jypjiyb" unicorn:name="text-inputs" unicorn:checksum="GXzew3Km"> |
|
0 commit comments