Finding out whether the OnsenUI component is compiled during a unit test

My project utilizes the vue-cordova-webpack template. Within my Vue component, I have integrated a v-ons-input element. During the unit testing of my component, I encountered the need to modify the value of v-ons-input. However, this can only be done post-compilation of the ons-input, as the input is generated after compilation (refer to OnsenUI component compilation). The complication arises from the asynchronous execution of the compilation process, and the absence of a clear method to determine when the OnsenUI component is ready for use.

What should be my course of action? I resorted to creating a sinon spy for an internal method _compile within the ons-input element, and awaited its invocation:

it('test', (done) => {
   const wrapper = mount(myVueComponent)

   // Unable to set a value for ons-input at this point

   var spy = sinon.spy(wrapper.find('ons-input').element, '_compile')

   function waitForCall(spy) {
     return new Promise(function (resolve, reject) {
       (function wait() {
         if (spy.called) {
           return resolve()
         }
         setTimeout(wait, 10)
       })()
     })
   }

   waitForCall(spy).then(function () {
     // Now that ons-input is compiled, the value can be set
     wrapper.find('ons-input').element.value = 'foo'
     ...
   }).then(done, done)
 })

Is there a more elegant approach to identifying the readiness of the OnsenUI component for utilization in a unit test environment (without resorting to manipulation of the component's internal methods)?

P.S. Although I am aware of a non-test environment solution - listening for the init event on the document (as discussed here), this method does not prove effective in unit tests.

Answer №1

setTimeout is a reliable option in this context.

setTimeout(() => document.querySelector('ons-input input'));

More Information (optional)

The VOnsInput file imports the ons-input component, which is registered as a Custom Element synchronously. In order to ensure that ons-input is properly registered, Onsen UI uses a function called contentReady, which internally relies on setTimeout. Therefore, utilizing setTimeout in your code will achieve the same result.

To simplify, using setTimeout will ensure that the inner input element is attached when the callback of setTimeout is executed.

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 process for setting a default value in an array using Angular and then showcasing that value in a textbox?

I have been working on creating a form that includes a feature to dynamically append new rows. While I am able to successfully create new rows dynamically, I am facing an issue with displaying the initial values in my textboxes. Below is a snippet of my c ...

jQuery show/hide functionality allows you to toggle the visibility of elements

I am looking to create a toggle button that expands the menu-wrapper width and hides the sidebar when clicked. Here is the initial CSS styling: #my-wrapper { Width:500px; } #sidebar-wrapper { width:200px; } Upon clicking the button, the CSS will update ...

VueJS error: 'Property or method is not defined on the instance' message appears despite the method being defined on the instance

Is there a way in Vue.js to dynamically assign the class 'adjust' based on the value of a computed property called "isTrueSet"? I keep encountering the following error message: [Vue warn]: Property or method "itTrueSet" is not defined on the inst ...

Challenges with implementing a modular approach in Nuxt Vuex Store

Recently, I developed a Nuxt website and integrated a list of menu items from the WordPress API into the Vuex store. However, I used the deprecated classic mode for this integration and now wish to switch to the 'modules' mode as recommended in t ...

Extract information from a JSON Object using a specific pathway

Let's consider a scenario where we have a JSON Object structured as follows: var data = { "name": "abcd", "age": 21, "address": { "streetAddress": "88 8nd Street", "city": "New York" }, "phoneNumber": [ { ...

Opacity effect on input text when it exceeds the input boundaries

I'm having an issue: I need to create inputs with different states, including when the text is longer than the input itself. In this case, the extra text should fade out with a gradient transparency effect: https://i.sstatic.net/r5qQu.jpg Here is my ...

Extract rotation values of X, Y, and Z from a Matrix4 in three.js

Is it possible to extract specific rotation values (X, Y, Z) in degrees (float) from the Matrix4 in three.js? Due to the lack of direct implementation in many of its functions, I am unsure how to achieve this. ...

Randomly reorganize elements in a JavaScript array

I am facing an unusual issue while shuffling an array in JavaScript and I am unable to identify the root cause. Can someone provide assistance? When attempting to shuffle an array, the output I receive is unexpected: [1,2,3,4,5,6,7,8,9,10] Instead of ...

Displaying specific data based on selected checkbox values in the dataTable

I am currently working with a table using dataTable. Within this table, there is a filter labeled "All" and "Software Engineer." When I click on the label "Software Engineer," the data displayed is appropriate to the label. However, when I click on the "Al ...

Add content to div element using input from textarea

I'm attempting to create a basic text editor where users can input text and by clicking the edit button, it will add that text to the end of the id="textArea". However, nothing happens when I click the edit button after entering text in the textarea. ...

The text in the TextArea is not displaying line breaks as expected

Similar Question: Issue with new line preservation in .val() method for textarea I'm facing an issue where my text area does not preserve newlines when I enter a message with multiple lines. Even after retrieving the value from the text area, the ...

Is there a way to sort computers by Site ID within a Vuex getter?

In the process of developing an application, I have encountered a challenge. The app is designed to present users with a selection of sites, and upon clicking on a site, all the computers located at that specific site will be displayed. Users can then furt ...

Perform a sum operation with multiple conditions in Mongo DB to group the data

I have a collection of data stored in MongoDB with the following structure: { "_id" : ObjectId("63ceb466db8c0f5500ea0aaa"), "Partner_ID" : "662347848", "EarningsData" : [ { ...

What is the reason that certain functionalities do not work on an element that has two CSS classes assigned to it

I have a unique situation with two input elements and a button: <input class="close-acc-usr opt-in" type="text" name="closeAccUsr" /> <input class="close-acc-pin opt-in" type="number" name="cl ...

Issues arise with the functionality of CSS and JavaScript after successfully deploying a Next.js and React project to the server side

I need to deploy my React+Next and Node.js projects on the same port. To achieve this, I converted my TypeScript files to JavaScript and moved the .next folder to the backend side. Now, my API and frontend code are functioning on the same port thanks to Ex ...

Sending a variable to the resize() function in jQuery

Currently, I am working with two specific divs in my project: "#sidebar-wrapper" and ".j1". The height of ".j1" is dynamic as it changes based on its content. My objective is to set this height value to the "#sidebar-wrapper" element. I have attempted to ...

A SyntaxError was encountered because functions in strict mode code must be declared either at the top level or directly within another function

Hey there, I'm encountering a strange issue with my project. Everything runs smoothly when I run it in Developer mode using `grunt server`. However, when I try to run it in production mode with `grunt build`, I encounter an error: Uncaught SyntaxEr ...

Teach Google Bot how to recognize AJAX content

I'm facing a major issue while developing a website for someone else. The problem lies in the modals that are supposed to open when a user clicks on a product. My goal is to ensure that Google Bot recognizes these modals as individual pages. When a mo ...

What is the best way to extract information from a dynamically generated input field using vanilla JavaScript?

Creating a dynamic input field inside the 'education-field' div is the main goal here. The user can add more fields by clicking the "Add more" button, and the data from these input fields is crucial for form validation purposes. Users can input t ...

Tips for composing a single aggregation within a query

In my query, I wanted to find the first record with a CREATE_DATE greater than or equal to a specific date and less than another date. After calculating the duration between these dates, I needed to write another query. However, I was unsure how to combine ...