Correct way to Spy on .blur()??

2,707 views
Skip to first unread message

Tim

unread,
Sep 2, 2011, 3:51:06 AM9/2/11
to Jasmine
Hi!
I have a form which validates input fields when someone leaves the
field without writing something. I'm trying to write a Jamsine test
for this but cant seem to get the hang of this. I think that maybe the
right way to go would be by Spying on the blur event? The code I woul
like to test is the following:

var logOnForm = $(".logOnForm");
$('input[type=text], [type=password]').blur(function () {
$(this).valid();
});

I've been on and of trying to construct the test. This is it's current
status, beware that this might be all wrong but I have changed it back
and forth so maybe once I was on the right track:)

describe('when leaving input without writing something', function () {
var input = $("<input type=text id=UserName value class=input-
validation-error>");
it("validates when text field looses focus", function () {
var form = $("<form/>");
form.append(input);
console.log(form);
spyOnEvent(form, 'blur');
form.blur();
expect(form).toHaveValue('input-validation-error');
});

});

I'm using jquery validate btw. Thaks in advance!!

Regards
Tim

Aaron McLeod

unread,
Sep 2, 2011, 7:26:05 AM9/2/11
to jasmi...@googlegroups.com
Try using $.fn as the object, and then blur as the function in the spyOn.


--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.




--
Aaron McLeod
http://agmprojects.com

Rajan Agaskar

unread,
Sep 2, 2011, 7:28:42 AM9/2/11
to jasmi...@googlegroups.com
This seems OK to me, but you probably want to trigger the blur event on the input rather than the form, as it's closer to what might happen in a browser (it also looks like your code attaches to the inputs, not the form, so the blur event you call at the form level -- a parent of the inputs -- will never get its children). 

Another minor thing: try not to assign values outside of it or beforeEach blocks (as you do here with var input). When you do this, those values end up being long-lived, and thus mutable between tests, which can lead to confusing errors from test pollution. 

Thanks for using Jasmine!

Rajan

On Fri, Sep 2, 2011 at 3:51 AM, Tim <tim.ja...@gmail.com> wrote:

Rajan Agaskar

unread,
Sep 2, 2011, 7:32:28 AM9/2/11
to jasmi...@googlegroups.com
Oops, I missed that you were spying on form.blur. You shouldn't need to do this since you're testing the result of your input behavior being called (you don't care about form.blur at all from what I can tell). 

As an aside, when I want to know that a particular behavior fires an event, usually what I do is create a spy to listen for that event. IE, if I want to know that the document gets a custom "hello:there" event, I might do the following: 

var helloThereSpy = jasmine.createSpy("helloThere")
$(document).bind("hello:there", helloThereSpy);
expect(helloThereSpy).not.toHaveBeenCalled();
$(document).trigger("hello:there")
expect(helloThereSpy).toHaveBeenCalled();

Tim

unread,
Sep 2, 2011, 7:52:14 AM9/2/11
to Jasmine
Ok, but were should I use $.fn, should it look like this then
spyOnEvent($.fn, blur());
Or?? I'm a bit confused.

Thanks

Aaron McLeod

unread,
Sep 2, 2011, 10:43:10 AM9/2/11
to jasmi...@googlegroups.com
You would need to take out the parenthesis when you pass the function to spyOn. Though I think Rajan seems to have a better idea here. He's setting up a custom event using jquery's functionality, and then triggering it manually to see if it fires. I think if you do the same, except trigger the blur, it might work.
Reply all
Reply to author
Forward
0 new messages