It changes the page in a SPA
<router-view> // kinda like jsp include
<router-link :to="path"> // like <a>
HTML
JS
// $router functions
this.$router.push();
this.$router.replace();
// $route properties
this.$route.name;
this.$route.path;
this.$route.query;
import Vue from 'vue';
import Vuex from 'vuex';
import VueI18n from 'vue-i18n';
import VueRouter from 'vue-router'
Vue.use(VueI18n);
Vue.use(Vuex);
Vue.use(VueRouter);
so when your component uses $store, $t, $router, it won't Freak Out™️ in testing
// example:
if (this.conditionIsMet) {
this.$router.push({ name: 'somewhere' });
}
describe('push when condition is met', () => {
let vm;
beforeEach(() => {
vm = getComponent();
vm.conditionIsMet = true;
vm.$router.push = sandbox.stub();
});
...
👍
// example:
if (this.$route.name === 'somewhere') {
this.doThis();
} else {
this.doThat();
}
describe('doThis when route is somewhere', () => {
let vm;
beforeEach(() => {
vm = getComponent();
vm.$route.name = 'somewhere';
});
...
if (this.$route.name === 'somewhere') {
this.doThis();
} else {
this.doThat();
}
Not when Vue.use(Router)....
$route.name is static
🙅♀️
describe('doThis when route is somewhere', () => {
let vm;
beforeEach(() => {
vm = getComponent();
vm.$router.push({ name: 'somewhere' });
});
...
if (this.$route.name === 'somewhere') {
this.doThis();
} else {
this.doThat();
}
Yes!! but it can get messy.
🤷♀️
import Vue from 'vue';
import Vuex from 'vuex';
import VueI18n from 'vue-i18n';
import VueRouter from 'vue-router'
Vue.use(VueI18n);
Vue.use(Vuex);
Vue.use(VueRouter);
Why don't I just write my own
vue-test-utils
import { shallow } from '@vue/test-utils'
const wrapper = shallow(Component) // returns a Wrapper containing
// a mounted Component instance
wrapper.vm // the mounted Vue instance
import { shallow } from '@vue/test-utils'
const wrapper = shallow(Component, {
mocks: {},
stubs: {},
});
import { shallow } from '@vue/test-utils';
import Component from 'src/Component';
const RouterLinkStub = {
name: 'router-link-stub',
render: () => {},
props: ['to'],
};
const RouterViewStub = {
name: 'router-view-stub',
render: () => {},
};
const stubs = {
'router-link': RouterLinkStub,
'router-view': RouterViewStub,
};
const wrapper = shallow(Component, {
stubs,
});
import { shallow } from '@vue/test-utils';
import Component from 'src/Component';
const mocks = {
$router: {
currentRoute: {},
replace: () => {},
push: () => {},
routes: [], // maybe include your
// app's actual routes here
},
$route: {
path: '/',
hash: '',
params: {},
query: {},
meta: {},
name: null,
fullPath: '/',
from: {},
},
};
const wrapper = shallow(Component, {
mocks,
});
// assets.js
export const getSharedAssets = () => {
// stubs
const RouterLinkStub = {
name: 'router-link-stub',
render: () => {},
props: ['to'],
};
const RouterViewStub = {
name: 'router-view-stub',
render: () => {},
};
const stubs = {
'router-link': RouterLinkStub,
'router-view': RouterViewStub,
};
// mocks
const mocks = {
$router: {
currentRoute: {},
replace: () => {},
push: () => {},
routes: [],
},
$route: {
path: '/',
hash: '',
params: {},
query: {},
meta: {},
name: null,
fullPath: '/',
from: {},
},
};
return {
mocks,
stubs,
};
};
// mocks
const mocks = {
...
};
return {
store,
i18n,
mocks,
stubs,
};
};
// assets.js
import Vuex from 'vuex';
import VueI18n from 'vue-i18n';
import { modules } from 'app/store';
import { i18n } from 'shared/i18n';
export const getSharedAssets = () => {
// store
const store = new Vuex.Store({ modules });
// i18n
const i18n = new VueI18n({
locale: 'en-US',
fallbackLocale: 'en-US',
silentTranslationWarn: true,
missing: () => {},
});
i18n.setLocaleMessage('en-US',
require('static/i18n/en-US.json'));
// stubs
...
import { shallow } from '@vue/test-utils';
import Component from 'src/Component';
const mocks = {
$router: {
currentRoute: {},
replace: () => {},
push: () => {},
routes: [],
},
$route: {
path: '/',
hash: '',
params: {},
query: {},
meta: {},
name: null,
fullPath: '/',
from: {},
},
};
const RouterLinkStub = {
name: 'router-link-stub',
render: () => {},
props: ['to'],
};
const RouterViewStub = {
name: 'router-view-stub',
render: () => {},
};
const stubs = {
'router-link': RouterLinkStub,
'router-view': RouterViewStub,
};
const getComponent = () => {
const wrapper = shallow(Component, {
mocks,
stubs,
});
return wrapper.vm;
});
import { shallow } from '@vue/test-utils';
import Component from 'src/Component';
import { getSharedAssets } from 'test/assets';
const getComponent = () => {
const wrapper = shallow(Component, {
...getSharedAssets(),
});
return wrapper.vm;
});
describe('doThis when route is somewhere', () => {
let vm;
beforeEach(() => {
vm = getComponent();
vm.$route.name = 'somewhere';
});
...
if (this.$route.name === 'somewhere') {
this.doThis();
} else {
this.doThat();
}
👍
import {
shallow,
createLocalVue,
} from '@vue/test-utils'
import VueRouter from 'vue-router'
const localVue = createLocalVue()
localVue.use(VueRouter)
shallow(Component, {
localVue,
})