Confirm that a new class has been assigned to an element

I'm having trouble creating a unit test for a Vue.js component where I need to check if a specific CSS class is added to the template.

Below is the template code:

<template>
  <div id="item-list" class="item-list">
    <table id="item-list-lg" class="table table-hover nomargin hidden-xs hidden-sm hidden-md">
      <thead>
        <tr>
          <th>Name</th>
          <th>Included modules</th>
        </tr>
      </thead>
      <tbody>
        <tr v-bind:id="'list-lg-item-' + item.id"
            v-for="item in items"
            v-bind:key="item.id"
            v-bind:class="itemClass(item)">
          <td class="list-item-name">{{item.name}}</td>
          <td class="list-included-parts">
            <span v-for="part in item.parts" :key="part.id">{{part.name}}, </span>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

Here's the Component class (Typescript):

import { Component, Prop, Vue } from 'vue-property-decorator';
import { Item, Items } from '@/models/Item';

@Component
export default class ItemList extends Vue {
    @Prop({required: false}) private items: Item[] = Items;

    public itemClass(item: Item): any {
        return {
            'list-item-details': true,
            'list-global-item': item.isGlobalItem(),
        };
    }
}

The code looks correct as the items are properly highlighted in the component during runtime. However, the unit test fails with the following error message:

Error: [vue-test-utils]: find did not return tr#list-lg-item-id.1, cannot call classes() on empty Wrapper

This is my test code (Typescript):

describe('ItemList.vue', () => {
  const wrapper = shallowMount(ItemList, {
    propsData: { items: Items },
  });

  it('highlights global items in the list', () => {
    Items
      .filter((i) => i.isGlobalItem())
      .map((i) =>
        // For example, list-item-id.1
        expect(wrapper.find(`tr#list-lg-item-${i.id}`).classes())
          .to.contain('list-global-item'));
  });
});

I have tried using find() with just the id instead of a tr element with that id without success. Additionally, when I output the HTML from the wrapper in the test, I can see that the tr element with the correct id is present.

<div data-v-63e8ee02="" id="item-list" class="item-list">
  <table data-v-63e8ee02="" id="item-list-lg" class="table table-hover nomargin hidden-xs hidden-sm hidden-md">
    <thead data-v-63e8ee02="">
      <tr data-v-63e8ee02="">
        <th data-v-63e8ee02="">Name</th>
        <th data-v-63e8ee02="">Included parts</th>
      </tr>
    </thead>
    <tbody data-v-63e8ee02="">
      <tr data-v-63e8ee02="" id="list-item-id.1" class="list-item-details list-global-item">
        <td data-v-63e8ee02="" class="list-item-name">Foo</td>
        <td data-v-63e8ee02="" class="list-included-parts">
          <span data-v-63e8ee02="">Bar, </span>
          <span data-v-63e8ee02="">Baz, </span>
          <span data-v-63e8ee02="">Qux, </span>
          <span data-v-63e8ee02="">Quux, </span>
        </td>
      </tr>
    </tbody>
  </table>
</div>

What could be causing this issue? Could it be related to the fact that the id attribute is set dynamically?

Answer №1

The reason for this issue was due to the id tags containing periods in the generated values, requiring them to be escaped in the selector strings, such as list-item-id\.1.

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

When a Javascript function marked as async is executed, it will return an object

Async function is returning [object Promise] instead of the desired real value. Interestingly, I can see the value in the console log. It seems like this behavior is expected from the function, but I'm unsure how to fix my code. This code snippet is ...

Exploring the JSON Array in Angular5 with the power of ngFor

