summaryrefslogtreecommitdiffstatshomepage
path: root/www/static/test/ext/disable-element.js
blob: 58e5a7324775d6c5847c8db0608b6e39bfb28e36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
describe("disable-element extension", function() {
    beforeEach(function () {
        this.server = makeServer();
        clearWorkArea();
    });
    afterEach(function () {
        this.server.restore();
        clearWorkArea();
    });

    it('disables the triggering element during htmx request', function () {
        // GIVEN:
        // - A button triggering an htmx request with disable-element extension
        // - The button is enabled
        this.server.respondWith("GET", "/test", function (xhr) {
            xhr.respond(200, {})
        });
        var btn = make('<button hx-get="/test" hx-ext="disable-element" hx-disable-element="self">Click Me!</button>')
        btn.disabled.should.equal(false);

        // WHEN clicking
        btn.click();

        // THEN it's disabled
        btn.disabled.should.equal(true);

        // WHEN server response has arrived
        this.server.respond();

        // THEN it's re-enabled
        btn.disabled.should.equal(false);
    });

    it('disables the designated element during htmx request', function () {
        // GIVEN:
        // - A button triggering an htmx request with disable-element extension
        // - Another button that needs to be disabled during the htmx request
        // - Both buttons are enabled
        this.server.respondWith("GET", "/test", function (xhr) {
            xhr.respond(200, {})
        });
        var btn = make('<button hx-get="/test" hx-ext="disable-element" hx-disable-element="#should-be-disabled">Click Me!</button>')
        var btn2 = make('<button id="should-be-disabled">Should be disabled</button>')
        btn.disabled.should.equal(false);
        btn2.disabled.should.equal(false);

        // WHEN clicking
        btn.click();

        // THEN it's not disabled, but the other one is
        btn.disabled.should.equal(false);
        btn2.disabled.should.equal(true);

        // WHEN server response has arrived
        this.server.respond();

        // THEN both buttons are back enabled
        btn.disabled.should.equal(false);
        btn2.disabled.should.equal(false);
    });

    it('disables multiple elements during htmx request', function () {
        this.server.respondWith("GET", "/test", function (xhr) {
            xhr.respond(200, {})
        });
        var btn = make('<button class="should-be-disabled" hx-get="/test" hx-ext="disable-element" hx-disable-element=".should-be-disabled">Click Me!</button>')
        var btn2 = make('<button class="should-be-disabled">Should be disabled</button>')
        var btn3 = make('<button>Should NOT be disabled</button>')
        btn.disabled.should.equal(false);
        btn2.disabled.should.equal(false);
        btn3.disabled.should.equal(false);

        // WHEN clicking
        btn.click();

        // THEN the first two are disabled, but the last one isn't
        btn.disabled.should.equal(true);
        btn2.disabled.should.equal(true);
        btn3.disabled.should.equal(false);

        // WHEN server response has arrived
        this.server.respond();

        // THEN all buttons are back enabled
        btn.disabled.should.equal(false);
        btn2.disabled.should.equal(false);
        btn3.disabled.should.equal(false);
    });

});