Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RouterLinkStub doesn't seem to supports new slot api? #1803

Open
existe-deja opened this issue Mar 8, 2021 · 7 comments
Open

RouterLinkStub doesn't seem to supports new slot api? #1803

existe-deja opened this issue Mar 8, 2021 · 7 comments
Labels

Comments

@existe-deja
Copy link

Subject of the issue

I'm testing a Tab handler component. It's highly coupled with router-link. Since v3.1.0 it's recommended to use the slot api to handle complex templates.
I have to test the behavior of my logic wich depends on isActive and isExactActive slot props.
Currently the RouterLinkStub component doesn't work with the slot api.

Steps to reproduce

components/ToTest.vue

<template>
  <ul>
    <router-link v-slot="{ href, navigate, isActive }" :to="{ name: 'index' }" custom>
      <li :class="{ 'class-logic-to-test': isActive && foo }">
        <a :href="href" @click="navigate">Some link</a>
      </li>
    </router-link>
  </ul>
</template>

<script>
export default {
  name: 'ToTest',

  props: {
    foo: {
      type: Boolean,
      default: true
    }
  }
}
</script>

tests/ToTest.spec.js

import { mount, RouterLinkStub } from '@vue/test-utils'
import ToTest from '@/components/ToTest.vue'

describe('ToTest', () => {
  test('is a Vue instance', () => {
    const wrapper = mount(ToTest, {
      propsData: {
        foo: true
      },
      stubs: {
        RouterLink: RouterLinkStub
      }
    })
    expect(wrapper.html()).toContain('class-logic-to-test')
  })
})

Expected behaviour

The RouterLinkStub component should render the content of the slot. Wich is with the exemple:

<li class="class-logic-to-test">
  <a href="index">Some Link</a>
</li>

Actual behaviour

The component renders:

<a custom=""></a>

Possible Solution

Rewrite the RouterLinkStub component to support the custom props and the slot api

@lmiller1990
Copy link
Member

Seems like a bug - probably need to dive into the RouterStub and see what's going on.

@mtlehtin
Copy link

Same here. I'm using the new slot api to create custom behaviour.

So in the meanwhile I extended the router-stub for my purposes to something like this. You may need to add custom-prop and some logic to the slot props if needed.

import { RouterLinkStub } from '@vue/test-utils'

// ...

const stubs = {
  RouterLink: {
    ...RouterLinkStub,
    render() {
      return this.$scopedSlots.default({
        href: this.to,
        navigate: jest.fn(),
        isActive: false
      })
    }
  }
}

@lmiller1990
Copy link
Member

lmiller1990 commented Nov 29, 2021

Sorry guys, all my time is spent on VTU v2 and Vue Jest lately.

I wonder if #1877 will fix this?

Did anyone get to look at this? Happy to give some pointers, I'd love to see this fixed but I can't look at it right now.

@BrevAlessio
Copy link

As of today, in 1.3.4, this is not working.

@ebisbe
Copy link
Collaborator

ebisbe commented Feb 15, 2023

@BrevAlessio I think that you can't expect it to be fixed in VTU1 ( sadly ). Based on the last comments from @lmiller1990 in #1564 (comment) .

So Lachlan, Can I mark this PR as wontfix and close #1877 as it's all related to stubs?

@lmiller1990
Copy link
Member

I think this is actually pretty easy to fix, we'd just need to rework https://github.com/vuejs/vue-test-utils/blob/dev/packages/test-utils/src/components/RouterLinkStub.js to do whatever the OP wants - this is kind of different from the others stubs, since we ship a specific implementation, that's apparently out of date.

@ebisbe
Copy link
Collaborator

ebisbe commented Feb 16, 2023

Ok, then I can have a look once I'm back

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants