Jasmine Unit Test Case For Dynamic Character Count
Can anyone give me example of writing test case for checking if the function within the keyup event is called in jquery and jasmine. I am fairly new to jquery and jasmin
Solution 1:
Ok, I'm assuming you are running your test directly in the browser, right? And by your code, I'm assuming that updateChars
function is global so it is attached to window
.
Saying that, what you need is a spy
, in jasmine
we use the function spyOn
, here is an example:
beforeEach(function() {
//here we setup the "spy"spyOn(window, 'updateChars');
});
it("checking remaining characters", function() {
txt.val('hello');
txt.trigger('keyup');
expect(updateChars).toHaveBeenCalled():
});
This is just an illustrative example that you need to adjust to your needs.
Some notes
I see in your code this line loadFixtures('Fixture.html');
, I don't know what it actually does, but if it is an async call, then you will need to use the done
callback in the beforeEach
.
Another illustrative example with an asynchronous call:
beforeEach(function(done) {
//here we setup the "spy"spyOn(window, 'updateChars');
//supose this is a promise and we can call .thenloadFixtures('Fixture.html')
.then(function(){
txt = $('#text');
remainingChar = $('#count');
maxLengthBox = txt.attr('maxLength');
remainingChar.html(maxLengthBox);
done(); //finally
});
});
it("checking remaining characters", function(done) {
txt.val('hello');
txt.trigger('keyup');
expect(updateChars).toHaveBeenCalled():
done();
});
Hope it helps
Post a Comment for "Jasmine Unit Test Case For Dynamic Character Count"