|
| 1 | +import {requestSubmit, apply, isPolyfilled, isSupported} from '../lib/form-requestsubmit.js' |
| 2 | + |
| 3 | +describe('requestSubmit', () => { |
| 4 | + let form |
| 5 | + beforeEach(() => { |
| 6 | + form = document.createElement('form') |
| 7 | + document.body.append(form) |
| 8 | + }) |
| 9 | + afterEach(() => { |
| 10 | + form.remove() |
| 11 | + }) |
| 12 | + |
| 13 | + it('has standard isSupported, isPolyfilled, apply API', () => { |
| 14 | + expect(isSupported).to.be.a('function') |
| 15 | + expect(isPolyfilled).to.be.a('function') |
| 16 | + expect(apply).to.be.a('function') |
| 17 | + expect(isSupported()).to.be.a('boolean') |
| 18 | + expect(isPolyfilled()).to.equal(false) |
| 19 | + }) |
| 20 | + |
| 21 | + it('does not dispatch or submit for invalid forms', () => { |
| 22 | + const input = document.createElement('input') |
| 23 | + input.required = true |
| 24 | + form.append(input) |
| 25 | + let called = false |
| 26 | + form.addEventListener('submit', () => { |
| 27 | + called = true |
| 28 | + }) |
| 29 | + requestSubmit.call(form) |
| 30 | + expect(called).to.equal(false) |
| 31 | + }) |
| 32 | + |
| 33 | + it('dispatches submit event', () => { |
| 34 | + const input = document.createElement('input') |
| 35 | + form.append(input) |
| 36 | + let called = false |
| 37 | + form.addEventListener('submit', event => { |
| 38 | + called = true |
| 39 | + event.stopPropagation() |
| 40 | + }) |
| 41 | + requestSubmit.call(form) |
| 42 | + expect(called).to.equal(true) |
| 43 | + }) |
| 44 | + |
| 45 | + it('passes submitter in event', () => { |
| 46 | + const input = document.createElement('input') |
| 47 | + input.type = 'button' |
| 48 | + form.append(input) |
| 49 | + let submitter = null |
| 50 | + form.addEventListener('submit', event => { |
| 51 | + submitter = event.submitter |
| 52 | + event.stopPropagation() |
| 53 | + }) |
| 54 | + requestSubmit.call(form, input) |
| 55 | + expect(submitter).to.equal(input) |
| 56 | + }) |
| 57 | + |
| 58 | + it('includes the input value in FormData', () => { |
| 59 | + const input = document.createElement('input') |
| 60 | + input.type = 'button' |
| 61 | + input.name = 'foo' |
| 62 | + input.value = '1' |
| 63 | + form.append(input) |
| 64 | + let formdata = null |
| 65 | + form.addEventListener('submit', event => { |
| 66 | + formdata = new FormData(form) |
| 67 | + event.stopPropagation() |
| 68 | + }) |
| 69 | + requestSubmit.call(form, input) |
| 70 | + expect(formdata.get('foo')).to.equal('1') |
| 71 | + expect(Array.from(form.querySelectorAll('input')).length).to.equal(1) |
| 72 | + }) |
| 73 | +}) |
0 commit comments