What is the process for testing ng-select in Angular using the testing library?

Below is a list of companies displayed in an ng-select:

<div class="input-group">
    <ng-select role="company" id="company" class="form-control" #Company
               formControlName="company">
        <ng-option *ngFor="let c of companies" [value]="c.id">
            {{c.name}}
        </ng-option>
    </ng-select>
</div>

In order to perform the necessary unit tests, I am using Jest and Angular testing library. The ng-select serves as a searchable drop-down where users can enter keywords. To select the first option by typing text and pressing enter or clicking on the first option, what approach should be taken?

The following method I'm attempting does not achieve the desired result:

userEvent.type(screen.getByRole('company'), companyName);

How can I effectively access the ng-select? My current method works for textboxes but encounters issues with ng-select.

Answer №1

Link to ARIA Combobox Guidelines

Avoid Changing Default Roles

To access your select field, you can use:

const selectField = screen.queryByRole('combobox');

To retrieve the options, use:

const options = screen.queryAllByRole('checkbox');

If you want to display different options in your select, consider using ng-template with ng-multi-label-tmp or ng-option. Add a data-testid="example" to the content within ng-template and then query that specific option with:

const optionA = screen.queryByTestId('example');

Review the ng-template documentation for more information on its usage, but it may not be necessary in your case.

Avoid modifying roles solely for testing purposes. ARIA roles serve an important function in accessibility (e.g., for screenreaders).

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

What is the solution to the problem when "Date" is not an instance of Date?

As I was working on fixing an issue with an "Invalid date" error in my ngx-bootstrap-bsDatepicker control and FormGroup, I came across a problem with the date value stored in the object this.ts.startDateTime when compared to a new Date() object. 08:11:03. ...

What is the process for creating an additional username in the database?

As a beginner frontend trainee, I have been tasked with configuring my project on node-typescript-koa-rest. Despite my best efforts, I encountered an error. To set up the project, I added objection.js and knex.js to the existing repository and installed P ...

A guide to effectively converting and parsing a class object that includes Uint8Array elements

After stringifying a class object that contains a property in Uint8Array, and then parsing it later, the property is no longer in Uint8Array format. Here's an example: class Example { title:string; byteData:Uint8Array; ...

Ensure that enums in Typescript are initialized explicitly

Is there a way to explicitly initialize the following enum in typescript? enum BloodGroup { OPositive = "O +ve", ONegative = "O -ve", APositive = "A +ve", ANegative = "A -ve", } I attempted something like this (I know it won't wo ...

The Firebase Cloud Function is failing to trigger on the onCreate event within the Firebase Realtime Database

I recently deployed a function to Firebase with the following code: import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; console.log('FILE LOADED'); const serviceAccount = require(' ...

Establishing a response header in conjunction with a specified string

I'm facing an issue while attempting to include a token in the headers of an HTTP request from backend to frontend, along with sending a custom string. Despite my efforts, I am encountering a problem where the token appears as null on the client-side. ...

Angular component nesting involves placing one component within another component in

When a component is nested inside another, what is that called? For example: <agm-map [latitude]="lat" [longitude]="lng"> <agm-marker [latitude]="lat" [longitude]="lng"></agm-marker> </agm-map> Can you explain how this nesting w ...

What is the process for sending an email using Angular 6 and ASP.NET CORE web api?

When a user inputs their email and password in the designated boxes, they also include a recipient email address with a CC. This information is then sent via a web api. The email will contain text like "Total Sales Today" along with an attached PDF file. ...

Testing Angular: Inability to achieve full code coverage for Ternary branch in unit testing

Currently, I am attempting to test a ternary branch that utilizes the window.location property. @Injectable() export class ABCService { private hostUrl = (window.location.host.indexOf('localhost') > -1) ? 'example.com' : window.lo ...

Using TypeScript to define generic types for classes, method parameters, and method return types

I am facing an issue with this particular function function getCollection<T>(collectionType: T): Collection<T> { return new Collection<T>() } In the Collection class, I have the following code snippet export class Collection<T> { ...

Looking to retrieve CloudWatch logs from multiple AWS accounts using Lambda and the AWS SDK

Seeking guidance on querying CloudWatch logs across accounts using lambda and AWS SDK Developing a lambda function in typescript Deploying lambda with CloudFormation, granting necessary roles for reading from two different AWS accounts Initial exe ...

Wait for each observable subscription to complete

In my scenario, I have an array called orderCodes, which stores specific order codes. With each code, I can retrieve the corresponding order details, where each order contains multiple products. My goal is to extract the code of each product from the order ...

Tips for deactivating a specific value or graying it out within the list of value items in a drop-down menu using <ng-selectize> or <options> in Angular 7

I am facing a situation where I have to dynamically generate a list of options for a dropdown menu in my Angular 7 application. However, I need to be able to disable specific options in the list based on certain conditions or flags. For example: I am disp ...

The autocomplete suggestions appear as faded gray text following the cursor in the input field, rather than displaying as a pop-up selection

My website is built using Angular 10 and Angular Material. I am looking to enhance the input suggestions in matInput, but in a unique way that differs from matAutocomplete. Instead of displaying a list of options, I want to show just one suggestion for a ...

Struggling to find the definition of a Typescript decorator after importing it from a separate file

Consider the following scenario: decorator.ts export function logStuff(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<any>) { return { value: function (...args: any[]) { args.push("Another argument ...

Splitting the div into two columns

I've encountered various solutions to this issue, but when I integrate an Angular2 component inside the divs, it fails to function properly. Here is my progress so far: https://i.stack.imgur.com/qJ8a9.jpg Code: <div id="container"> <div ...

What is the best way to modify a constant array in Angular?

Hello team, I'm fresh to working with angular and I have a TypeScript file that contains a list of heroes: export const HEROES: Hero[] = [ { id: 11, name: 'Dr Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombas ...

Angular 9: How to Return an Object Instead of a Boolean with an Observable Getter in BehaviorSubject

Apologies if this question has already been addressed on Stack Overflow, but I haven't found a suitable solution yet. I am working with a BehaviorSubject that tracks a boolean state. I have a public getter method that returns an Observable of the Beh ...

I am interested in utilizing Vue Fallthrough attributes, but I specifically want them to be applied only to the input elements within the component and not on the container itself

I am facing an issue with my checkbox component. I want to utilize Fallthrough attributes to pass non-declared attributes to the input inside the checkbox component. However, when I add HTML attributes to the Vue component, those attributes are applied not ...

How to display a JSON object array in Angular 4

My object is made up of fields, objects, and arrays, resembling the following structure: Within my HTML code, I retrieve data for aaType and adjust. <div class="flexdirectioncolumn"> <div class="flex50"> <label class="label-pro ...