Comparing version 0.1.0 to 0.2.1
@@ -1,2 +0,2 @@ | ||
/*! ajaxable - 0.0.8 */ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.ajaxable=t():e.ajaxable=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}var o=n(1),i=r(o);e.exports=function(e,t){return new i["default"](e,t)}},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var u=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),a=n(2),f=r(a),c=function(e){function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";o(this,t);var r=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));if(!e)throw new Error("The element is empty");var s={responseType:"json"},u=n||{};for(var a in s)a in u||(u[a]=s[a]);r.els=r.parseEl(e),r.opts=u;for(var f=0;f<r.els.length;f++)r.bindForm(r.els[f]);return r}return s(t,e),u(t,[{key:"onStart",value:function(e){return this.on("start",e)}},{key:"onEnd",value:function(e){return this.on("end",e)}},{key:"onResponse",value:function(e){return this.on("response",e)}},{key:"onError",value:function(e){return this.on("error",e)}},{key:"submit",value:function(){for(var e=0;e<this.els.length;e++)this.els[e].dispatchEvent(new Event("submit"))}},{key:"parseEl",value:function(e){return"string"==typeof e&&(e=document.querySelectorAll(e)),e.length||(e=[e]),e}},{key:"bindForm",value:function(e){var t=this;this.checkForm(e);var n="submit",r=function(n){if(!e.checkValidity())throw new Error("Validation failed");n.preventDefault(),t.sendForm(e)};this.removeListeners(e,n),this.addListener(e,n,r)}},{key:"sendForm",value:function(e){var t=this,n=this.fetchData(e),r=new XMLHttpRequest;this._ar++;var o={el:e,req:r,activeRequests:this._ar};this.emit("start",o),r.addEventListener("progress",function(n){return t.emit("progress",n,e,r)}),r.addEventListener("load",function(e){var n="json"==t.opts.responseType,i="";try{i=n?JSON.parse(r.responseText):r.response}catch(s){return void t.emit("error",s,o)}t.emit("response",i,o)}),r.addEventListener("error",function(e){return t.emit("error",e,o)}),r.addEventListener("loadend",function(e){t._ar--,o.activeRequests=t._ar,t.emit("end",o)}),r.open(e.method,e.action),r.send(n)}},{key:"fetchData",value:function(e){this.checkForm(e);var t=new FormData(e);return t}},{key:"checkForm",value:function(e){if(!(e&&e instanceof HTMLFormElement))throw new Error("The element is not a valid form")}},{key:"addListener",value:function(e,t,n){e in this._eh||(this._eh[e]={}),t in this._eh[e]||(this._eh[e][t]=[]),this._eh[e][t].push(n),e.addEventListener(t,n)}},{key:"removeListeners",value:function(e,t){if(e in this._eh){var n=this._eh[e];if(t in n)for(var r=n[t],o=r.length;o--;){var i=r[o];e.removeEventListener(t,i)}}}}]),t}(f["default"]);c.prototype._eh={},c.prototype._ar=0,t["default"]=c},function(e,t){function n(){}n.prototype={on:function(e,t,n){var r=this.e||(this.e={});return(r[e]||(r[e]=[])).push({fn:t,ctx:n}),this},once:function(e,t,n){function r(){o.off(e,r),t.apply(n,arguments)}var o=this;return r._=t,this.on(e,r,n)},emit:function(e){var t=[].slice.call(arguments,1),n=((this.e||(this.e={}))[e]||[]).slice(),r=0,o=n.length;for(r;r<o;r++)n[r].fn.apply(n[r].ctx,t);return this},off:function(e,t){var n=this.e||(this.e={}),r=n[e],o=[];if(r&&t)for(var i=0,s=r.length;i<s;i++)r[i].fn!==t&&r[i].fn._!==t&&o.push(r[i]);return o.length?n[e]=o:delete n[e],this}},e.exports=n}])}); | ||
/*! ajaxable - 0.2.1 */ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.ajaxable=t():e.ajaxable=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}var o=n(1),i=r(o);e.exports=function(e,t){return new i["default"](e,t)}},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),u=n(2),f=r(u),c=function(e){function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";o(this,t);var r=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));if(!e)throw new Error("The element is empty");var s={responseType:"json",headers:{}},a=n||{};for(var u in s)u in a||(a[u]=s[u]);var f="X-Requested-With";""!=a.headers[f]&&(a.headers[f]="XMLHttpRequest"),r.els=r.parseEl(e),r.opts=a;for(var c=0;c<r.els.length;c++)r.bindForm(r.els[c]);return r}return s(t,e),a(t,[{key:"onStart",value:function(e){return this.on("start",e)}},{key:"onEnd",value:function(e){return this.on("end",e)}},{key:"onResponse",value:function(e){return this.on("response",e)}},{key:"onError",value:function(e){return this.on("error",e)}},{key:"submit",value:function(){for(var e=0;e<this.els.length;e++)this.els[e].dispatchEvent(new window.Event("submit"))}},{key:"parseEl",value:function(e){return"string"==typeof e&&(e=document.querySelectorAll(e)),e.length||(e=[e]),e}},{key:"bindForm",value:function(e){var t=this;this.checkForm(e);var n="submit",r=function(n){if(!e.checkValidity())throw new Error("Validation failed");n.preventDefault(),t.sendForm(e)};this.removeListeners(e,n),this.addListener(e,n,r)}},{key:"sendForm",value:function(e){var t=this,n=this.fetchData(e),r=new XMLHttpRequest,o=this.opts.headers;this._ar++;var i={el:e,req:r,activeRequests:this._ar,requestData:this.fetchFormData(n)};this.emit("start",i),r.addEventListener("progress",function(n){return t.emit("progress",n,e,r)}),r.addEventListener("load",function(e){var n="json"==t.opts.responseType,o="";try{o=n?JSON.parse(r.responseText):r.response}catch(s){return void t.emit("error",s,i)}t.emit("response",o,i)}),r.addEventListener("error",function(e){return t.emit("error",e,i)}),r.addEventListener("loadend",function(e){t._ar--,i.activeRequests=t._ar,t.emit("end",i)}),r.open(e.method,e.action);for(var s in o)r.setRequestHeader(s,o[s]);r.send(n)}},{key:"fetchData",value:function(e){this.checkForm(e);var t=new window.FormData(e);return t}},{key:"fetchFormData",value:function(e){var t={};if(e.entries){var n=!0,r=!1,o=void 0;try{for(var i,s=e.entries()[Symbol.iterator]();!(n=(i=s.next()).done);n=!0){var a=i.value;t[a[0]]=a[1]}}catch(u){r=!0,o=u}finally{try{!n&&s["return"]&&s["return"]()}finally{if(r)throw o}}}return t}},{key:"checkForm",value:function(e){if(!(e&&e instanceof window.HTMLFormElement))throw new Error("The element is not a valid form")}},{key:"addListener",value:function(e,t,n){e in this._eh||(this._eh[e]={}),t in this._eh[e]||(this._eh[e][t]=[]),this._eh[e][t].push(n),e.addEventListener(t,n)}},{key:"removeListeners",value:function(e,t){if(e in this._eh){var n=this._eh[e];if(t in n)for(var r=n[t],o=r.length;o--;){var i=r[o];e.removeEventListener(t,i)}}}}]),t}(f["default"]);c.prototype._eh={},c.prototype._ar=0,t["default"]=c},function(e,t){function n(){}n.prototype={on:function(e,t,n){var r=this.e||(this.e={});return(r[e]||(r[e]=[])).push({fn:t,ctx:n}),this},once:function(e,t,n){function r(){o.off(e,r),t.apply(n,arguments)}var o=this;return r._=t,this.on(e,r,n)},emit:function(e){var t=[].slice.call(arguments,1),n=((this.e||(this.e={}))[e]||[]).slice(),r=0,o=n.length;for(r;r<o;r++)n[r].fn.apply(n[r].ctx,t);return this},off:function(e,t){var n=this.e||(this.e={}),r=n[e],o=[];if(r&&t)for(var i=0,s=r.length;i<s;i++)r[i].fn!==t&&r[i].fn._!==t&&o.push(r[i]);return o.length?n[e]=o:delete n[e],this}},e.exports=n}])}); |
{ | ||
"name": "ajaxable", | ||
"version": "0.1.0", | ||
"version": "0.2.1", | ||
"description": "Make your form instantly ajaxable", | ||
@@ -29,7 +29,7 @@ "main": "dist/ajaxable.min.js", | ||
"lint": "eslint src", | ||
"test": "mocha --compilers js:babel-core/register --recursive ./test/*.js", | ||
"test:dev": "mocha --compilers js:babel-core/register --recursive -R min -w ./test/*.js", | ||
"build": "WEBPACK_ENV=prod && npm run test && webpack && npm version --no-git-tag-version patch", | ||
"build:doc": "documentation build src/index.js -o ./docs.md -f md", | ||
"cover": "babel-node ./node_modules/istanbul/lib/cli cover node_modules/.bin/_mocha -- --compilers js:babel-core/register --recursive -R spec ./test/*.js", | ||
"test": "mocha --compilers js:babel-core/register --require ./test/helper.js --recursive ./test/*.js", | ||
"test:dev": "npm test -- -R min -w", | ||
"build": "WEBPACK_ENV=prod && npm run test && npm run build:doc && npm version --no-git-tag-version patch && webpack", | ||
"build:doc": "documentation build src/index.js -o ./docs/API.md -f md", | ||
"cover": "babel-node ./node_modules/istanbul/lib/cli cover node_modules/.bin/_mocha -- --compilers js:babel-core/register --require ./test/helper.js --recursive -R spec ./test/*.js", | ||
"start": "WEBPACK_ENV=dev webpack-dev-server --progress --colors" | ||
@@ -45,6 +45,10 @@ }, | ||
"babel-preset-es2015": "^6.16.0", | ||
"coveralls": "^2.11.14", | ||
"documentation": "^4.0.0-beta11", | ||
"eslint": "^3.7.1", | ||
"expect": "^1.20.2", | ||
"istanbul": "^1.1.0-alpha.1", | ||
"jsdom": "^9.8.0", | ||
"mocha": "^3.1.1", | ||
"sinon": "^1.17.6", | ||
"webpack": "^1.13.2", | ||
@@ -51,0 +55,0 @@ "webpack-dev-server": "^1.16.2" |
104
README.md
# Ajaxable | ||
Make your form instantly ajaxable. This library simply takes the standard HTML form as an input and make it send requests via AJAX keeping HTML5 validations. | ||
[![Build Status](https://travis-ci.org/artf/ajaxable.svg?branch=master)](https://travis-ci.org/artf/ajaxable) | ||
[![Coverage Status](https://coveralls.io/repos/github/artf/ajaxable/badge.svg?branch=master)](https://coveralls.io/github/artf/ajaxable?branch=master) | ||
This library simply takes the standard HTML forms as an input and make them send requests via AJAX keeping HTML5 validations. | ||
## Installation | ||
Download the file from [here](https://cdn.rawgit.com/artf/ajaxable/master/dist/ajaxable.min.js) or get it directly from the `/dist` folder | ||
Download the file from [here](https://cdn.rawgit.com/artf/ajaxable/master/dist/ajaxable.min.js), via `npm i ajaxable` or get it directly from the `/dist` folder | ||
@@ -79,100 +82,5 @@ | ||
# constructor | ||
[API Reference here](./docs/API.md) | ||
Init the form by providing the element, it can be either HTML selector or the form element (HTMLFormElement). | ||
The options are optional and could contain: | ||
`responseType` - Define the response type, eg. `json`(default), `blob`, `arraybuffer`, leave empty if undefined | ||
**Parameters** | ||
- `el` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [HTMLFormElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement))** | ||
- `options` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** Options | ||
**Examples** | ||
```javascript | ||
ajaxable('form.ajaxable', { | ||
responseType: '', | ||
}); | ||
``` | ||
# onStart | ||
Bind a callback and execute it on start of each request | ||
The callback accepts parameters object as argument | ||
**Parameters** | ||
- `clb` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Callback function | ||
**Examples** | ||
```javascript | ||
ajaxable('...').onStart((params) => { | ||
// do stuff | ||
}) | ||
``` | ||
# onEnd | ||
Bind a callback and execute it on end of each request | ||
The callback accepts parameters object as argument | ||
**Parameters** | ||
- `clb` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Callback function | ||
**Examples** | ||
```javascript | ||
ajaxable('...').onEnd((params) => { | ||
// do stuff | ||
}) | ||
``` | ||
# onResponse | ||
Bind a callback and execute it on response of each request | ||
The callback accepts the response and parameters as arguments | ||
**Parameters** | ||
- `clb` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Callback function | ||
**Examples** | ||
```javascript | ||
ajaxable('...').onResponse((res, params) => { | ||
// do stuff | ||
}) | ||
``` | ||
# onError | ||
Bind a callback and execute it on error of each request | ||
The callback accepts the error and parameters as arguments | ||
**Parameters** | ||
- `clb` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Callback function | ||
**Examples** | ||
```javascript | ||
ajaxable('...').onError((err, params) => { | ||
// do stuff | ||
}) | ||
``` | ||
# submit | ||
Submit the request | ||
**Examples** | ||
```javascript | ||
ajaxable('...').submit(); | ||
``` | ||
## Testing | ||
@@ -179,0 +87,0 @@ |
@@ -7,9 +7,12 @@ import EventEmitter from 'tiny-emitter'; | ||
* Init the form by providing the element, it can be either HTML selector or the form element (HTMLFormElement). | ||
* The options are optional and could contain: | ||
* `responseType` - Define the response type, eg. `json`(default), `blob`, `arraybuffer`, leave empty if undefined | ||
* @param {string|HTMLFormElement} el | ||
* @param {Object} [options] Options | ||
* @param {string} [options.responseType] Define the response type, eg. `json`(default), `blob`, `arraybuffer`, leave empty if undefined | ||
* @param {Object} [options.headers] Define custom headers | ||
* @example | ||
* ajaxable('form.ajaxable', { | ||
* responseType: '', | ||
* headers: { | ||
* 'Content-Type': 'text/html; charset=UTF-8' | ||
* } | ||
* }); | ||
@@ -25,6 +28,7 @@ */ | ||
responseType: 'json', | ||
headers: {}, | ||
}; | ||
let opts = options || {}; | ||
for (var name in defaults) { | ||
for (let name in defaults) { | ||
if (!(name in opts)) | ||
@@ -34,2 +38,7 @@ opts[name] = defaults[name]; | ||
// Set default X-Requested-With if not setted | ||
const reqHead = 'X-Requested-With'; | ||
if(opts.headers[reqHead] != '') | ||
opts.headers[reqHead] = 'XMLHttpRequest'; | ||
this.els = this.parseEl(el); | ||
@@ -102,3 +111,3 @@ this.opts = opts; | ||
for (let i = 0; i < this.els.length; i++) { | ||
this.els[i].dispatchEvent(new Event('submit')); | ||
this.els[i].dispatchEvent(new window.Event('submit')); | ||
} | ||
@@ -158,4 +167,10 @@ } | ||
let req = new XMLHttpRequest(); | ||
let headers = this.opts.headers; | ||
this._ar++; | ||
let params = {el, req, activeRequests: this._ar}; | ||
let params = { | ||
el, | ||
req, | ||
activeRequests: this._ar, | ||
requestData: this.fetchFormData(formData) | ||
}; | ||
this.emit('start', params); | ||
@@ -185,2 +200,8 @@ req.addEventListener("progress", (e) => | ||
req.open(el.method, el.action); | ||
// Set headers | ||
for (let head in headers){ | ||
req.setRequestHeader(head, headers[head]); | ||
} | ||
req.send(formData); | ||
@@ -197,3 +218,3 @@ } | ||
this.checkForm(el); | ||
let formData = new FormData(el); | ||
let formData = new window.FormData(el); | ||
return formData; | ||
@@ -203,2 +224,18 @@ } | ||
/** | ||
* Fetch data from the FormData object | ||
* @param {FormData} fd | ||
* @return {Object} | ||
* @private | ||
*/ | ||
fetchFormData(fd) { | ||
let obj = {}; | ||
if(fd.entries){ | ||
for(let pair of fd.entries()) { | ||
obj[pair[0]] = pair[1]; | ||
} | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Check if the element is a valid form node | ||
@@ -209,3 +246,3 @@ * @param {HTMLFormElement} el | ||
checkForm(el) { | ||
if(!el || !(el instanceof HTMLFormElement)){ | ||
if(!el || !(el instanceof window.HTMLFormElement)){ | ||
throw new Error('The element is not a valid form'); | ||
@@ -242,3 +279,3 @@ } | ||
let eventHandlers = handlers[event]; | ||
for(var i = eventHandlers.length; i--;) { | ||
for(let i = eventHandlers.length; i--;) { | ||
let handler = eventHandlers[i]; | ||
@@ -245,0 +282,0 @@ node.removeEventListener(event, handler); |
@@ -1,4 +0,199 @@ | ||
import {expect} from 'expect'; | ||
import expect from 'expect'; | ||
import sinon from 'sinon'; | ||
import ajaxable from './../src'; | ||
describe('Ajaxable', () => { | ||
}) | ||
let obj; | ||
let form; | ||
let d = document; | ||
beforeEach(() => { | ||
form = `<form id="form1" class="forms"></form> | ||
<form id="form2" class="forms"></form>`; | ||
d.body.innerHTML = form; | ||
}); | ||
it('Throws error without element', () => { | ||
expect(() => { | ||
obj = ajaxable(); | ||
}).toThrow(); | ||
}); | ||
it('Init with string, single element', () => { | ||
const el = ajaxable('#form1').els; | ||
expect(el.length).toEqual(1); | ||
}); | ||
it('Init with string, multiple elements', () => { | ||
const el = ajaxable('.forms').els; | ||
expect(el.length).toEqual(2); | ||
}); | ||
it('Init with the dom', () => { | ||
const el = ajaxable(d.getElementById('form1')).els; | ||
expect(el.length).toEqual(1); | ||
}); | ||
it('Throw on wrong element with checkForm', () => { | ||
obj = ajaxable('#form1'); | ||
expect(() => { | ||
obj.checkForm(); | ||
}).toThrow(); | ||
}); | ||
describe('Ajaxable methods', () => { | ||
let formEl; | ||
let formEl2; | ||
let requests; | ||
let xhr; | ||
beforeEach(() => { | ||
form = `<form id="formel1" class="forms"> | ||
<button id="send-data"></button> | ||
</form> | ||
<form id="formel2" class="forms"> | ||
<input name="name2" value="value1"/> | ||
<button id="send-data2"></button> | ||
</form>`; | ||
d.body.innerHTML = form; | ||
formEl = d.getElementById('formel1'); | ||
formEl2 = d.getElementById('formel2'); | ||
obj = ajaxable('#formel2'); | ||
xhr = sinon.useFakeXMLHttpRequest(); | ||
requests = []; | ||
xhr.onCreate = function(xhr) { | ||
requests.push(xhr); | ||
}; | ||
}); | ||
afterEach(function() { | ||
xhr.restore(); | ||
}); | ||
it('Adds event listener correctly', (done) => { | ||
obj.addListener(formEl, 'submit', (e) => { | ||
done(); | ||
}); | ||
d.getElementById('send-data').click(); | ||
}); | ||
it('Removes event listener correctly', (done) => { | ||
obj.addListener(formEl, 'submit', (e) => { | ||
done(new Error('Not expected to submit')); | ||
}); | ||
obj.removeListeners(formEl, 'submit'); | ||
d.getElementById('send-data').onclick = () => { | ||
done(); | ||
}; | ||
d.getElementById('send-data').click(); | ||
}); | ||
it('Bind form correctly', (done) => { | ||
obj.sendForm = () => {done()}; | ||
formEl2.checkValidity = () => true; | ||
obj.bindForm(formEl2); | ||
obj.submit(); | ||
}); | ||
it('Fetch data from the FormData', () => { | ||
let fd = new window.FormData(); | ||
obj.fetchFormData(fd); | ||
fd.entries = () => [['test', 'value']]; | ||
let res = obj.fetchFormData(fd); | ||
expect(res).toEqual({test:'value'}); | ||
}); | ||
it('Bind form correctly, with fail on checkValidity', (done) => { | ||
obj.sendForm = () => {done()}; | ||
formEl2.checkValidity = () => false; | ||
obj.bindForm(formEl2); | ||
obj.submit(); | ||
done(); | ||
}); | ||
it('Triggers onStart on sendForm', (done) => { | ||
let formEl = d.getElementById('formel2'); | ||
obj.onStart((params) => { | ||
expect(params.el.id).toEqual(formEl.id); | ||
done(); | ||
}); | ||
obj.sendForm(formEl); | ||
}); | ||
it('Triggers onEnd on sendForm', (done) => { | ||
let formEl = d.getElementById('formel2'); | ||
obj.onEnd((params) => { | ||
expect(params.el.id).toEqual(formEl.id); | ||
done(); | ||
}); | ||
obj.sendForm(formEl); | ||
requests[0].respond(); | ||
}); | ||
it('Triggers onResponse on sendForm, auto parse JSON', (done) => { | ||
const data = {test: 'value'}; | ||
obj.onResponse((res, params) => { | ||
expect(res).toEqual(data); | ||
done(); | ||
}); | ||
obj.sendForm(formEl2); | ||
requests[0].respond(200, | ||
{'Content-Type': 'text/json'}, | ||
JSON.stringify(data)); | ||
}); | ||
it('Triggers onResponse on sendForm, do not parse JSON', (done) => { | ||
obj = ajaxable('#formel2', {responseType: ''}); | ||
const data = {test: 'value'}; | ||
obj.onResponse((res, params) => { | ||
expect(res).toEqual(JSON.stringify(data)); | ||
done(); | ||
}); | ||
obj.sendForm(formEl2); | ||
requests[0].respond(200, | ||
{'Content-Type': 'text/json'}, | ||
JSON.stringify(data)); | ||
}); | ||
it('Triggers onError on sendForm, with wrong JSON', (done) => { | ||
const data = {test: 'value'}; | ||
obj.onError((err, params) => { | ||
expect(err).toExist(); | ||
done(); | ||
}); | ||
obj.sendForm(formEl2); | ||
requests[0].respond(200, | ||
{'Content-Type': 'text/json'}, | ||
JSON.stringify(data)+'test'); | ||
}); | ||
it('Triggers onError on sendForm', (done) => { | ||
const data = {test: 'value'}; | ||
obj.onError((err, params) => { | ||
expect(err).toExist(); | ||
done(); | ||
}); | ||
obj.sendForm(formEl2); | ||
requests[0].respond(); | ||
}); | ||
it('Sends X-Requested-With in the headers', () => { | ||
obj.sendForm(formEl2); | ||
const reqHead = requests[0].requestHeaders; | ||
expect(reqHead['X-Requested-With']).toExist(); | ||
expect(reqHead['X-Requested-With']).toEqual('XMLHttpRequest'); | ||
}); | ||
it('Sends empty X-Requested-With headers', () => { | ||
obj = ajaxable('#formel2', { | ||
headers: {'X-Requested-With': ''} | ||
}); | ||
obj.sendForm(formEl2); | ||
const reqHead = requests[0].requestHeaders; | ||
expect(reqHead['X-Requested-With']).toNotExist(); | ||
}); | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26432
12
480
14
106