Sinon.js: spying on an empty callback

Currently, I am experimenting with callback functions using sinon.js

handleLoginActions = function (callback) {
    ...
    if (callback) {
        callback()
    }
    ..
}

var loginCallbackStub = stub();
handleLoginActions(loginCallbackStub);
expect(loginCallbackStub).to.have.been.calledOnce; //successfully executed

However, when attempting to test the scenario with an invalid callback function, I encounter this issue: null is not a spy or a call to a spy! (and it makes sense)

var loginCallbackStub = null;
handleLoginActions(loginCallbackStub);
expect(loginCallbackStub).to.not.have.been.called;

Any suggestions or insights on this matter?

Thank you

Answer №1

It is important to ensure that when invoking something as a function, it is not throwing an error. So, always test for the absence of an error:

expect(handleLoginActions()).to.not.throw(Error)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Acquiring JSON data within a JavaScript function using PHP

Requesting assistance on reading a JSON value using the code below, to retrieve data based on the $movieID. <script> function bookBTN(x) { <?php $url = 'http://movie.com/movieOne?seatNum=' . x; $jsonURL = file_get_ ...

Looking to manipulate the form submit data before it is submitted using the standard submit event and jQuery? Read on to learn how to remove, enhance, or change

Is there a way to extract data from a form like the one below? <form action="/search/search" data-remote="true" data-type="json" id="search_form" method="get"> <div class="input-group"> <span class="input-group-addon"> <i ...

What is the best way to calculate the number of items in your mongoose reference with a specific field?

I am trying to calculate the number of active Workers that belong to a specific company. Here is an example scenario: const workerSchema = new Schema( { userId: { type: Types.ObjectId, ref: 'User', ...

Is there ever a situation when it's acceptable to forego graceful degradation?

Currently, I am constructing a video-sharing CMS that heavily relies on jQuery and ajax for various functionalities such as rich UI effects and data submission/retrieval from the database. Unfortunately, if JavaScript is disabled, about 90% of the function ...

WebPack integration with Wordpress encounters an issue due to ReactJS Code Splitting while trying to load Bootstrap

Currently, I have set up a WebPack configuration that enables the direct injection of ReactJS into a website. This process compiles all the React code into a single index.js file, but I am facing an issue with its size. To address this, I am attempting to ...

Using jQuery's $.Deferred in conjunction with the window object's top.postMessage()

I am having trouble understanding how to effectively use $.Deferred. I currently have a situation similar to window.top.postMessage(mystring, myorigin); This works without any issues. I don't need assistance with sending/receiving postMessage What ...

Flickity remains in plain sight on desktop devices

I am trying to hide the flickity slider on desktop and larger devices. I have followed the instructions in the documentation, but for some reason, it's not working as expected. Here is how the div looks: <div class="w-full flex pl-4 pb-16 overflo ...

Error: The "use client" component does not recognize either window or localStorage

I'm currently working on creating a wrapper function that can be used in every dashboard component. "use client"; const UserWrapper = ({ children }) => { const user = JSON.parse(window.localStorage.getItem("ysg_u")); retur ...

Traversing through Object consisting of dual arrays within VueJS

Currently, I am working on a chat application built in VueJS and encountering an issue when attempting to display messages along with their respective timestamps. The challenge arises from the need to iterate through an object that contains two arrays: one ...

Passing a class as a parameter in Typescript functions

When working with Angular 2 testing utilities, I usually follow this process: fixture = TestBed.createComponent(EditableValueComponent); The EditableValueComponent is just a standard component class that I use. I am curious about the inner workings: st ...

Refresh the text area with the latest information received from the servlet using JavaScript

I am currently working on a program that involves calling a JavaScript function with multiple requests to a servlet. My goal is to execute these requests one by one and receive a response after each execution. However, the function I have only displays the ...

What is the best way to access and iterate through JSON data?

I am trying to extract data from my Json file, however, I cannot use a specific 'key' because it changes on a daily basis. https://i.sstatic.net/sZySk.png My attempted solution is as follows: template: function(params) { const objects ...

The onChange function failed to provide the expected target value

I attempted to implement an onChange function for a TextArea component, but I encountered an issue where the state was not updating when using console.log or passing it to the payload. Interestingly, when I used JSON.stringify, the value was successfully ...

Utilizing JavaScript within a WordPress loop

I'm facing an issue with running JavaScript within my WordPress loop where it doesn't seem to recognize the PHP variables. My goal is to create a functionality where clicking on one box reveals hidden content, and clicking on the same box or anot ...

Navigation issue discovered while trying to implement Ionic's collection-repeat feature

As a newcomer to Ionic and Angular.js, I recently downloaded an example of an ionic collection repeat and navigation from http://codepen.io/ionic/pen/mypxez. Initially, the example worked perfectly with just single index.html and index.js files. However, I ...

Is it possible for a Titanium application to utilize the iOS sharing functionalities?

I am looking to integrate a share feature in my app that allows users to easily share content by clicking on a share icon. I want the iOS share page to appear from the bottom of the screen, giving users options to share via message, mail, Twitter, and Face ...

"Troubleshooting: Passing selected checkbox values from jQuery popup to parent window is not functioning correctly

Friendz I am currently working on a JSP project where I am trying to create a pop-up window using jQuery. This pop-up window contains checkboxes that allow users to select values, and upon submitting the form, the selected checkbox values should be displa ...

Inspecting CSS values through Javascript is an important aspect of front

How can I create a JavaScript function that checks my CSS code and changes the background color to #29fd53 if it is currently #222327? Likewise, how do I make it switch back to #222327 if the current background color is #29fd53? This is my JavaScript code ...

Issue with integrating a JavaScript game into a Django template

Recently, I set up a Django server for my personal/local use and wanted to incorporate an interactive game into it (not for deployment, just for myself). After some searching, I came across this cool open-source game: https://github.com/MattSkala/html5-bom ...

Is Conditional Verification Possible with Vuelidate?

I have a form that is subject to different validations depending on the parameter called action stored in VUEX store. I am attempting the following: data: function() { const validations = { sendToProject: { cardProject: { require ...