Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test a form submit in Jasmine?

I have a form that does some extensive Javascript stuff before finally POSTing to it's ACTION URL. I am writing some Jasmine unit tests and want to make sure the Javascript stuff happens when the form is submitted. However, I definitely don't want the page to go to the ACTION URL while I am unit testing.

I saw what seemed like a good suggestion here: http://groups.google.com/group/jasmine-js/browse_thread/thread/a010eced8db17c1a?pli=1

...but, as I am fairly new to Jasmine, I am unsure how to implement it and cannot find any pertinent examples on the web. Does anyone have some sample code I could look at that would accomplish what I need?

Thanks!

like image 439
gcdev Avatar asked Sep 14 '11 14:09

gcdev


People also ask

How do you test a jasmine form?

log('it happened'); value = document. querySelector('input'). value; return false; }); Also, if you want to keep the setTimeout , you will need to use the it 's optional single argument that should be called when the async work is complete, and move your expect inside the setTimeout function.

Which function is used to write a test suite in Jasmine?

It's used to test a specific behavior of the JavaScript code that's usually encapsulated by an object/class or a function. It's created using the Jasmine global function describe() that takes two parameters, the title of the test suite and a function that implements the actual code of the test suite.

Is Jasmine a testing tool?

Jasmine is an open-source testing framework for JavaScript. It aims to run on any JavaScript-enabled platform, to not intrude on the application nor the IDE, and to have easy-to-read syntax. It is heavily influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.

Why is Jasmine tested?

Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.


2 Answers

Maybe this code snippet can help you...

it('calls ajax post on export button click', function() {
  view.render();
  var form = $('#export_images_xml_form');
  var submitCallback = jasmine.createSpy().andReturn(false);
  form.submit(submitCallback);

  $('#export_images_xml_button').click();

  expect(form.attr('action')).toEqual('/export');
  expect($('#export_images_xml_form input').attr('value')).toEqual('22,33,44');
  expect(submitCallback).toHaveBeenCalled();
});

What I do is basically stopping every submit for given form returning false in the associated callback (see submitCallback behavior).

Then I can also test callback has been called...

Hope it helps!

like image 76
peekaboo Avatar answered Sep 20 '22 23:09

peekaboo


If you don't want to test that submit was called, you don't need to create a spy. With plain jQuery, you can also attain the effect of not submitting the form. I put the following code outside of all tests, that way it applies to all tests within the file.

$('form').on('submit', function() {
    return false;
});
like image 32
krillgar Avatar answered Sep 16 '22 23:09

krillgar