VUE开玩笑单元测试,可以从组件库中进行测试或渲染组件

发布于 2025-01-22 20:29:17 字数 1686 浏览 0 评论 0原文

我们有一些复杂的形式,取决于登录方式,我们呈现不同的按钮。

但是,该组件来自通过NPM安装的库。因此,我在开玩笑时遇到困难。

有没有办法渲染 /加载组件以便我可以测试正确的组件?

describe('P1 buttons - show the correct buttons for credential type and registration status', () => {
  beforeEach(async () => {
    await wrapperBeforeEach();
  });
  test('expect button for OPEN_BANKING_UK_MANUAL to be visible', async () => {
    currentRegistrationDetails.credentials = institutionP1UkAutoBarclays;
    wrapper.vm.currentRegistrationDetails.platform = 'P1';
    await wrapper.vm.$nextTick();

    // wont work if Button is component
    const button = wrapper.find('[data-test-id="button-p1-download-ssa"]');

    // this would work if the Button component was local to the project
    const buttomExternal = wrapper.findComponent(Button);

    const registrationP1 = wrapper.find('.registration-p1');
    console.log(registrationP1.html());

    // this fails as Button doesn't render at all.
    expect(buttomExternal.isVisible()).toBe(true);
  });
});

来自console.log(registrationp1.html()))的HTML;是这样 -

  <div class="form-actions mb-5">
    <div class="form-actions-content">
      <!----> 
      <!---->
    </div>
  </div>

出现我们的应用程序中的按钮

      <!----> 
      <!---->

在使用我们的组件库的同时,我们不断地

。实际按钮标记是

        <Button
          data-test-id="button-p1-download-ssa"
          type="primary"
          class="primary button"
          :is-loading="savingSsa"
          :on-click="downloadTheSsaFromModal"
          >Request the SSA</Button
        >

We have some complex forms and depending on the login we render different buttons.

However the component comes from a library installed via NPM. So I'm having trouble reaching them in Jest.

Is there a way to render / load the components so I can test the right ones are present?

describe('P1 buttons - show the correct buttons for credential type and registration status', () => {
  beforeEach(async () => {
    await wrapperBeforeEach();
  });
  test('expect button for OPEN_BANKING_UK_MANUAL to be visible', async () => {
    currentRegistrationDetails.credentials = institutionP1UkAutoBarclays;
    wrapper.vm.currentRegistrationDetails.platform = 'P1';
    await wrapper.vm.$nextTick();

    // wont work if Button is component
    const button = wrapper.find('[data-test-id="button-p1-download-ssa"]');

    // this would work if the Button component was local to the project
    const buttomExternal = wrapper.findComponent(Button);

    const registrationP1 = wrapper.find('.registration-p1');
    console.log(registrationP1.html());

    // this fails as Button doesn't render at all.
    expect(buttomExternal.isVisible()).toBe(true);
  });
});

The HTML from console.log(registrationP1.html()); is this —

  <div class="form-actions mb-5">
    <div class="form-actions-content">
      <!----> 
      <!---->
    </div>
  </div>

The buttons should appear between the

      <!----> 
      <!---->

We keep running into these issues with our applications whilst using our component library.

The actual button markup is

        <Button
          data-test-id="button-p1-download-ssa"
          type="primary"
          class="primary button"
          :is-loading="savingSsa"
          :on-click="downloadTheSsaFromModal"
          >Request the SSA</Button
        >

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2025-01-29 20:29:17

我认为您只需要测试道具与npm依赖关系正确绑定。测试其他库中的按钮不在您的范围中。

I think you only have to test that the props are bound correctly with the npm dependency. It's not in your scope to test that the Button from an other library is working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文