Creating mock implementations using jest in vue applications

I have been experimenting with testing whether a method is invoked in a Vue component when a specific button is clicked.

Initially, I found success using the following method:

it('should call the methodName when button is triggered', () => {
  const methodNameSpy = jest.spyOn(wrapper.vm, 'methodName')

  wrapper.find('.is-success').trigger('click')

  expect(methodNameSpy).toBeCalled()
})

This approach worked well for certain components, but I encountered an error indicating that the method was not called in some other components:

Error: expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

I suspect why this simple test may be failing in this scenario, but I am unsure of how to address it or if it is indeed the root cause.

The specific component in question is a "tooltip"; once the save/close button is clicked, it closes and disappears. This could potentially explain why the test is failing as it may no longer locate the elements being tested.

Here are the relevant code snippets for the component and its respective tests:

component.vue

<template lang="html">
    <div class="editresewin" @click.stop>
        <div class="topbar">
            <button
                class="button is-danger"
                @click="close"
            >
                <b-icon icon="close" />
            </button>
            <button
                class="button is-success"
                @click="save"
            />
            <h2 class="...">Title</h2>
        </div>
        <div class="...">
            <div class="...">
                <label>Label</label>
                <input .../>
            </div>
        </div>
    </div>
</template>

component.spec.vue

describe('TheEditResourceFile', () => {
  let wrapper
  let defaultParams

  
  beforeEach(() => {
    defaultParams = {
      localVue,
      i18n,
      router,
      store,
      propsData: {
        args: {
          title: '',
          description: '',
        },
        type: 'file',
      },
    }
    wrapper = shallowMount(TheEditResourceFile, defaultParams)
  })

  it('should trigger the save method', () => {
      const saveSpy = jest.spyOn(wrapper.vm, 'save')

      wrapper.find('.is-success').trigger('click')

      expect(saveSpy ).toBeCalled()
    })
})

Please let me know if further clarification is required.

Thank you for your assistance.

Edit: Incorrect code provided for failed test.

Answer №1

  1. When using Vue 2, make sure to mock/spy on the component method within the .methods definition rather than on the wrapper before mounting. (Note: In Vue 3.2.31 and later, this order is no longer necessary for methods called post-mount.)

  2. Ensure that the trigger('click') call is awaited as it triggers the bound method asynchronously.

  3. Since a spy always returns truthy values, using toBeTruthy() as an assertion has no meaning. Instead, validate with toHaveBeenCalled() on the spy.

