The jasmine test revealed a failure as the spy for openQuickSubtypes was expected to have been called during the context menu test case

I encountered an error with my jasmine test, where I was expecting the spy openQuickSubtypes to have been called.

The issue arose while I was working on implementing a context menu.

component.html

<div class="each-shift" *ngFor="let shift of shiftsWithEmptyBoxes">
    <div class="shift-cover" [ngClass]="shiftDetails(shift)">
      <div class="requested-vertical-bar"
           [ngClass]="getShiftVerticalBarColor(shift)"></div>
      <div class="shift-left-box" #subtype (contextmenu)="openQuickSubtypes(subtype, subtypeMenu, shift); $event.preventDefault();">
        {{ showShiftOverTime(shift) }}
        {{ getShiftSubTypeLetter(shift, false) }}
        {{ showSubTypeShiftNotation(shift) }}
      </div>

component.ts

 openQuickSubtypes(origin:any,menu:any,index:number)
  {
    this.contextService.openContextMenu(origin,menu,this.viewContainerRef,{data:index})
    .subscribe(openMenu=>{
    })
  }

my test case: test case should open right click

it('should right click', fakeAsync(() => {
    component.shiftsWithEmptyBoxes = [{
      "shiftId": 130374,
      "shiftType": "empty"
    }
    ];
    fixture.detectChanges();
    const menuClick = fixture.debugElement.query(By.css('.shift-left-box'));
    const spyonOpenQuickSubtypes = spyOn(component, 'openQuickSubtypes').and.callThrough();
    const event = new MouseEvent('click',
    {
    view: window,
    bubbles: true,
    cancelable: true,
    relatedTarget: document
    });  
    menuClick.nativeElement.dispatchEvent(event);
    fixture.detectChanges();
    fixture.whenStable().then(() => {
      expect(spyonOpenQuickSubtypes).toHaveBeenCalled();
    });
  }));

Every time I run the test case, I keep encountering the same error:

Error: Expected spy openQuickSubtypes to have been called.

Answer №1

Give this method a try without relying on the return value of spyOn


it('should perform a right click', fakeAsync(() => {
    component.shiftsWithEmptyBoxes = [{
      "shiftId": 130374,
      "shiftType": "empty"
    }
    ];
    fixture.detectChanges();
    const menuClick = fixture.debugElement.query(By.css('.shift-left-box'));
    spyOn(component, 'openQuickSubtypes').and.callThrough();
    menuClick.triggerEventHandler("contextmenu", new  MouseEvent("contextmenu"));
    fixture.detectChanges();
    expect(component.openQuickSubtypes).toHaveBeenCalled(); 
  }));

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

My reselect function seems to be malfunctioning - I'm not receiving any output. Can anyone help me

I'm looking to implement reselect in my code to retrieve the shopping cart based on product ids. Here's my reselect.ts file: import { createSelector } from "reselect"; import { RootState } from "../store"; export const shopp ...

The new experimental appDir feature in Next.js 13 is failing to display <meta> or <title> tags in the <head> section when rendering on the server

I'm currently experimenting with the new experimental appDir feature in Next.js 13, and I've encountered a small issue. This project is utilizing: Next.js 13 React 18 MUI 5 (styled components using @mui/system @emotion/react @emotion/styled) T ...

Tips on toggling the visibility of an element in Angular 4

This is my unique html element: <ng-container> <span *ngIf="row.messageText && row.messageText.length >= 30 && expanded">{{row.messageText.substr(0, 25)}} <span>more</span> </span> ...

Tips for determining the overall percentage breakdown of 100% based on the individual denominator for every column within angular 8

In my code, I have a simple function that calculates the sum of numbers and strings in columns within a table. The sum calculation itself works fine and provides accurate results. However, the problem arises when I attempt to divide the total sum of each c ...

Angular does not support the functionality of having multiple material paginations within a single component

