1 describe('JSON posting', {
2 'Should stringify and post the supplied data to a supplied URL': function () {
4 ko.utils.postJson('http://example.com/some/url', {myModel : {a : 1}}, { submitter : function(x) { submittedForm = x } });
6 value_of(submittedForm.action).should_be('http://example.com/some/url');
7 var input = submittedForm.childNodes[0];
8 value_of(input.tagName).should_be('INPUT');
9 value_of(input.name).should_be('myModel');
10 value_of(input.value).should_be('{"a":1}');
13 'Given an existing form, should take the URL from the form\'s \'action\' attribute': function() {
14 var existingForm = document.createElement("FORM");
15 existingForm.action = 'http://example.com/blah';
18 ko.utils.postJson(existingForm, {myModel : {a : 1}}, { submitter : function(x) { submittedForm = x } });
20 value_of(submittedForm.action).should_be('http://example.com/blah');
23 'Given an existing form, should include any requested field values from that form': function() {
24 var existingForm = document.createElement("FORM");
25 existingForm.innerHTML = '<input name="someField" value="myValue"/><input name="anotherField" value="unwantedValue"/>';
28 ko.utils.postJson(existingForm, {myModel : {a : 1}}, { includeFields : ['someField'], submitter : function(x) { submittedForm = x } });
30 value_of(ko.utils.getFormFields(submittedForm, 'someField')[0].value).should_be('myValue');
31 value_of(ko.utils.getFormFields(submittedForm, 'anotherField').length).should_be(0);
34 'Given an existing form, should include Rails and ASP.NET MVC auth tokens by default' : function() {
35 var existingForm = document.createElement("FORM");
36 existingForm.innerHTML = '<input name="__RequestVerificationToken_Lr4e" value="wantedval1"/>'
37 + '<input name="__RequestVe" value="unwantedval"/>'
38 + '<input name="authenticity_token" value="wantedval2"/>';
41 ko.utils.postJson(existingForm, {myModel : {a : 1}}, { submitter : function(x) { submittedForm = x } });
43 value_of(ko.utils.getFormFields(submittedForm, '__RequestVerificationToken_Lr4e')[0].value).should_be('wantedval1');
44 value_of(ko.utils.getFormFields(submittedForm, '__RequestVe').length).should_be(0);
45 value_of(ko.utils.getFormFields(submittedForm, 'authenticity_token')[0].value).should_be('wantedval2');