suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Vuejs-Unit-Test-Problem: Text kann auf leerem DOMWrapper nicht aufgerufen werden

Ich verwende Vue-Test-Utils, um eine Komponente zu testen, und habe beim Versuch, Text zu testen, diese Fehlermeldung erhalten. Auf leerem DOMWrapper kann kein Text aufgerufen werden.

Aktualisierter Code. „RangeError: Maximale Aufrufstapelgröße überschritten“ im atable.spec.ts-Test.

atable.spec.ts

import ATable from "../components/ATable.vue";
import { IPatientResponse } from "@/types/patients";
import { shallowMount, RouterLinkStub } from "@vue/test-utils";

it("renders proper texts.", () => {
  const wrapper = shallowMount(ATable, {
    props: {
      patients: [
        {
          id: 1,
          firstName: "arpit",
          lastName: "satyal",
          email: "arpited7@gmail.com",
          contact: "12345",
          address: "ktm",
          dob: "1998-07-29",
          allergies: ["Pet allergies"],
        },
      ] as IPatientResponse[],
    },
    stubs: {
      RouterLink: RouterLinkStub,
    },
    scopedSlots: {
      bodyCell: `
          <template slot-scope="{ column, record }" v-if="column.key === 'firstName'">
          <router-link :to="{ name: 'PatientProfile', params: { id: record.id } }">
            <div id="test-patient">{{ record.firstName }} {{ record.lastName }}</div>
          </router-link>
        </template>`,
    }
  });
  const patientNameSelector = wrapper.find("#test-patient");
  // see if the message renders
  expect(patientNameSelector.text()).toEqual("arpit satyal");
});

Tabellenansicht Refaktorieren Sie die Tabellenkomponente in eine eigene Komponente. Es erhält nun Requisiten von der Dashboard-Komponente.

<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <section>
    <a-table :columns="columns" :data-source="patients" :pagination="false">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'firstName'">
          <router-link :to="{ name: 'PatientProfile', params: { id: record.id } }">
            <span id="test-patient">{{ record.firstName }} {{ record.lastName }}</span>
          </router-link>
        </template>
        <template v-else-if="column.key === 'specialAttention'">
          <span class="pointer">
            <EyeOutlined
              v-if="record.specialAttention"
              @click="markAsSpecial(record, false)"
              class="iconStyle"
            />
            <EyeInvisibleOutlined
              v-else
              @click="markAsSpecial(record, true)"
              class="iconStyle"
            />
          </span>
        </template>

        <template v-else-if="column.key === 'action'">
          <span class="pointer">
            <router-link :to="{ name: 'UpdatePatient', params: { id: record.id } }">
              <EditOutlined style="margin-right: 10px" class="iconStyle" />
            </router-link>
            <a-divider type="vertical" />
            <DeleteOutlined @click="deletePatient(record.id)" class="iconStyle" />
            <a-divider type="vertical" />
          </span>
        </template>
      </template>
    </a-table>
  </section>
</template>

<script lang="ts">
import {
  EditOutlined,
  DeleteOutlined,
  EyeInvisibleOutlined,
  EyeOutlined,
} from "@ant-design/icons-vue";
import { defineComponent } from "@vue/runtime-core";

const columns = [
  {
    title: "Name",
    dataIndex: "firstName",
    key: "firstName",
    align: "center",
  },
  {
    title: "Contact",
    dataIndex: "contact",
    key: "contact",
    align: "center",
  },
  {
    title: "Address",
    dataIndex: "address",
    key: "address",
    align: "center",
  },
  {
    title: "DOB",
    key: "dob",
    dataIndex: "dob",
    align: "center",
  },
  {
    title: "Special Attention",
    key: "specialAttention",
    align: "center",
  },
  {
    title: "Actions",
    key: "action",
    align: "center",
  },
];
export default defineComponent({
  props: ["deletePatient", "markAsSpecial", "patients"],
  components: {
    EditOutlined,
    DeleteOutlined,
    EyeInvisibleOutlined,
    EyeOutlined,
  },
  data() {
    return {
      columns,
    };
  },
});
</script>


P粉204136428P粉204136428390 Tage vor690

Antworte allen(1)Ich werde antworten

  • P粉697408921

    P粉6974089212023-11-01 13:16:58

    您正在测试Dashboard组件并尝试检查ATable)组件渲染了传递给的槽内容它。这是错误的。 考虑到 ATable 组件,在测试 Dashboard 组件时您应该检查的是 ATable 组件是否刚刚渲染。就是这样。

    // In Dashboard.spec.js
    it("renders ATable component.", () => {
      const ATableStub = {
        template: '<div>ATableStub</div>' 
      }
        const wrapper = shallowMount(Dashboard, {
          stubs: {
            ATable: ATableStub
          },
          data() {
            return {
              patients: [
                {
                  id: 1,
                  firstName: "arpit",
                  lastName: "satyal",
                  email: "arpited7@gmail.com",
                  contact: "12345",
                  address: "ktm",
                  dob: "1998-07-29",
                  allergies: ["Pet allergies"],
                },
              ] as IPatientResponse[],
            };
          },
        });
    wrapper.findComponent(ATableStub).exists.toBe(true);
      });
    });

    您应该将测试 ATable 的槽内容留给测试 ATable 组件,而不是仪表板 组件。

    // In ATable.spec.js
    import { shallowMount, RouterLinkStub } from '@vue/test-utils'
    
      it("renders proper texts.", () => {
        const wrapper = shallowMount(ATable, {
          stubs: {
            RouterLink: RouterLinkStub
          },
          scopedSlots: {
            bodyCell: `
              <template slot-scope="{ column, record }" v-if="column.key === 'firstName'">
              <router-link :to="{ name: 'PatientProfile', params: { id: record.id } }">
                <div id="test-patient">{{ record.firstName }} {{ record.lastName }}</div>
              </router-link>
            </template>`
          },
          data() {
            return {
              patients: [
                {
                  id: 1,
                  firstName: "arpit",
                  lastName: "satyal",
                  email: "arpited7@gmail.com",
                  contact: "12345",
                  address: "ktm",
                  dob: "1998-07-29",
                  allergies: ["Pet allergies"],
                },
              ] as IPatientResponse[],
            };
          },
        });
        const patientNameSelector = wrapper.find("#test-patient");
        // see if the message renders
        expect(patientNameSelector.text()).toEqual("arpit satyal");
      });
    });

    Antwort
    0
  • StornierenAntwort