Jsdom: Posting a form.

Created on 6 Jan 2011  ·  6Comments  ·  Source: jsdom/jsdom

Having trouble figuring out how to post a form using jsdom and jquery. is this possible?

var form = '

';
$(form).appendTo('body');
$('form').submit();

The above doesn't work for me, should it, or am I doing something wrong?

feature

Most helpful comment

It looks like form submission is partially implemented. It dispatches a HTMLEvent but does not support the submit method.

Minimal reproduction

As a Mocha + Chai test:

import {JSDOM} from 'jsdom'
import {assert} from 'chai'
import sinon from 'sinon'

describe('JSDOM Form', () => {

    it('submit form with button', () => {
        const dom = new JSDOM(`
  <form id="my-form">
    <button type="submit" id="submit-button">Submit Button</button>
  </form>
`, {})
        const listener = sinon.stub()
        dom.window.document.getElementById('my-form').addEventListener('submit', listener)

        dom.window.document.getElementById('submit-button').click()

        assert.isTrue(listener.called)
    })

})

What is expected?

Submit works without any errors.

What happens?

Test succeeds but returns this error:

Error: Not implemented: HTMLFormElement.prototype.submit

All 6 comments

not yet, there are no default actions at this time.

This is probably much easier to implement now that I've implemented the form data algorithm (in FormData-impl.js). However the navigation after the form submission would of course still not work.

After form is submitted, how to read the response body? For example resourceLoader did not trigger anything after a submitted form in a iframe.

Form submission is not currently implemented, so it is not possible.

Any changes with this?

It looks like form submission is partially implemented. It dispatches a HTMLEvent but does not support the submit method.

Minimal reproduction

As a Mocha + Chai test:

import {JSDOM} from 'jsdom'
import {assert} from 'chai'
import sinon from 'sinon'

describe('JSDOM Form', () => {

    it('submit form with button', () => {
        const dom = new JSDOM(`
  <form id="my-form">
    <button type="submit" id="submit-button">Submit Button</button>
  </form>
`, {})
        const listener = sinon.stub()
        dom.window.document.getElementById('my-form').addEventListener('submit', listener)

        dom.window.document.getElementById('submit-button').click()

        assert.isTrue(listener.called)
    })

})

What is expected?

Submit works without any errors.

What happens?

Test succeeds but returns this error:

Error: Not implemented: HTMLFormElement.prototype.submit

Was this page helpful?
0 / 5 - 0 ratings