Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2676 - Tests for v2 to prevent regression of issue from v1 #2829

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/core/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,34 @@ describe('Core htmx AJAX Tests', function() {
values.should.deep.equal({ c1: ['cb1', 'cb3'], c2: 'cb5', c3: 'cb6' })
})

it('properly handles radio inputs', function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is essentially just a cut-down clone of the function above which tests checkboxes in detail, to cover the shouldInclude logic which applies to both input types:

if (elt.type === 'checkbox' || elt.type === 'radio') { return elt.checked }

var values
this.server.respondWith('Post', '/test', function(xhr) {
values = getParameters(xhr)
xhr.respond(204, {}, '')
})

var form = make('<form hx-post="/test" hx-trigger="click">' +
'<div role="radiogroup">' +
'<input id="rb1" name="r1" value="rb1" type="radio">' +
'<input id="rb2" name="r1" value="rb2" type="radio">' +
'<input id="rb3" name="r1" value="rb3" type="radio">' +
'<input id="rb4" name="r2" value="rb4" type="radio">' +
'<input id="rb5" name="r2" value="rb5" type="radio">' +
'<input id="rb6" name="r3" value="rb6" type="radio">' +
'</div>' +
'</form>')

form.click()
this.server.respond()
values.should.deep.equal({})

byId('rb1').checked = true
form.click()
this.server.respond()
values.should.deep.equal({ r1: 'rb1' })
})

it('text nodes dont screw up settling via variable capture', function() {
this.server.respondWith('GET', '/test', "<div id='d1' hx-trigger='click consume' hx-get='/test2'></div>fooo")
this.server.respondWith('GET', '/test2', 'clicked')
Expand Down
36 changes: 36 additions & 0 deletions test/core/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,42 @@ describe('Core htmx client side validation tests', function() {
form.textContent.should.equal('Clicked!')
})

it('Custom validation error prevents request for unticked checkboxes', function() {
this.server.respondWith('POST', '/test', 'Clicked!')

var form = make('<form hx-post="/test" hx-trigger="click">' +
'No Request' +
'<input id="i1" name="i1" type="checkbox">' +
'</form>')
byId('i1').setCustomValidity('Nope')
form.textContent.should.equal('No Request')
form.click()
this.server.respond()
form.textContent.should.equal('No Request')
byId('i1').setCustomValidity('')
form.click()
this.server.respond()
form.textContent.should.equal('Clicked!')
})

it('Custom validation error prevents request for unselected radiogroups', function() {
this.server.respondWith('POST', '/test', 'Clicked!')

var form = make('<form hx-post="/test" hx-trigger="click">' +
'No Request' +
'<input id="i1" name="i1" type="radio">' +
'</form>')
byId('i1').setCustomValidity('Nope')
form.textContent.should.equal('No Request')
form.click()
this.server.respond()
form.textContent.should.equal('No Request')
byId('i1').setCustomValidity('')
form.click()
this.server.respond()
form.textContent.should.equal('Clicked!')
})

Copy link
Contributor Author

@ehenighan ehenighan Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two functions are essentially clones of the previous function but applied specifically to the input types with special handling in shouldInclude (if (elt.type === 'checkbox' || elt.type === 'radio') { return elt.checked }) to ensure that custom validation isn't broken for unchecked items again in the future

it('hyperscript validation error prevents request', function() {
this.server.respondWith('POST', '/test', 'Clicked!')

Expand Down