question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Test fails when <router-view></router-view> is an element inside the component

See original GitHub issue

For the following component the test fails but succeeds when i remove the <router-view></router-view>

Teams.vue

<template>
    <div class="teams card">
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Teams</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="team in teams">
                        <td>
                            <span v-show="!team.editing">{{team.name}}</span>
                            <input type="text" class="form-control" v-model="team.name" v-show="team.editing" />
                        </td>
                        <td>
                            <router-link :to="modalFor(team)">Edit</router-link>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <router-view></router-view>
    </div>
</template>

<script>
  import { Team } from '../../../models/team'
  import Modal from '../../../components/Modal'
  export default {
    components: {Modal},
    name: 'teams',
    mounted () {
      // load all teams
      Team.all().then(teams => {
        // disable editing mode for all teams
        teams.forEach(team => { team.editing = false })
        this.teams = teams
      })
    },
    data () {
      return {
        teams: [],
      }
    },
    methods: {
          modalFor (team) {
              return '/admin/teams/' + team.id
      }
    }
  }

Teams.spec.js

import Teams from '@/pages/dashboard/admin/Teams'
import { mount } from 'avoriaz'

describe('Teams.vue', () => {
  it('should render correct contents', () => {
    const wrapper = mount(Teams)
    const tableHeader = wrapper.find('.table tr th')[0]
    expect(tableHeader.text()).to.equal('Teams')
  })
})

The test result with the <router-view> inside the component:

image

The test result without the <router-view>

image

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
eddyerburghcommented, May 18, 2017

router-view is injected as a global component when you Vue.use() vue-router.

You need to register a mock global component:

// mock component
const routerView = {
  name: 'router-view',
  render: h => h('div'),
};

// register mock component
Vue.component('router-view', routerView);
1reaction
eddyerburghcommented, Jul 19, 2017

You can mock $route 😄

describe('Fixed.vue', () => {
  it('is a component', () => {
    const $route = {name: 'some name'}
    Vue.component('router-view', routerView)

    const wrapper = shallow(Fixed, {
      globals: { $route }
    })
    expect(wrapper.isVueComponent).toBe(true)
  })
})

globals adds properties to the Vue prototype, so they are accessible inside each vm.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generated test fails when unit testing component inside the ...
I just encountered the same problem. Using Nrwl's Nx to scaffold a new application plus a "feature-shell" library (the design pattern ...
Read more >
Testing React components - Apollo GraphQL Docs
This article describes best practices for testing React components that use Apollo Client. The examples below use Jest and React Testing Library, ...
Read more >
Testing Components – Testing Angular
Introduction to testing Angular Components with Angular's TestBed. ... For accessing elements in the DOM, Angular has another abstraction: ...
Read more >
router view
If not passed then will be determined based on whether its element has ... The `router-link` Component in Vue. vue and change the...
Read more >
How to Test React Components: the Complete Guide
The tests will pass if your hypothesis is correct and fail if it is wrong. Unlike your react components, your tests are not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found