Currently, I am working on a project using Angular5 and encountering an issue with the *ngFor directive. The model class I have defined looks like this: export class FetchApi { value: Array<String>; api_status: string; api_version: string; d ...

The hyperlinks are no longer functioning correctly, such as the example: https://unpkg.com/[email protected]/dist/vue.min.js

I'm running into issues with the CDN link from unpkg not working anymore. Even older versions are failing to load. https://unpkg.com/[email protected] /dist/vue.min.js Is there an alternative link that I can use instead? Thanks! <script src=&qu ...

What is the best way to archive data from CMS within Vue3 for quick access?

I have constructed a basic Content Management System using Laravel Nova within a Vue3/Laravel8 app. As a novice, I am utilizing axios.get to retrieve image links and markdown from a database and then assigning it to a reactive state in this manner: <scr ...

Transforming Bootstrap using Typescript styled components

I have been working on customizing the Navbar in my React app using Typescript's styled components. However, I am facing difficulties in restyling the default styles from Bootstrap that I have copied. Here is an example of the React Bootstrap Navbar c ...

Tips for preventing unforeseen consequences in computed properties with VueJS

Currently, I am facing a challenge prefilling a form with data from a Vuex store. The code provided seems to give the expected result, but I am aware that this might not be the correct approach. My experience with Vue/Vuex is still limited. Since the input ...

Responsive breakpoints in TailwindCSS seem to have an issue with applying padding and margin styles properly

Currently, I am tackling a project that involves creating a responsive design with Tailwind CSS on nuxtjs. Has anyone encountered the issue of py-8 applying to both breakpoints? If so, please share your insights! Here is how I have structured the compone ...

Does installing the global npm package (@vue/cli) on Node v10.9 also automatically install Node v10.8?

This situation is quite puzzling and I have yet to find a definitive answer. It seems as though it was meant to happen this way, but it still feels odd. Here's the sequence of events: I completely removed Node from my OSX system. I then reinstalled ...

Using Vue.js to dynamically append a bound URL

Within my Vue template, I currently have the following code: <img class="workimg" v-bind:src="item.imagemobile"> I am considering appending it to: <img class="workimg" v-bind:src="/featured/item.imagemobile"> However, this syntax seems to b ...

Having trouble utilizing HTML Canvas in the newest release of Angular and TypeScript

After extensive searching on the internet, I have not been able to find any working examples of using HTML canvas in Angular. It seems that changes in syntax in Typescript and Angular's newer versions have rendered all existing solutions obsolete. I ...

Error message: NextJs throws aReferenceError when trying to access the document object on page refresh

encountered the error ReferenceError: document is not defined when attempting to refresh the page I am working on creating a component using react-quill and then calling that component within a page. This is my component : import React, { useState } from ...

What is the best way to implement a subquery using EXISTS in Knex?

I'm currently facing challenges while constructing a query using knex, specifically when it comes to incorporating the 'WHERE' clause with the condition EXISTS (SELECT * FROM caregiver_patient WHERE patient_id IN (0,1)). Below is the origin ...

Unable to connect to Vue project using IP address on different devices with Vue CLI 3 and npm

I am struggling with a project built using vue cli 3. Upon running the command "npm run serve", I receive a success message indicating that the app is running locally on http://localhost:8080/ and on the network at http://1.2.3.4:8080/. While everything wo ...

Show image using Typescript model in Angular application

In my Angular (v8) project, I have a profile page where I typically display the user's photo using the following method: <img class="profile-user-img" src="./DemoController/GetPhoto?Id={{rec.Id}}" /> However, I am considering an alternative ap ...

v-autocomplete not displaying full list of items in selection dropdown

I have a question regarding sending an array with values ranging from 1 to 100 on the v-autocomplete item props. However, when scrolling through the list, only numbers up to 40-50 are visible. <template> <v-app> <v-container> ...

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 ...

When using Framer Motion for page transitions alongside React Router DOM v6, the layout components, particularly the Sidebar, experience rerenders when changing pages

After implementing page transitions in my React app using Framer Motion and React-Router-DOM, I noticed that all layout components such as the sidebar and navbar were unexpectedly rerendering upon page change. Here's a snippet of my router and layout ...

Guidelines for creating a binary release of Node.js with native modules

Currently, I am in the midst of exploring the world of Node.js projects, delving into different bundlers and various other components. One interesting concept that came to mind is the idea of bundling Node.js into a single binary for Linux, macOS, or Windo ...

Update a specific row in a table by displaying a form modal using Vue.js

I am currently working on a table that includes an edit button along with an edit form dialog: <v-data-table v-bind:headers="headers" v-bind:items="packets" v-bind:search="search" > <template slot="items" scope="props"> < ...

Combining LiveChatInc widget with NuxtJs for seamless integration

I've been working with NuxtJS on my project and I'm attempting to integrate LiveChatInc chat using the instructions provided on this page: I've tried using middleware to insert the JS code, but I'm encountering errors from Eslint: wind ...