Angular-jasmine: conducting tests on a component that utilizes timeout to render the DOM

Within my custom directive, I have implemented the following functionality:

link: function (scope, element) {
    var editor = CKEDITOR.inline(element.find('div[contenteditable]')[0], {}

To ensure that the directive is properly linked and that the editor is successfully created within the element using the CKEDITOR.inline method, I have written this test:

it('should compile', function () {  

    var element = angular.element('<directive></directive>');
    var compiled = $compile(element)(scope);
    $('body').append(compiled);

    expect(element.find('.ckeditor')).toExist();
});

However, a challenge arises as CKEDITOR updates the DOM asynchronously:

CKEDITOR.inline = function(element) {
    setTimeout(function() {
        element.append('<div class=ckeditor></div>');
    },0);
}

As a result, the test fails to locate the element with the specified class due to the asynchronous nature of the element insertion. How can I address this issue in testing?

Answer №1

Specifications have the ability to be executed in an asynchronous manner:

it('must pass compilation', function (done) {  
    ...
    setTimeout(() => {
        expect(element.find('.ckeditor')).toExist();
        done();
    }, 10);
});

Alternatively, jasmine.clock can be utilized:

beforeEach(function() {
    jasmine.clock().install();
});

afterEach(function() {
    jasmine.clock().uninstall();
});


it('must pass compilation', function () {  
    ...
    jasmine.clock().tick(10);
    expect(element.find('.ckeditor')).toExist();
});

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

"An issue with the colyseus server has been detected within the JavaScript code

I have written some code but it seems to be causing errors. const colyseus = require("colyseus"); const http = require("http"); const express = require("express"); const port = process.env.port || 3000; const app = express(); ...

I'm experiencing an issue with fullCalendar where the dayRender function is not functioning as expected

I have been using fullCalendar and I am looking to customize the color of specific days. I have successfully created an overlay that is displayed when a user clicks on a particular day. Everything works as expected with the overlay, but now I am encounte ...

Manage the angularJS user interface switch through an external event

I have implemented an AngularJS Material UI switch and I am looking to update its status based on an external event. This event occurs when a MQTT message is received on a specific topic that is published. To achieve this, I am utilizing a Node.js MQTT cli ...

Tips for effectively jasmine testing with the createSpyObj function, where class properties are defined as spies

When attempting to create a mock service with set-only properties, I encountered errors indicating that the value was undefined despite following the guidance in the documentation here. I want to be able to track the values of these properties during test ...

AngularJS endless scrolling from both top and bottom

I have a question regarding the functionality of ngInfiniteScroll. Currently, when using this plugin, more items are loaded as you scroll to the bottom, similar to how Facebook's timeline works. However, I am in need of a reverse functionality where i ...

Transform jQuery code to its equivalent in vanilla JavaScript

While I am proficient in using jQuery, my knowledge of pure JavaScript is somewhat limited. Below is the jQuery code that I have been working with: $(document).ready(function() { $.get('http://jsonip.com/', function(r){ var ip_addre ...

Error: Unable to access the 'CustomerId' property because it is undefined

Recently, I made some updates to my website. As part of the upgrade process, I switched to AngularJS v1.7.6 and JQuery v1.12.1. However, after making these changes, I encountered an error in my console log. TypeError: Cannot read property 'CustomerId ...

Is it possible to link fields with varying titles in NestJS?

Currently, I am developing a NestJS application that interacts with SAP (among other external applications). Unfortunately, SAP has very specific field name requirements. In some instances, I need to send over 70 fields with names that adhere to SAP's ...

Setting up a service URL with parameters using a versatile approach

I am faced with a situation where I have over 200 service URLs that follow a specific format: serviceURL = DomainName + MethodName + Path; The DomainName and MethodNames can be configured, while the path may consist of elements such as Param1, Param2, an ...

Obtain the inner text input value using jQuery

In my form, there is a feature that adds a new row of text inputs dynamically when a user wants to add more rows. Each new row is automatically populated with input fields that have the same id and class as the previous ones. My question is: how can I re ...

Tips for simulating Cache::remember in Laravel

While attempting to mock a response for Cache::remember within a unit test method, I used the following code: Cache::shouldReceive('remember') ->once() ->with('my_key', 120, function() {}) // There are 3 args in remember ...

Tips on how to sort a dropdown menu in Angular JS?

I've looked at a few similar questions, but their solutions don't seem to be working for me. Here's the code I'm using to display data in an AngularJS table. I want to filter the query to only show SecurityID numbers 8, 9, and 10. When ...

Implementing Ajax to Load Template-Part in Wordpress

Hey there! I'm currently working on enhancing my online store by adding a new feature. What I'd like to achieve is that when a customer clicks on a product, instead of being taken to the product page, the product details load using AJAX right on ...

tips for generating a random number for direct display

Can anyone help me figure out how to automatically display the total of numbers from this script on my blog post without having to click anything? Additionally, I need it to update if the browser is refreshed. ` http://jsfiddle.net/BenedictLewis/xmPgR/ ...

Why is my PHP function not able to properly receive the array that was sent to it via Ajax?

After retrieving an array through an ajax query, I am looking to pass it to a PHP function for manipulation and utilization of the elements at each index. The PHP function in question is as follows: class ControladorCompraEfectivoYTarjeta { public fu ...

Issue with jQuery datepicker not triggering onChangeMonthYear event

Recently, I've been working on creating an app using jQuery date picker. To see my progress so far, feel free to check out this fiddle: http://jsfiddle.net/Lf6sD/2/. In the options, there's a mention of an onChangeMonthYear event that should trig ...

Error Uncovered: Ionic 2 Singleton Service Experiencing Issues

I have developed a User class to be used as a singleton service in multiple components. Could you please review if the Injectable() declaration is correct? import { Injectable } from '@angular/core'; import {Http, Headers} from '@angular/ht ...

By utilizing the window.history.back() function, it takes me four clicks to navigate back one page

I find it peculiar that my back button is using the JS method window.history.back() to return to the previous page. However, I have noticed a strange behavior with this button. When I click on it before the entire document loads, it functions as expected a ...

Disabling Babel in Nuxt.js: A Step-by-Step Guide

I've come to the decision to eliminate Babel transpilation from my projects, as I no longer see the need to accommodate pre-ES6 era browsers. However, my search efforts have yielded no results on how to go about this. My Nuxt project is currently fill ...

Is there an easy way to determine if an element inside a Vue component is overflowing?

I created a unique component called ResultPill which includes a tooltip feature (implemented using vuikit). The text displayed in the tooltip is determined by a getter function called tooltip, utilizing vue-property-decorator. Here are the relevant parts o ...