JavaScript How do you spy on a call back in an ajax call with jasmine? ajax callback,ajax callback function,ajax datatable callback,call ajax

I'm currently using enyo and I have a function on a component that takes a callback function and makes an ajax call, then calls the callback on success. I can't seem to figure out how to spy on the callback function.

enyo.kind({
  name: 'Login',
  isLoggedIn: function (callback) {
      $.ajax({
        url: '/checkLogin'
      })
      .done(function (data) {
         /* Some logic here */
         return callback.apply(null, data);  //IF all goes well this should call the spy
      })
      .fail(function(){/*Fail Stuff*/});
  }
  ....
});

For the test I have:

describe('Valid User', function() {
   var ajaxSpy;
   var loginTest = new Login();
   beforeEach( function () {

      ajaxSpy = spyOn($, 'ajax').andReturn({
         done: function (fn ) {
             fn();
         },
         fail: function (){}
      });
   });

   it("should call the callback", function () {
      var spy = jasmine.createSpy("spy");
      loginTest.isLoggedIn(spy);
      expect(spy).toHaveBeenCalled();
   });
 }

In this case when this runs the callback becomes undefined, I think it's probably because of the ajax spy intercepting it but I'm unsure how to fix it.

Answer:1

I tried many solutions I found on the web. None worked. I'm trying to make a website scroll horizontally when I'm scrolling vertically. I tried to accomplish this via js - nothing. Then I read that I ...

I tried many solutions I found on the web. None worked. I'm trying to make a website scroll horizontally when I'm scrolling vertically. I tried to accomplish this via js - nothing. Then I read that I ...

I'm writing a node wrapper to interact with an external api and am having a difficult time testing the asynchronous createJob method. Below is the test case code: api_key = "...

I'm writing a node wrapper to interact with an external api and am having a difficult time testing the asynchronous createJob method. Below is the test case code: api_key = "...

  1. mocha test asynchronous function
  2. mocha tests async
  3. mocha test asynchronous callback
  4. mocha test asynchronous code
  5. mocha test asynchronous calls
  6. mocha asynchronous tests
  7. mocha test async function
  8. mocha test async await
  9. mocha test async error
  10. mocha test async method
  11. mocha test async code
  12. mocha test async done
  13. mocha test async promise
  14. mocha test async await function
  15. mocha async tests timeout
  16. mocha async test example
  17. mocha async test order
  18. mocha async test fail
  19. mocha async test case
  20. mocha dynamic tests async

I'm writing a module that will create a dynamic menu on the fly. How to run a directive after adding new <li> with css class dropdown which is also added by ng-class. The code: angular.module('...

I'm writing a module that will create a dynamic menu on the fly. How to run a directive after adding new <li> with css class dropdown which is also added by ng-class. The code: angular.module('...

  1. dynamically adding dropdown using angularjs
  2. dynamically add dropdown in javascript
  3. dynamically add dropdown in jquery
  4. dynamically add dropdown using jquery
  5. dynamically add dropdown on button click javascript
  6. dynamically adding dropdown using jquery
  7. dynamically add dropdown items bootstrap
  8. dynamically adding dropdown using angular 4
  9. dynamically adding dropdown values javascript
  10. dynamically add dropdown in html
  11. dynamically add dropdown mvc
  12. bootstrap dynamically add dropdown
  13. javascript dynamically add dropdown options
  14. jquery dynamically add dropdown options
  15. angularjs dynamically add dropdown

i want to show a pop up with red color for some words i want to show variable "keyword name" in red color as well as " Todays Position" word . These both words should be in red color font. ...

i want to show a pop up with red color for some words i want to show variable "keyword name" in red color as well as " Todays Position" word . These both words should be in red color font. ...

  1. write alert in javascript
  2. write alert in php
  3. write alert in c#
  4. write alert in html
  5. write alert in jquery
  6. write alert in jsp
  7. write alert in code behind c#
  8. write in alert
  9. response.write with alert message