jasmine Expected spy myLinks to have been called error - javascript

I am having a hard time understanding jasmine spyOn function.
I wrote a simple function and test if my method was called:
function myView() {
myLinks();
}
Here are my tests:
describe('#myView', function() {
it('updates link', function() {
var spyEvent = spyOn(window, 'myLinks');
expect(spyEvent).toHaveBeenCalled();
});
});
This returns the following failure:
Expected spy myLinks to have been called
What am i doing wrong here?

You need to call the myView() function so the myLinks() have been called.
function myLinks(){
//some tasks
}
function myView() {
myLinks();
}
This two function above are declared in window object, then you create a spy object pointing to the window.
describe('#myView', function() {
myView();//Call the method so the myLinks was called too
it('updates link', function() {
var spyEvent = spyOn(window, 'myLinks');
expect(spyEvent).toHaveBeenCalled();
});
});

Related

How to test if jQuery load() is being called with Jasmine?

I have a javascript function deleteSomething(), within which the load() function in jQuery is called.
function deleteSomething(a, b){
deleteIt(a, b, function(success){
if(success){
$("#table").load(...);
}
})
}
Now I want to test deleteSomething() with Jasmine to see if load() is being called. But got Error: Expected a spy, but got Function
describe("deleteSomething", function() {
beforeEach(function () {
...
});
it("should call load() when deleteIt returns true", function() {
spyOn($('#table'), 'load'));
deleteIt.and.callFake(function(a, b, callback){
callback(true);
});
deleteSomething(a, b);
expect($('#table').load).toHaveBeenCalled();
});
});
I'm new to Jasmine, how should I do this?
You need to spy on jQuery prototype, it is available here: $.fn.
So your code should look something like this:
describe("deleteSomething", function() {
it("Should call load", function() {
//$.fn is where the load function is defined
// $("...") returns a new jQuery.fn instance that has load function
// from jQuery.fn.prototype
spyOn($.fn, 'load');
$("#anything").load();
expect($.fn.load).toHaveBeenCalled();
});
});
Some more information about the difference of object own members and prototype members (inherited) can be found here.

Calling a function in another function with AngularJS factory

I have an AngularJS factory that has multiple functions.
I want to call one of the functions inside the other function as shown below:
.factory("AppStart", function($cordovaSQLite) {
return {
init: function() {
var res = "hello";
console.log("in load start up page");
},
create_table: function() {
AppStart.init();
}
}
});
But I get the following error:
AppStart is not defined.
So how do I call the init() function in the create_table() function? I have tried just calling init(), but it doesn't work either.
To accomplish this, I recommend defining your functions with names, and then creating a service object with properties that refer to them, as I did below:
.factory("AppStart", function($cordovaSQLite) {
function init() {
var res = "hello";
console.log("in load start up page");
}
function create_table() {
init();
}
return {
init: init,
create_table: create_table
};
});

Jasmine Testing toEqual is correct value, still failing test

I am writing a series of tests to demonstrate the value of 'this' in different contexts.
Here is my current Test suite:
describe("this keyword", function(){
it('returns the global context of this', function(){
expect(globalThis()).toEqual(window);
});
it('returns the method context of this', function(){
expect(methodThis.showThis()).toEqual({ showThis : Function });
});
});
My second test won't pass with this code even though it is the exact value:
var methodThis = {
showThis: function(){
return this;
}
};
All this function is doing is returning the context of this inside of an object.
Why is this test failing even though the correct toEqual value is being passed to toEqual?
You can use jasmine.any for comparing the function type:
describe("this keyword", function(){
it('returns the method context of this', function(){
expect(methodThis.showThis()).toEqual({ showThis : jasmine.any(Function) });
});
});

Meteor.setTimeout() issue in Meteor Timers?

I did a sample example on Meteor.setTimeout() using Meteor. In this example i get an error. I didn't have any idea about this.So please see the below code,error and suggest me how to do?
Error :
Exception in setTimeout callback: TypeError: undefined is not a function
at _.extend.withValue (http://localhost:3000/packages/meteor.js?8ec262df25783897eaad01255bc8bd1ca4e78b24:773:17)
at http://localhost:3000/packages/meteor.js?8ec262df25783897eaad01255bc8bd1ca4e78b24:358:45
at http://localhost:3000/packages/meteor.js?8ec262df25783897eaad01255bc8bd1ca4e78b24:801:22
JS Code :
if (Meteor.isClient)
{
Meteor.setTimeout(Test("10"), 1000);
Meteor.setInterval(Test1, 1000);
Template.hello.greeting = function ()
{
return "Welcome to timerapp.";
};
Template.hello.events
({
'click input' : function ()
{
// template data, if any, is available in 'this'
if (typeof console !== 'undefined')
console.log("You pressed the button");
//Test();
}
});
}
function Test(x)
{
console.log("*** Test() ***"+x);
}
function Test1()
{
console.log("*** Test1() ***");
}
if (Meteor.isServer)
{
Meteor.startup(function ()
{
// code to run on server at startup
});
}
The problem is that setTimeout expects a function as a first parameter but you are passing the result of evaluating Test("10") which is "undefined".
You can solve the issue by wrapping your call to Test1 in an anonymous function:
Meteor.setTimeout(function(){Test("10");}, 1000);

Testing callback called after a event trigger with Sinon Js

It's my first test on Javacript with Mocha/Sinon/Chai And I don't know if it's possible to do this :
var obj = {
first : function () {
console.log('make job 1');
}
};
var objManager = function() {
$(document).on('event1', obj.first);
};
new objManager();
var spy = sinon.spy(obj, 'first');
describe('Test', function () {
it('My first test', function () {
$(document).trigger('event1');
spy.should.not.have.been.called;
});
});
My spy isn't called and don't understand why... My function "obj.first" has printed "make job 1".
if I modify my test by :
it('My first test', function () {
obj.first();
spy.should.not.have.been.called;
});
My spy is called.
So my question is : How make sinon spy work with a event ?
The problem is that you first bind the function to the event and then replace the function in obj with the spy. Doing this will not have any effect on the function you have bound to the event cause this is still the original function.
Do test this you have to create the spy before instantiate your objManager.

Categories