summaryrefslogtreecommitdiffstatshomepage
path: root/test/attributes/hx-on-wildcard.js
blob: 941a29716d1a2657864527d422e9e5e001686a44 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
describe('hx-on:* attribute', function() {
  beforeEach(function() {
    this.server = makeServer()
    clearWorkArea()
  })
  afterEach(function() {
    this.server.restore()
    clearWorkArea()
  })

  it('can handle basic events w/ no other attributes', function() {
    var btn = make("<button hx-on:click='window.foo = true'>Foo</button>")
    btn.click()
    window.foo.should.equal(true)
    delete window.foo
  })

  it('can use dashes rather than colons', function() {
    var btn = make("<button hx-on-click='window.foo = true'>Foo</button>")
    btn.click()
    window.foo.should.equal(true)
    delete window.foo
  })

  it('can modify a parameter via htmx:configRequest', function() {
    this.server.respondWith('POST', '/test', function(xhr) {
      var params = parseParams(xhr.requestBody)
      xhr.respond(200, {}, params.foo)
    })
    var btn = make("<button hx-on:htmx:config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>")
    btn.click()
    this.server.respond()
    btn.innerText.should.equal('bar')
  })

  it('can modify a parameter via htmx:configRequest with dashes', function() {
    this.server.respondWith('POST', '/test', function(xhr) {
      var params = parseParams(xhr.requestBody)
      xhr.respond(200, {}, params.foo)
    })
    var btn = make("<button hx-on-htmx-config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>")
    btn.click()
    this.server.respond()
    btn.innerText.should.equal('bar')
  })

  it('expands :: shorthand into htmx:', function() {
    this.server.respondWith('POST', '/test', function(xhr) {
      var params = parseParams(xhr.requestBody)
      xhr.respond(200, {}, params.foo)
    })
    var btn = make("<button hx-on::config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>")
    btn.click()
    this.server.respond()
    btn.innerText.should.equal('bar')
  })

  it('expands -- shorthand into htmx:', function() {
    this.server.respondWith('POST', '/test', function(xhr) {
      var params = parseParams(xhr.requestBody)
      xhr.respond(200, {}, params.foo)
    })
    var btn = make("<button hx-on--config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>")
    btn.click()
    this.server.respond()
    btn.innerText.should.equal('bar')
  })

  it('can cancel an event via preventDefault for htmx:config-request', function() {
    this.server.respondWith('POST', '/test', function(xhr) {
      xhr.respond(200, {}, '<button>Bar</button>')
    })
    var btn = make("<button hx-on:htmx:config-request='event.preventDefault()' hx-post='/test' hx-swap='outerHTML'>Foo</button>")
    btn.click()
    this.server.respond()
    btn.innerText.should.equal('Foo')
  })

  it('can respond to data-hx-on', function() {
    this.server.respondWith('POST', '/test', function(xhr) {
      var params = parseParams(xhr.requestBody)
      xhr.respond(200, {}, params.foo)
    })
    var btn = make("<button data-hx-on:htmx:config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>")
    btn.click()
    this.server.respond()
    btn.innerText.should.equal('bar')
  })

  it('has the this symbol set to the element', function() {
    this.server.respondWith('POST', '/test', function(xhr) {
      xhr.respond(200, {}, 'foo')
    })
    var btn = make("<button hx-on:htmx:config-request='window.elt = this' hx-post='/test'>Foo</button>")
    btn.click()
    this.server.respond()
    btn.innerText.should.equal('foo')
    btn.should.equal(window.elt)
    delete window.elt
  })

  it('can handle multi-line JSON', function() {
    this.server.respondWith('POST', '/test', function(xhr) {
      xhr.respond(200, {}, 'foo')
    })
    var btn = make("<button hx-on:htmx:config-request='window.elt = {foo: true,\n" +
            "                                                             bar: false}' hx-post='/test'>Foo</button>")
    btn.click()
    this.server.respond()
    btn.innerText.should.equal('foo')
    var obj = { foo: true, bar: false }
    obj.should.deep.equal(window.elt)
    delete window.elt
  })

  it('can handle multiple event handlers in the presence of multi-line JSON', function() {
    this.server.respondWith('POST', '/test', function(xhr) {
      xhr.respond(200, {}, 'foo')
    })
    var btn = make("<button hx-on:htmx:config-request='window.elt = {foo: true,\n" +
            "                                                             bar: false}\n'" +
            " hx-on:htmx:after-request='window.foo = true'" +
            " hx-post='/test'>Foo</button>")
    btn.click()
    this.server.respond()
    btn.innerText.should.equal('foo')

    var obj = { foo: true, bar: false }
    obj.should.deep.equal(window.elt)
    delete window.elt

    window.foo.should.equal(true)
    delete window.foo
  })

  it('de-initializes hx-on-* content properly', function() {
    window.tempCount = 0
    this.server.respondWith('POST', '/test', function(xhr) {
      xhr.respond(200, {}, "<button id='foo' hx-on:click=\"window.tempCount++;\">increment</button>")
    })
    var div = make("<div hx-post='/test'>Foo</div>")

    // get response
    div.click()
    this.server.respond()

    // click button
    byId('foo').click()
    window.tempCount.should.equal(1)

    // get second response
    div.click()
    this.server.respond()

    // click button again
    byId('foo').click()
    window.tempCount.should.equal(2)

    delete window.tempCount
  })

  it('is not evaluated when allowEval is false', function() {
    var calledEvent = false
    var handler = htmx.on('htmx:evalDisallowedError', function() {
      calledEvent = true
    })
    htmx.config.allowEval = false
    try {
      var btn = make("<button hx-on:click='window.foo = true'>Foo</button>")
      btn.click()
      should.not.exist(window.foo)
    } finally {
      htmx.config.allowEval = true
      htmx.off('htmx:evalDisallowedError', handler)
      delete window.foo
    }
    calledEvent.should.equal(true)
  })

  it('can handle being swapped using innerHTML', function() {
    this.server.respondWith('GET', '/test', function(xhr) {
      xhr.respond(200, {}, '<button id="bar" hx-on:click="window.bar = true">Bar</button>')
    })

    make(
      '<div>' +
            '<button id="swap" hx-get="/test" hx-target="#baz" hx-swap="innerHTML">Swap</button>' +
            '<div id="baz"><button id="foo" hx-on:click="window.foo = true">Foo</button></div>' +
            '</div>'
    )

    var fooBtn = byId('foo')
    fooBtn.click()
    window.foo.should.equal(true)

    var swapBtn = byId('swap')
    swapBtn.click()
    this.server.respond()

    var barBtn = byId('bar')
    barBtn.click()
    window.bar.should.equal(true)

    delete window.foo
    delete window.bar
  })

  it('cleans up all handlers when the DOM updates', function() {
    // setup
    window.foo = 0
    window.bar = 0
    var div = make("<div hx-on:increment-foo='window.foo++' hx-on:increment-bar='window.bar++'>Foo</div>")
    make('<div>Another Div</div>') // sole purpose is to update the DOM

    // check there is just one handler against each event
    htmx.trigger(div, 'increment-foo')
    htmx.trigger(div, 'increment-bar')
    window.foo.should.equal(1)
    window.bar.should.equal(1)

    // teardown
    delete window.foo
    delete window.bar
  })
})