Troubleshooting Vue Unit Tests: Issue with Setting Input Values

I am currently utilizing Vue with typescript and making an effort to perform a unit test on the input value for a login page. The issue lies in the fact that after setting the input value, it does not return as expected – instead, it comes back empty ("") and I am struggling to identify the cause.

Here is the code from Homepage.vue:

<form class="form" @submit.prevent="submit">
        <input
          id="userNameTextInput"
          v-model="username"
          placeholder="Name"
          minlength="2"
        /><br />
        <input
          id="passwordTextInput"
          type="password"
          v-model="password"
          placeholder="Password"
          minlength="8"
        /><br />
        <button
          id="submitButton"
          color="white"
          background="darkslateblue"
          type="submit"
          @click="submit"
        >
          Submit
        </button>
        <div id="incorrectLoginBlock" v-show="toggleIncorrectLogin">
          <p>Incorrect Membername or Password</p>
        </div>
      </form>

Also, here's the section of code from Homepage.spec.ts:

import HomePage from '@/components/HomePage.vue';
import {mount, VueWrapper} from '@vue/test-utils';

describe('Homepage.vue', () => {
  let wrapper: VueWrapper<any>;

  beforeEach(() => {
    wrapper = mount(HomePage);
  });

  it('Checking for incorrect login message display', async () => {
        const userTextValueInvalid = wrapper.find('#userNameTextInput');
        userTextValueInvalid.setValue('wrongPass');
        expect(userTextValueInvalid.element.textContent).toContain(
        'incorrectName',
    );

This is the error being encountered:

expect(received).toContain(expected) // indexOf

Expected substring: "incorrectName"

Received string: ""

const userTextValueInvalid = wrapper.find('#userNameTextInput');

userTextValueInvalid.setValue('wrongPass');

expect(userTextboxElementAccessDenied.element.textContent).toBe('incorrectName');

Answer №1

Here is the working code:

describe("Login validation", async () => {
  const usernameInput = wrapper.find("#username");
  await usernameInput.setValue("user123");

  expect(
    (usernameInput.element as HTMLInputElement).value
  ).toContain("user123");
});

Answer №2

const userInput = wrapper.find('input');
const userElement = userInput.element as HTMLInputElement;

let userInputValue = 'User input value';
await userInput.setValue(userInputValue);
expect(userElement.value).toEqual(userInputValue);

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

Testing a Django command feature by making an external API call and patching it with two different datasets

As part of my testing process, I am experimenting with a Django command that interacts with an external API called redminelib. While my mock class and patch in the tests are functioning well individually, I encountered an issue when attempting to patch th ...

Choose all or none of the items from the list with a single click - v-list-item-group

I am interested in incorporating Vuetify's v-list-item-group component into my Vue application. This list is intended to represent nodes that are related to a graph, allowing users to select none, some, or all of them and delete the selected nodes. T ...

"Changing the name of a symbol that is automatically imported from an internal library in

Within my module, I find myself using the Element class that is implicitly imported from the "dom" internal library. However, I also need to create my custom Element class within the same module. This presents a problem due to the name collision and poten ...

Tips for extracting information from a TypeScript JSON document

Hey there, I'm currently having trouble understanding how to retrieve data from a JSON file. environment.ts: export const environment = { production: false, urlListBooks: "/assets/list-books.json", urlGetBooks: "/assets/edit- ...

"Discover the step-by-step process of building a vue.js3 application with typescript, vue-router, and vuex without relying on

I have been assigned the task of developing a Vue3 application with TypeScript support using Vuex for state management and vue-router for basic routing. However, I am not allowed to use vue-cli for this project. Here is my current code: <head> & ...

The number input component that is meant to be reusable is experiencing functionality issues within the NUXT framework

I have a reusable input component that is being used in various places. Everything works well with the number input, but the issue arises when I completely clear the input. This action triggers a warning message in the console: [Vue warn]: Invalid prop: t ...

What is the process for uninstalling Vue CLI 2 from my system?

I am looking to start a new vue project using @vue/cli . It seems that the vue/cli has been updated to version 3. The documentation I found is located at and I am currently running ubuntu 17.10, attempting to uninstall vue-cli using npm uninstall vue-cl ...

Automated mocking of controller and service dependencies in Angular

When working with Angular unit testing, manually mocking and stubbing all dependencies can be quite cumbersome, especially when only generic mocks are needed. Additionally, whenever the dependency list for a service/controller changes, tests may break due ...

Incorrect format option selected in the vue-datetime component

I have come across an issue with the vue-datetime plugin where the format option does not seem to be working correctly. Here is the code snippet that demonstrates the problem: <datetime type="date" v-model="formData.testDate" input-class="form-control" ...

Ionic 2 faced an unresolved core-js dependency issue

Recently, I started working on a new Ionic 2 project and encountered an issue when trying to incorporate https://github.com/afrad/angular2-websocket. Upon installation, I received the warning message: UNMET PEER DEPENDENCY core-js@^2.4.1 The template pro ...

Display only distinct items in a v-for loop in Vue.js

I am attempting to show icons based on specific v-for and v-if conditions. However, the icons are appearing multiple times when I only want them to display once. I attempted using v-if = 'index === 0', but this did not resolve the issue. <di ...

The condition will be false if a number is present, even if it is zero

I am facing an issue with a class containing an optional field called startDateHour: export class Test { startDateHour?: number; // more fields, constructor etc. } I need to perform an action only if the startDateHour exists: if (test.startDateHour ...

Is it possible to assign a property value to an object based on the type of another property?

In this illustrative example: enum Methods { X = 'X', Y = 'Y' } type MethodProperties = { [Methods.X]: { x: string } [Methods.Y]: { y: string } } type Approach = { [method in keyof Method ...

Tips for passing props to multiple child components in Vue?

In Vue, I'm curious about how to achieve the same functionality as declaring an object in React and passing it to multiple child components. const shared_props = { prop1: 'value1', prop2: 'value2', prop3: 'value3', ...

Error in typography - createStyles - 'Style<Theme, StyleProps, "root"

I'm encountering an error in a small React app. Here is a screenshot of the issue: The project uses "@material-ui/core": "4.11.3". In the codebase, there is a component named Text.tsx with its corresponding styles defined in Text.styles.tsx. The styl ...

One way to incorporate type annotations into your onChange and onClick functions in TypeScript when working with React is by specifying the expected

Recently, I created a component type Properties = { label: string, autoFocus: boolean, onClick: (e: React.ClickEvent<HTMLInputElement>) => void, onChange: (e: React.ChangeEvent<HTMLInputElement>) => void } const InputField = ({ h ...

What is the most efficient way to update a specific element in a redux/vuex store?

What is the most efficient way to update an element(hash) in a list within a store (redux, vuex) using O(1) complexity? The order of the elements must be maintained as I will be adding/removing elements frequently. Updates will occur every millisecond, re ...

Implementing a Typescript hook using useContext along with an uninitialized context object

I am currently attempting to develop a custom hook called useAuth in React 17 with TypeScript. While my solution is somewhat functioning, it requires the following syntax when utilizing the hook methods: const auth = useAuth(); // Do other stuff ...

What is the best way to efficiently integrate Google Analytics and Facebook Pixel into Vue.js dynamically?

I am currently working on a Vue.js project with multiple pages for different customers, such as www.sample.com/shopA, www.sample.com/shopB, www.sample.com/shopC. All of these shops share the same file, and I am looking to implement a way for each customer ...

Can someone confirm if I am importing this png file correctly? I am encountering an error with Vite, here is my code

Error: TypeScript+ React + Vite [plugin:vite:import-analysis] Failed to find import "./assets/heropic.png" in "src\components\Hero.tsx". Are you sure the file exists? Hello fellow developers! I am new to working with react and typescript. Curren ...