[Vuejs]-How to make unit test "location.href" by jest Vue.js

0👍

window.location.href gives you the URL of the current page;

window.location.pathname will give you the path name of the current page, which I believe, is what you are looking for.

0👍

This is my Solution!

import { shallowMount } from '@vue/test-utils'
import redirectComponent from '../components/RedirectComponent.vue'

describe('Redirect component', () => {
  const wrapper = shallowMount(redirectComponent)

  Object.defineProperty(window, 'location', {
    value: {
      href: 'http://localhost',
    },
    configurable: true,
  });

  it('Redirect By Click Test', () => {
    wrapper.find('.target').trigger('click');
    expect(window.location.href).toEqual('/home');
  })
})

Leave a comment