it('should trigger the save method', () => {
  1️⃣
  const saveSpy = jest.spyOn(TheEditResourceFile.methods, 'save')
  wrapper = shallowMount(TheEditResourceFile, defaultParams)

  2️⃣
  await wrapper.find('.is-success').trigger('click')

  3️⃣
  expect(saveSpy).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

Hide the div element once the timer runs out

Struggling to make a timer disappear when it reaches 00:00, every attempt results in the div being hidden immediately. Check out the code snippet I've been working with: $(document).ready(function(e) { var $worked = $("#worked"); function upd ...

Navigating through JSON arrays with Node.js

I have been given the task of iterating through a complex JSON file that contains an array of JSON objects. I am finding it difficult to access the array object within the JSON file. Specifically, I need to access the "class-name" object from the JSON f ...

Unable to transform socket.io event into Bacon EventStream

After successfully binding the event on socket.io, the following code was executed: io = require('socket.io')() io.on 'connection', (socket) -> console.log socket.id io.listen 3000 An attempt was made to convert the socket.io ...

Saving Selected Radio button values into Jquery Array

In my HTML table, there are multiple rows with radio buttons representing the sexes in the 0th position of <td>. I am trying to store the values of sex (either 1 or 0) in an array. Below is a snippet for a table with 3 rows. HTML Code: <table> ...

Exploring the capabilities of the Next.js router and the useRouter

import { routeHandler } from "next/client"; import { useRouteNavigator } from "next/router"; const CustomComponent = () => { const routerFromHook = useRouteNavigator(); } export default CustomComponent; Can you explain the disti ...

Tips for sending PHP variables together with a Typeahead variable

I have simplified this code as much as possible. I have a typeahead variable that is functioning correctly. However, I also need to pass two unrelated variables $php_var1 and $php_var2 along with the typeahead variable. These PHP variables are initialized ...

Error: Unable to initialize monthSelectPlugin as a constructor while trying to utilize the Flatpickr plugin

I'm trying to incorporate the monthSelectPlugin for flatpickr in a Rails application. I have it specified in my importmap like this: pin "flatpickr/dist/plugins/monthSelect", to: "https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/emai ...

Running multiple Karma and Jasmine projects within a single solution efficiently (and the necessity for Jenkins integration)

In our extensive solution, we are managing various projects with Karma & Jasmine Tests. Utilizing Jenkins for continuous integration, our goal is to streamline the process by running the Karma execute command just once. This means eliminating the need to m ...

Verify WTForm after dynamic updates to select field options with Jquery

As I work on developing a flask application, I have successfully created a form using WTForms. This form consists of two SelectFields (dropdowns) and a submit button. My goal is to make the dropdowns dynamic - meaning that when the user selects an option f ...

What is the process for refreshing information in VueJS?

<script> export default { data() { return { data: {}, dataTemp: {} } }, methods: { updateData() { let queries = { ...this.$route.query } this.data = { ...this.data, pID: queries.pid, s ...

NPM Package Encountering Module Parsing Issue

I encountered a strange error while building my project with Webpack. The error is related to the Got package that I am trying to import from its `package.json` file. Module parse failed: .../node_modules/got/package.json Unexpected token (2:8) You may ne ...

When new text is added to Div, the first line is not displayed

One of the divs on my page has an ID of "TrancriptBox" and contains multiple lines of text. When I scroll through it on my iPad, everything works fine. However, if I scroll and then change the text within that div, it doesn't display the first line o ...

Data Binding in AngularJS appears to be non-functional

Experimenting with AngularJS, I created a small code snippet that doesn't seem to bind data properly. Here is the HTML and JS code for those who prefer not to visit the provided link: first.html <!doctype html> <html ng-app="firstApp"> & ...

Hide multiple divs with similar ids in JQuery when clicking outside of them

Is there a way to hide all div elements with similar id if none of them are clicked on? Currently, my code only works for the first div because I am using index[0] to retrieve the id. How can I make it work for all ids? Below is the code snippet: $(win ...

Utilizing a material-ui button within a React application as the trigger for a Popup using MuiThemeProvider

I want to trigger a Popup in React using a button with a custom theme: <PopUp modal trigger={ <MuiThemeProvider theme={buttonTheme}> <Button variant="contained" color="secondary">Excluir& ...

Angular JS - Selecting Directives on the Fly

I'm currently developing an application where users can choose from various widgets using a graphical user interface (GUI). My plan is to integrate these widgets as angular directives. THE CONTROLLER $scope.widgets = ['one', 'two' ...

Encountering a JavaScript error due to an erroneous character when trying to parse

I need to convert a `json` string into an object format that is extracted from a `.js` file. Here is the `JSON` string located in `document.js`: [ { "type": "TableShape", "id": "63c0f27a-716e-804c-6873-cd99b945b63f", "x": 80, ...

What is the correct way to utilize the Vuex mutation payload object effectively?

I have two input fields where I can enter a value to search for either the name of a company or the location. The search functionality works when only one argument is provided in the foundJobs mutation and action. However, when the payload contains an obje ...

Responsive design issues with Bootstrap 3 box layout

I am currently working on creating a bootstrap4 layout that includes three boxes arranged side by side on a wide screen. However, my goal is to ensure that if the screen size decreases, the red and green boxes always stay adjacent to each other while the b ...

VueJS: Send all unspecified attributes to child component in the same way as using v-bind="$props"

I am looking for a way to receive any props passed by the parent component into the child component without explicitly mentioning them in props:[]. This is because I may not always know which props will be bound. Parent component <template> <di ...