Skip to content

Commit

Permalink
feat: wip fail
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Nov 10, 2020
1 parent fb21b9a commit 4844a7a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
12 changes: 0 additions & 12 deletions .vscode/settings.json

This file was deleted.

17 changes: 14 additions & 3 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { routeLocationKey, routerKey } from '../src'
import { config, mount } from '@vue/test-utils'
import Test from './fixtures/Test'
import { RouterLink } from 'vue-router'
Expand Down Expand Up @@ -28,15 +27,27 @@ describe('Component', () => {
config.global.provide = { foo: 'foo' }
// shouldn't this be always present for convenience?
// record string is not enough
config.global.provide![routerKey as any] = routerMock
config.global.provide![routeLocationKey as any] = routeMock
// config.global.provide![routerKey as any] = routerMock
// config.global.provide![routeLocationKey as any] = routeMock

// config.global.mocks.
})

it('works', async () => {
const wrapper = mount(Test, {
// initial location
// route: '/',
// router: false,
// global: {
// mocks: {
// $router: routerMock,
// $route: routeMock,
// },
// provide: {
// [routerKey as symbol]: routerMock,
// [routeLocationKey as symbol]: routeMock,
// },
// },
})

// await wrapper.pendingNavigation()
Expand Down
30 changes: 30 additions & 0 deletions __tests__/routeLocation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { config, mount } from '@vue/test-utils'
import { routeLocationKey, routerKey } from 'vue-router'
import Test from './fixtures/Test'

describe('Route location', () => {
const routerMock = {
push: jest.fn(),
}
const routeMock = {
fullPath: 'fullPath',
}

beforeAll(() => {
config.global.provide = config.global.provide || {}
// config.global.provide = { foo: 'foo' }
// shouldn't this be always present for convenience?
// record string is not enough
// config.global.provide[routerKey as any] = routerMock
// config.global.provide[routeLocationKey as any] = routeMock

// config.global.mocks.
})

it('creates router properties', async () => {
const wrapper = mount(Test)

expect(wrapper.vm.$router).toBeDefined()
expect(wrapper.vm.$route).toBeDefined()
})
})
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
collectCoverage: true,
collectCoverageFrom: ['<rootDir>/src/**/*.ts'],
testMatch: ['<rootDir>/__tests__/**/*.spec.ts'],
setupFiles: ['<rootDir>/jest.setup.js'],
globals: {
__DEV__: true,
__BROWSER__: true,
Expand Down
4 changes: 4 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { routerMockPlugin } = require('./src')
const { config } = require('@vue/test-utils')

config.plugins.VueWrapper.install(routerMockPlugin)
34 changes: 14 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { defineComponent, PropType, h } from 'vue'
import { createRouter, createWebHashHistory } from 'vue-router'
export { routeLocationKey, routerKey } from 'vue-router'
import { config, VueWrapper } from '@vue/test-utils'

export function routerMockPlugin(wrapper: VueWrapper<any>) {
const router = createMockedRouter()

// TODO: remove in next test utils release
config.global.plugins = config.global.plugins || []
config.global.plugins.push(router)
console.log('hey')
// config.global.provide![routerKey as any] = router
// config.global.provide![routeLocationKey as any] =

return {}
}

export function createMockedRouter() {
const router = createRouter({
Expand All @@ -23,21 +35,3 @@ export function createMockedRouter() {

return router
}

export const Component = defineComponent({
props: {
custom: Boolean as PropType<boolean>,
data: {
required: true,
type: Object as PropType<{ title: string; summary: string }>,
},
},

setup(props) {
return () =>
h(
'p',
`Custom: ${props.custom}. ${props.data.title} - ${props.data.summary}.`
)
},
})

0 comments on commit 4844a7a

Please sign in to comment.