I am currently working on creating a component that contains two dataTables, each with a different dataSource. However, I am facing an issue where my Tables are not visible immediately after the component is loaded due to the *ngIf directive being used. Be ...

Creating an Angular 2 component library that is compatible with both webpack.js and system.js: A guide

This is my first venture into creating an Angular 2 library. So far, it consists of a collection of components. I am aiming to make this library compatible with both Webpack and SystemJS. I have successfully written the code for the first component to be c ...

Tips for submitting a form with AngularJS version 4

I am developing a single page application using AngularJS 4. Can anyone guide me on how to submit a form upon clicking the submit button in AngularJS 4? Your help is greatly appreciated. Thank you! ...

Obtaining the identifier of a generic type parameter in Typescript

Is there a method in TypeScript to retrieve the name of a generic type parameter? Consider the following method: getName<T>(): string { .... implement using some operator or technique } You can use it like this: class MyClass{ } getName< ...

What is the correct placement for the "require("firebase/firestore");" code in my Angular 4 project?

After doing thorough research on the internet and carefully reading through the Firestore documentation, I am facing difficulties in converting my partially built Angular (4) project. Following the instructions in the "Get Started with Cloud Firestore" gu ...

Enhancing the session object with new properties

I am attempting to include extra properties in the session object req.session.confirmationCode = confirmationCode; However, I encounter an error stating that the property confirmationCode does not exist Property 'confirmationCode' does not exist ...

Exploring the Angular 2 Framework by Streaming Data from a RESTful API

Is it possible to easily handle a continuously streaming data feed in angular2? I am working on a chat application with a server written in go and a client implemented in angular2. To enable real-time updates, I have set up an endpoint that maintains the c ...

What is the best way to retrieve specific data based on their unique identifier?

Imagine having a table like this, with the following data: Id , Name , IsBillable 1 One 1 2 two 0 3. three 0 I have stored this data in a variable called masterData using the get method, and I am using it to populate a dropdown me ...

After logging out, Next-auth redirects me straight back to the dashboard

In my NextJS application, I've implemented a credential-based authentication flow along with a dashboard page. To handle cases where an unauthorized user lands on the dashboard route, I've created a custom AccessDenied component. In the getServer ...

Using interpolation brackets in Angular2 for variables instead of dots

I'm curious if Angular2 has a feature similar to the bracket notation in Javascript that allows you to use variables to select an object property, or if there is another method to achieve the same functionality. Here is the code snippet for reference ...

Transitioning from AngularJS to Angular: Updating the factory prototype

I've been in the process of migrating from AngularJS to Angular and have made a lot of progress. However, I am currently facing challenges with factories and prototypes. In this context, the Card object represents a playing card. function Car ...

a feature in mongoose that automatically increments versions when creating a new document

Is it possible to set up an auto-increment feature for the versionKey (__v) field whenever a new document is created, or should I consider using a different field like 'version' in the schema? Here's an example of the schema used in my appl ...

Angular nested Tree is not appearing as anticipated

The tree structure I created using Angular Nested Tree component is not displaying the User Interface as expected. The words "fruit" and "vegetables" are overlapping with the text "expand more", making it difficult to read. Can someone help me fix this UI ...

Developing a search feature within a MEAN stack application

I'm a beginner in the world of MEAN stack applications and I'm curious about how to incorporate search functionality into my application. While I've done some research on the topic, most suggestions point towards using elastic search. Do you ...

Utilizing Sequelize to search for existing items or create new ones in a list

My experience with sequelize is limited and I am having trouble understanding certain aspects. Despite my efforts to search for more information, I couldn't find anything specific that addresses my confusion. // API Method function SeederApi(req: Req ...

Creating a component with the name "c-name" is not possible because the specified module does not exist

Current working directory /src/app Directory of app.module.ts /src/app/app.module.ts Adding a new component to this directory catalog/single/configurator/[new component here] Attempt #1 to add a component ng g c catalog/single/configurator/details-popo ...