forked from knockout/knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonPostingBehaviors.js
61 lines (49 loc) · 3.12 KB
/
jsonPostingBehaviors.js
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
describe('JSON posting', function() {
it('Should stringify and post the supplied data to a supplied URL', function () {
var submittedForm;
ko.utils.postJson('http://example.com/some/url', {myModel : {a : 1}}, { submitter : function(x) { submittedForm = x } });
expect(submittedForm.action).toEqual('http://example.com/some/url');
var input = submittedForm.childNodes[0];
expect(input.tagName).toEqual('INPUT');
expect(input.name).toEqual('myModel');
expect(input.value).toEqual('{"a":1}');
});
it('Given an existing form, should take the URL from the form\'s \'action\' attribute', function() {
var existingForm = document.createElement("FORM");
existingForm.action = 'http://example.com/blah';
var submittedForm;
ko.utils.postJson(existingForm, {myModel : {a : 1}}, { submitter : function(x) { submittedForm = x } });
expect(submittedForm.action).toEqual('http://example.com/blah');
});
it('Given an existing form, should include any requested field values from that form', function() {
var existingForm = document.createElement("FORM");
existingForm.innerHTML = '<input name="someField" value="myValue"/><input name="anotherField" value="unwantedValue"/>';
var submittedForm;
ko.utils.postJson(existingForm, {myModel : {a : 1}}, { includeFields : ['someField'], submitter : function(x) { submittedForm = x } });
expect(ko.utils.getFormFields(submittedForm, 'someField')[0].value).toEqual('myValue');
expect(ko.utils.getFormFields(submittedForm, 'anotherField').length).toEqual(0);
});
it('Given an existing form, should include Rails and ASP.NET MVC auth tokens by default', function() {
var existingForm = document.createElement("FORM");
existingForm.innerHTML = '<input name="__RequestVerificationToken_Lr4e" value="wantedval1"/>'
+ '<input name="__RequestVe" value="unwantedval"/>'
+ '<input name="authenticity_token" value="wantedval2"/>';
var submittedForm;
ko.utils.postJson(existingForm, {myModel : {a : 1}}, { submitter : function(x) { submittedForm = x } });
expect(ko.utils.getFormFields(submittedForm, '__RequestVerificationToken_Lr4e')[0].value).toEqual('wantedval1');
expect(ko.utils.getFormFields(submittedForm, '__RequestVe').length).toEqual(0);
expect(ko.utils.getFormFields(submittedForm, 'authenticity_token')[0].value).toEqual('wantedval2');
});
it('Should not truncate large values', function() {
// Represents https://github.com/knockout/knockout/issues/1252
var longString = new Array(1000000).join('a'),
longStringJson = JSON.stringify(longString),
submittedForm;
ko.utils.postJson("http://example.com", { longString: longString }, {
submitter: function(x) { submittedForm = x; }
});
var submittedValue = ko.utils.getFormFields(submittedForm, 'longString')[0].value;
expect(submittedValue.length).toEqual(longStringJson.length);
expect(submittedValue).toEqual(longStringJson);
});
});