Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:45:59+00:00 2026-06-18T07:45:59+00:00

I’m rather new at both RequireJS and Jasmine so I’m having a little trouble

  • 0

I’m rather new at both RequireJS and Jasmine so I’m having a little trouble setting up a few basic tests to start with. I’ve found a lot of info on how to set up the two together, and have it working. However I have an interesting problem I can’t seem to fix.

Unfortunately I’m not sure the best way to word the problem so here’s some code:

main.js:

require(['jquery', 'manipulate'], function($, Manipulate) {
  var manipulate = new Manipulate;
  $(document).on('change', 'td input', function() {
    manipulate.pushChange();
  });
});

manipulate.js:

define(function() {
  function Manipulate() {
    //
  };

  Manipulate.prototype.pushChange = function() {
    return true;
  };

  return Manipulate;
});

ManipulateSpec.js:

describe('Manipulate', function() {
  var manipulate;

  beforeEach(function() {
    var flag = false;

    // Load fixtures into the HTML
    loadFixtures('ManipulateFixture.html');

    // Require the manipulate.js file
    require(['jquery', 'manipulate', 'main'], function(jQuery, Manipulate) {
      manipulate = new Manipulate;

      // Define any spies
      spyOn(manipulate, 'pushChange');

      flag = true;
    });

    // Wait for manipulate.js to load before running tests
    waitsFor(function() {
      return flag;
    });
  });

  it('should call pushChange after changing a cell', function() {
    $('td input').eq(0).trigger('change');
    expect(manipulate.pushChange).toHaveBeenCalled();
  });
});

(Removed some extra code)

If I console.log inside Manipulate.pushChange, it is firing. The problem is, the Manipulate object that is being spied on isn’t the same object that is passed as an argument in the main.js file. So adding manipulate.pushChange in my it() block makes the test pass.

I found an answer for Backbone.js apps, which calls delegateEvents. I’m not sure if there is a similar solution for vanilla Javascript, jQuery, etc. but I can’t find one.

Is there a better way to structure my files, potentially putting my jQuery event handlers in the manipulate module? Or just a way to “copy” events between the two objects? I don’t even believe using createSpy will help me much in this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:46:01+00:00Added an answer on June 18, 2026 at 7:46 am

    There are some problems with your code that make it really hard to test. First you can’t test requiereJs modules like the way you try it, if you wanna mock the dependencies. Take a look at this SO for some solutions.

    The other problem is that you relay on jquery and DOM events. So most of the time I doesnt try to rebuild the DOM with fixtures for the test. Instead I spy on the jquery object that the event was binded to and call the function by myself. So in for your code

    $(document).on('change', 'td input', function() {
      manipulate.pushChange();
    });
    

    you could spy on $like this

    var documentSpy ={on:jasmine.createSpy()};
    spyOn(window, "$").andReturn(event); // in a requireJs module mock it like in the SO I've mention above
    

    Now when your code bind the event it will just call the spy and you can check if the event was binded correctly:

    var callback = documentSpy.on.mostRecentCall.args[2]
    expect(documentSpy.on).toHasBeenCalledWith('change', 'td input', callback);
    // fire the callback
    callback();
    expect(manipulate.pushChange).toHaveBeenCalled();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am writing an app with both english and french support. The app requests
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.