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

  • Home
  • SEARCH
  • 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 8873831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:33:49+00:00 2026-06-14T18:33:49+00:00

I’m posting this for those that still use PrototypeJS and want to test custom

  • 0

I’m posting this for those that still use PrototypeJS and want to test custom events using jasmine and jasmine-prototype plugin.

We’re triggering custom events based on users clicks or form submits. We wanted a simple way to test that the custom events is fired correctly.

Here is an example:

var CustomEventsDispatcher = ( function() {
   function init(){

    //if elements exists
    if($$('a.trigger_bomb')) {
        // Observe the elements
        $$(elements).each(function(element) {
            element.observe('click', function(e) {
                this.fire("bomb:explode", {})
            });
        });
    }


    return {'init' : init};
 })();

Using resources available on the web, I’ve created a fixture …

 // spec/fixtures/links.html
 <a class="trigger_bomb" href="#"> Trigger Bomb </a>

… and a test suite:

  //spec/trigger_bomb_spec.js
 describe("Trigger Bombs", function() {

    beforeEach(function(){
       loadFixtures("links.html")
       CustomEventsDispatcher.init();
   })

   it("should raise the custom event", function(){
      var element=$$('a.trigger_bomb').first();
      spyOnEvent(element, 'click');
      element.click();

      expect('click').toHaveBeenTriggeredOn(element);
      expect('bomb:explode').toHaveBeenTriggeredOn(element);
   });
 }); 

The first assertion works fine, but not the second. The reason is that spyOnEvent alters the way click events are handled on element.

  • 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-14T18:33:52+00:00Added an answer on June 14, 2026 at 6:33 pm

    The simplest way is to mock the fire function like this:

      //spec/trigger_bomb_spec.js
     describe("Trigger Bombs", function() {
    
        beforeEach(function(){
           loadFixtures("links.html")
           CustomEventsDispatcher.init();
       })
    
       it("should raise the custom event", function(){
          var element=$$('a.trigger_bomb').first();
            spyOn(element, 'fire')
            element.click()
            expect(element.fire).toHaveBeenCalledWith('bomb:explode', {})
       });
     }); 
    

    However this solution has a side effect : the click will be executed which will cause the jasmine page result to reload continuously. You need to intercept click events and stop the default behavior with a global beforeEach hook:

    // spec/helpers/spec_helper.js
    beforeEach(function(){
    
        // stop all clicks from fixtures 
        // (but allow clicks from the user in the jasmine result page)
        document.observe('click', function(e){
            var element = e.findElement('#HTMLReporter')
            if (element == document)   {
                Event.stop(e)
            }
        })             
    })
    

    Hope this will help someone to save a few hours of debugging :). Feel free to comment / improve / propose a better alternative.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

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.