javascriptjqueryjasminejasmine-jquery

jasmine test run even before .click() is triggered


I am just playing with a basic ruby application which changes the html direction attribute upon clicking an button. Its working fine on the JQuery side but in my test case runs even when .click() is triggers. Here are my files.

display.html.erb

<p>
<%= button_tag 'Convert', id: 'test', remote: 'true' %>
</p>

Jquery: conversion.js

$(document).ready(function(){
  $('#test').on('click', setHtmlStyle);
});
var setHtmlStyle = function(){
var $style = $('html');
$style.attr('dir', $style.attr('dir') === 'rtl' ? 'ltr' : 'rtl');
};

Spec: conversion_spec.js

describe('#test', function (){
  beforeEach(function(){
    loadFixtures('conversion.html');
  });
  describe('Clicking test button', function(){
    it('should have an attribute', function(){
      $('#test').click();
      expect($('html').attr('dir')).toBe('rtl');
    });
  });

Fixture: conversion.html

<html>
<input type='button' name='Convert' id='test'>
</html>

Here is the error message when i run my tests:

Failure/Error: Expected 'ltr' to be 'rtl'.

Any help or suggestions would be truly appreciated. Thanks.


Solution

  • I had changed my code in order to make sure the .click() function is working.

    $(document).ready(function(){
      $(document).on('click', '#test', setHtmlStyle);
    });
    
    var setHtmlStyle = function(){
      var $style = $('html');
      $style.attr('dir', $style.attr('dir') === 'rtl' ? 'ltr' : 'rtl');
    };