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

Bug: Using a real router with Composition API example is missing push #1632

Closed
N8ers opened this issue Jun 30, 2022 · 3 comments
Closed

Bug: Using a real router with Composition API example is missing push #1632

N8ers opened this issue Jun 30, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@N8ers
Copy link
Contributor

N8ers commented Jun 30, 2022

Describe the bug
In the docs there is a code example for Using a real router with Composition API. Here is the code example in it's current state:

import { mount, flushPromises } from '@vue/test-utils'
import { createRouter, createWebHistory } from 'vue-router'
import { routes } from "@/router"

let router;

beforeEach(async () => {
  router = createRouter({
    history: createWebHistory(),
    routes: routes,
  })

  router.push('/')
  await router.isReady()
});

test('allows authenticated user to edit a post', async () => {
  const wrapper = mount(Component, {
    props: {
      isAuthenticated: true
    },
    global: {
      plugins: [router],
    }
  })

  await wrapper.find('button').trigger('click')

  expect(push).toHaveBeenCalledTimes(1)
  expect(push).toHaveBeenCalledWith('/posts/1/edit')
})

In the assertion expect(push).toHaveBeenCalledTimes(1), we access the push variable, but push is not defined anywhere.

How should that be handled? Am I just missing something? I'd be happy to put up a PR for it if anyone has an answer!

@N8ers N8ers added the bug Something isn't working label Jun 30, 2022
@cexbrayat
Copy link
Member

Hi @N8ers

You're right, push is not defined here.

I suppose the test should look like:

test('allows authenticated user to edit a post', async () => {
  const wrapper = mount(Component, {
    props: {
      isAuthenticated: true
    },
    global: {
      plugins: [router],
    }
  })

  const push = jest.spyOn(router, 'push');
  await wrapper.find('button').trigger('click')

  expect(push).toHaveBeenCalledTimes(1)
  expect(push).toHaveBeenCalledWith('/posts/1/edit')
})

This is pseudo-code though, I haven't tested it.
If you're interested in opening a PR, can you check that this properly works and update the docs if that's the case?
That would be a great help!

@N8ers
Copy link
Contributor Author

N8ers commented Jun 30, 2022

Hey @cexbrayat!

Okay cool, I'm glad I didn't miss something. I can confirm that that worked for me. I'm using vitest not jest, but I'm confident it will be fine.

I'll make a PR and link it to this issue - thanks for the help 😄

@N8ers
Copy link
Contributor Author

N8ers commented Jun 30, 2022

I've submitted this PR to update the documentation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants