The issue of Angular JS rejecting $modalInstance due to an undefined function

Testing the delete functionality of my app with a mock delete $modal to simulate cancelling or confirming deletion.

  var modalInstanceMock=
      {
        result: {
            then: function(confirmCallback, cancelCallback) {
                //Store the callbacks for later when the user clicks on the OK or Cancel button of the dialog
                this.confirmCallBack = confirmCallback;
                this.cancelCallback = cancelCallback;
            }
        },
        confirmCallBack: function(item){
        return true;
        },
        cancelCallback: function(type){
        return false;
        },
        close: function( item ) {
            //The user clicked OK on the modal dialog, call the stored confirm callback with the selected item
     
            this.result.confirmCallBack( item );
        },
        dismiss: function( type ) {
            //The user clicked cancel on the modal dialog, call the stored cancel callback
    
            this.result.cancelCallback( type );
        }   
     
    };

Setting up the test scenario:

beforeEach(inject(function($modal) {
    spyOn($modal, 'open').andReturn(modalInstanceMock);
}));

Successfully testing the delete functionality:

var newRes = scope.deleteCar(car);
scope.modalInstance.close("ok");

Encountering an issue with dismissing:

var newRes = scope.deleteCar(car);
scope.modalInstance.dismiss("ok");

Receiving a Type:error undefined is not a function at Object.modalInstanceMock.dismiss message.

Confused about why close works correctly but dismiss is throwing an error.

Answer №1

Start with the beforeEach function,

modalInstance = {
        close: jasmine.createSpy('modalInstance.close'),
        dismiss: jasmine.createSpy('modalInstance.dismiss')
    },

Followed by the expectation.

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

IE8 does not support Jquery ajax() for cross domain requests to remote servers

My current script includes an ajax request to a remote server that provides a plain text response. This setup functions properly in all browsers with the exception of IE8, which is not surprising. The script snippet looks like this: $.ajax({ url: &apos ...

Convert the entirety of this function into jQuery

Can someone please help me convert this code to jQuery? I have a section of jQuery code, but I'm struggling with writing the rest! function showUser(str) { if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { ...

Selecting HTML5 data attributes using jQuery

There is a button on my page with multiple data attributes, and I am trying to hide it by selecting it based on its class and two specific data attributes. <a href='#' class='wishlist-icon' data-wish-name='product' data-wi ...

How come only the final element is being displayed from an array in JavaScript, rather than all of the elements present

I am facing an issue while attempting to extract specific information from a JSON data and create a new array with key-value pairs. However, instead of getting all the elements, it only returns the last one. Here is my current code snippet: const input = ...

Everything remained the same until an unexpected Ionic TypeError occurred, indicating that co.event is undefined

This problem has left me completely perplexed, as it's unlike anything I've experienced before. I'm currently developing an app in Ionic 2. Everything was running smoothly last night, but this morning I encountered a strange issue. While th ...

One way to display buttons only for the first row in a mat-table

I am working with a mat-table that displays a list of executing Jobs. Currently, there are stop and re-execute buttons in front of all the rows. However, I now want to only show the button on the first row. How can I achieve this? Here is the code for the ...

Guide to triggering React Material-UI modal and filling it with data from an Ajax request when a button is clicked

Despite my efforts to find a similar question, I couldn't come across one. My apologies if I overlooked it. Currently, I am working on a React Material-UI project to develop a basic web application. Within this application, there is an XGrid that disp ...

Executing NodeJS Functions in a Particular Order

Just starting to learn Node and working on building an app. Currently, in my route file, I am attempting to connect to MySQL and retrieve the major of the user, then use it for other operations. However, when I run the webpage, the console displays that t ...

What is the best way to spin a div HTML layer?

I'm trying to modify a Div layer that looks like this: ... <style type="text/css"> <!-- #newImg { position:absolute; left:180px; top:99px; width:704px; height:387px; z-index:1; background-image:url(../Pictures/rep ...

Problem with ng-repeat not updating select dropdown options

Struggling with AngularJS while working on a project, I encountered an issue with a 'select-options' element. Currently, I am making a $http request to retrieve data from a Cloud SQL database. To achieve this, I am invoking a function from my f ...

Tips for transferring data from an HTML textbox to a JavaScript variable

I'm currently working on a form that fetches data from a database and dynamically stores it in a table. There's a button labeled "add" which, when pressed by the user, adds a new row to the table. I also have a hidden counter that keeps track of ...

Preventing the use of zero as the first character in the text input field

Utilizing AngularJS and jQuery along with Javascript, I have encountered an issue. While the code works perfectly in JSFiddle, it fails to function on a JSP page. $('input#myId').keypress(function(e){ if (this.value.length == 0 &am ...

Verify the presence of the input post in the mongo database

Currently, I am utilizing the following node js code to store data in a database... //connect to database db.on('error', console.error); db.once('open', function() { router.post('/register', function(request, response) { ...

Achieving dynamic serving of static files using Rollup and integrating seamlessly with node-resolve

Currently, I am in the process of building a library using TSDX, which is a powerful CLI tool for package development based on Rollup. My project involves a collection of country flags SVGs that need to be imported and displayed dynamically when required. ...

AngularJS and the JavaScript programming language

Struggling with opening an Excel sheet by clicking on a button? I've written the code, but encountering issues. function openFile(strFilePath) { var objExcel; //Create EXCEL object objExcel = new ActiveXObject("Excel.Applicati ...

Get access to a JSON key using forward slashes in Node.js

I'm facing an issue with accessing a property in an object that contains forward slashes. Specifically, I need to access the child key pattern. Unfortunately, my attempts so far have been unsuccessful. Please provide a solution to access the pattern p ...

Tips on utilizing ajax to load context without needing to refresh the entire page

As a beginner in AJAX, I have some understanding of it. However, I am facing an issue on how to refresh the page when a new order (order_id, order_date, and order_time) is placed. I came across some code on YouTube that I tried implementing, but I'm n ...

Display only the error messages from ASP validators using JavaScript

How can I display only error messages for validation controls, not the text itself? The code I have tried is displaying null for error messages. function fnOnUpdateValidators() { for (var i = 0; i < Page_Validators.length; i++) { var val ...

PhoneGap/Cordova Issue: IceCreamCordovaWebViewClient ''URL restricted by whitelist''

After following the steps outlined in this guide: to build a debug APK using: ./build --debug I successfully deploy the APK with: abd -install <apk path> However, when the app initiates an API call, I encounter the following error message: Phon ...

If the width of the window is below 768px, do not execute the function

In order to prevent the showCover() function from firing when the window width is less than 768px on page load AND resize, I have made some adjustments to the code below. Even though the window is smaller than 768px, the function will not be triggered. ...