How can I retrieve recently updated data from my Vue.js component?

Testing my Vuejs component is a top priority for me. To make sure everything runs smoothly, I rely on avoriaz, jsdom, mocha, and chai.

<template>
<div id="test-event" class="test-event">
<button id="button" v-on:click="plusClick" v-bind:value="click_count">{{click_count + " elements"}}
</button>
</div>
</template>

<script>
export default {
    data () {
        click_count: 0
    },

methods: {
        plusClick(event) {
            console.log("element = ", this.click_count)
            return this.click_count = Number(event.target.value) + Number(1);
        },
       get_click_count() {
            console.log(" => ", this.click_count);
            return this.click_count;
        },

watch : {
        click_count: function(element) {
            console.log("element = ", element);
        }
    }
</script>

In my unit test:

it('should render the button when called ', function() {

    const wrapper = mount(Bar);    
    expect(wrapper.contains('#test-event')).to.equal(true);  
    var test_event = wrapper.find('#test-event');            
    expect(test_event[0].contains('button')).to.equal(true); 
    var button_test = test_event[0].find('button')[0];       

    button_test.simulate('click');  
    button_test.simulate('click');  

    console.log( wrapper.data().click_count );     
    // The value remains at 0
    console.log( wrapper.methods.get_click_count() ): 
    // Error - wrapper.methods.get_click_count() is not a function
    console.log( wrapper.methods.get_click_count ): 
    // Undefined
    button_test.simulate('click'); 
    button_test.simulate('click'); 

});

Despite my efforts, I'm struggling to retrieve the updated value of click_count. Any suggestions on how to capture this value?

Answer №1

My knowledge of Vue.js leads me to believe that the following methods could be useful:

wrapper.invokeClickCount() can be used to trigger the actual method,

wrapper.$data.clicks retrieves the value of click_count:

Answer №2

wrapper.vm.get_click_count is a function that triggers a specific action.

wrapper.vm.click_count as well as wrapper.data().click_count allow you to access relevant information.

In addition, there exists a .methods() method which has its own set of challenges when utilizing methods that rely on the context of "this".

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

Retrieve the rowid from the first column of a jqGrid row

I want the first column of my jqGrid to display the rowid number number | name | class 1 | A | acceptable 2 | B | good 3 | C | bad Alternatively, I would like to add a column like this (image) https://docs.google.com/file/d/0Bxi6bFcYZ_MgYTI1dUJCMWEtd0E/ ...

In a Javascript scenario, only the initial button can successfully submit to an Ajax form even when having unique

Similar issues have been reported by other users when looping rows of buttons, often due to inadvertently reusing the same Id or value. I am facing a similar issue, but each of my buttons is unique. AJAX request <script> $(document ...

Interactions between JavaScript Event Listeners and concealed elements in the DOM

I'm in the process of developing a travel reimbursement form for my workplace. We have 4 distinct categories of travel, which led me to come up with a solution involving individual buttons for each category. Upon clicking a button, the corresponding f ...

Presenting information on a webpage using a URL parameter to create an unordered list

I'm looking to automatically display a specific page from an unordered list of navigation links without the user needing to interact with the page. Is there a way to accomplish this using a URL string? Any suggestions or solutions would be greatly ap ...

Leverage parent slot variables within the Vue template slot

I have a basic FormComponent: <template> <form> <fieldset> <slot /> </fieldset> <span v-if="!isEditing" @click="edit()">Edit</span> </form> </template> <scr ...

Empty body detected in Jquery AJAX request with Django REST running in a Docker environment

Using JavaScript to load a template called index.html from the /static directory. The JavaScript code in app.js: var obj = new Object(); obj.startTime = "123"; obj.endTime = "456"; console.log("fetchNext "+JSON.stringify(obj)); v ...

An error occurred during runtime due to a TypeError when trying to execute the 'setItem' function on 'Storage'. The error message indicates that 2 arguments are required for this function, but only 1 argument

Having an issue with the local storage not working and the screen showing up as white in the browser. 5 | const getFromLocalStorage = () => { 6 | if (typeof window !== undefined) { > 7 | const value = localStorage.getItem(& ...

Navigating through async functions in an Express.js router

Encountered a lint error indicating that Promises cannot be returned in places where a void is expected. Both functions [validateJWT, getUser] are async functions. Any suggestions on how to resolve this issue without compromising the linter guidelines by u ...

Ways to verify if both the select and input fields are selected and populated with jQuery

In my html form, I have a select field and an input box with two classes being used: work_phone_class and work_phone_class_input. I am triggering ajax by clicking a save button with a specific class whenever the value in the select field is changed or any ...

AngularJS: Validators in Controller do not directly bind with HTML elements

The in-line validations on the HTML side are functioning properly, but when I utilize functions in my Controller (specifically ingresarNuevoEmpleador and limpiarNuevoEmpleador), the $invalid value is always true. Additionally, $setPristine does not seem to ...

Is there an easy method for sorting through JSON data with various criteria?

After receiving a response from my web service containing an array of objects, I am looking to filter the data based on various combinations of values. What is the most efficient way to do this without impacting the performance of my web service? Data: Th ...

Modifying the appearance and behavior of an element dynamically as the page is being loaded using AngularJS

Struggling with a challenge for two days now, I have attempted to implement a panel-box using bootstrap and AngularJS. Within the confines of a controller, my code looks like this: <div id="menu2a"> <div class="panel list-group"> <div ...

Tips on utilizing index and eliminating React Warning: Ensure every child within a list has a distinct "key" prop

Hello, I am encountering an issue where I need to properly pass the index in this component. Can you help me figure out how to do that? Error: react-jsx-dev-runtime.development.js:117 Warning: Each child in a list should have a unique "key" prop ...

Issues with the visibility of inlined styles within the Angular component library

After developing a custom library with components for an Angular 4 application, I encountered an issue where the CSS styling from the components was not being applied when using them in an application. Although the functionality worked fine, the visual asp ...

What is the best way to display the legend on my PieChart?

I am attempting to display the names of colors in my PieChart as a legend. Here is the JSON data: { "choice": "20-29 yrs. old", "count": 4 }, { "choice": "30-39 yrs. old", "count": 2 }, { "ch ...

Tips for sending routeparams value to model window in AngularJS

In the current project, I am working on implementing a modal window for the edit screen functionality. The process involves displaying a list of items on the screen, from which users can select one row and then click on the modify button to open the edit s ...

jQuery "ooze navigation" - precise pixel rounding

After successfully programming my jQuery slime menu, I am facing a minor issue. Although the menu works beautifully, I have noticed that when I move the arrow and the ending circle of the slime separately, they do not always connect accurately if the curso ...

Web application error: Karma and Webpack cannot locate Angular controller

After following a blog post on setting up an Angular project with webpack, I used an associated boilerplate on Github. Although the app is functioning properly, Karma seems to have trouble finding the controller it needs to test. // karma.config.js modul ...

Communication between nodes using serial ports in Node.js fails to receive a response from the connected Arduino board

I've been attempting to establish communication between a computer and an Arduino board using node.js. Despite having a simple program, it just doesn't seem to work as expected. The Arduino program (which seems to be working fine) is as follows: ...

Guide on incorporating a JS file in a React application

I recently obtained a template for my website that includes the following JS file which is being called from my React component. !(function($) { "use strict"; // Hero typed if ($('.typed').length) { var typed_strings = $(&quo ...