Skip to content

Commit 53c63d6

Browse files
committed
Fix existing tests
1 parent ab7b434 commit 53c63d6

File tree

4 files changed

+74
-82
lines changed

4 files changed

+74
-82
lines changed

Diff for: src/__tests__/plugins.js

-49
This file was deleted.

Diff for: src/__tests__/vue-router-mocha.js

-14
This file was deleted.

Diff for: src/__tests__/vue-router.js

+55-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
1-
// Please notice that this example is a draft example on how to test
2-
// the router.
3-
// Related issue on Vue Test Utils: https://github.com/vuejs/vue-test-utils-next/issues/152
1+
/* eslint-disable vue/require-prop-types */
2+
/* eslint-disable vue/one-component-per-file */
43

54
import '@testing-library/jest-dom'
65
import {waitFor} from '@testing-library/dom'
6+
import {defineComponent} from 'vue'
7+
import {createRouter, createWebHistory} from 'vue-router'
78
import {render, fireEvent} from '..'
89
import App from './components/Router/App.vue'
910
import Home from './components/Router/Home.vue'
1011
import About from './components/Router/About.vue'
1112

12-
const routes = [
13-
{path: '/', component: Home},
14-
{path: '/about', component: About},
15-
]
13+
test('full app rendering/navigating from base URL', async () => {
14+
// Create a Router instance
15+
// https://next.router.vuejs.org/api/#createrouter
16+
// using the a HTML5 history.
17+
// https://next.router.vuejs.org/api/#createwebhistory
18+
const router = createRouter({
19+
history: createWebHistory(),
20+
routes: [
21+
{path: '/', component: Home},
22+
{path: '/about', component: About},
23+
],
24+
})
1625

17-
test('full app rendering/navigating', async () => {
18-
// Notice how we pass a `routes` object to our render function.
19-
const {findByText, getByText, getByTestId} = render(App, {routes})
26+
const {findByText, getByText, getByTestId} = render(App, {
27+
global: {
28+
plugins: [router],
29+
},
30+
})
2031

2132
// Vue Router navigation is async, so we need to wait until the
2233
// initial render happens
2334
expect(await findByText('You are home')).toBeInTheDocument()
35+
expect(getByTestId('location-display')).toHaveTextContent('/')
2436

2537
await fireEvent.click(getByTestId('about-link'))
2638

@@ -32,3 +44,36 @@ test('full app rendering/navigating', async () => {
3244

3345
expect(getByText('You are on the about page')).toBeInTheDocument()
3446
})
47+
48+
test('sets router initial state', async () => {
49+
const Component = defineComponent({
50+
props: ['to'],
51+
template: `<router-link :to="to">Learn More</router-link>`,
52+
})
53+
54+
const route = {
55+
name: 'routeName',
56+
path: '/',
57+
component: defineComponent({template: `<div></div>`}),
58+
}
59+
60+
const router = createRouter({
61+
history: createWebHistory(),
62+
routes: [route],
63+
})
64+
65+
const to = {name: route.name}
66+
67+
const {getByText} = render(Component, {
68+
props: {to},
69+
global: {
70+
plugins: [router],
71+
},
72+
})
73+
74+
// We need to wait for router to complete the initial navigation.
75+
await router.isReady()
76+
77+
const button = getByText('Learn More')
78+
expect(button).toHaveAttribute('href', route.path)
79+
})

Diff for: src/__tests__/vuex.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import '@testing-library/jest-dom'
2+
import {createStore} from 'vuex'
23
import {render, fireEvent} from '..'
34
import VuexTest from './components/Store/VuexTest'
45
import {store} from './components/Store/store'
@@ -10,10 +11,15 @@ import {store} from './components/Store/store'
1011
// Tests should be completely isolated from one another.
1112
// Read this for additional context: https://kentcdodds.com/blog/test-isolation-with-react
1213
function renderVuexTestComponent(customStore) {
13-
// Render the component and merge the original store and the custom one
14-
// provided as a parameter. This way, we can alter some behaviors of the
15-
// initial implementation.
16-
return render(VuexTest, {store: {...store, ...customStore}})
14+
// Create a custom store with the original one and the one coming as a
15+
// parameter. This way we can alter some of its values.
16+
const mergedStore = createStore({...store, ...customStore})
17+
18+
return render(VuexTest, {
19+
global: {
20+
plugins: [mergedStore],
21+
},
22+
})
1723
}
1824

1925
test('can render with vuex with defaults', async () => {
@@ -26,7 +32,7 @@ test('can render with vuex with defaults', async () => {
2632

2733
test('can render with vuex with custom initial state', async () => {
2834
const {getByTestId, getByText} = renderVuexTestComponent({
29-
state: {count: 3},
35+
state: () => ({count: 3}),
3036
})
3137

3238
await fireEvent.click(getByText('-'))
@@ -37,17 +43,21 @@ test('can render with vuex with custom initial state', async () => {
3743
test('can render with vuex with custom store', async () => {
3844
// This is a silly store that can never be changed.
3945
// eslint-disable-next-line no-shadow
40-
const store = {
41-
state: {count: 1000},
46+
const store = createStore({
47+
state: () => ({count: 1000}),
4248
actions: {
4349
increment: () => jest.fn(),
4450
decrement: () => jest.fn(),
4551
},
46-
}
52+
})
4753

4854
// Notice how here we are not using the helper rendering method, because
4955
// there's no need to do that here. We're passing a whole store.
50-
const {getByTestId, getByText} = render(VuexTest, {store})
56+
const {getByTestId, getByText} = render(VuexTest, {
57+
global: {
58+
plugins: [store],
59+
},
60+
})
5161

5262
await fireEvent.click(getByText('+'))
5363
expect(getByTestId('count-value')).toHaveTextContent('1000')

0 commit comments

Comments
 (0)