Skip to content

Commit 81283d0

Browse files
committed
mocking vue router with global.mocks
1 parent 0415226 commit 81283d0

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/App.vue

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22
<button @click="increment">Click</button>
33
<div v-if="count % 2 === 0">Count: {{ count }}. Count is even.</div>
44
<div v-if="count % 2 !== 0">Count: {{ count }}. Count is odd.</div>
5+
6+
<div>PostID: {{ postId }}</div>
57
</template>
68

79
<script>
8-
import { computed } from "vue";
9-
import { useStore } from "vuex";
1010
export default {
11-
setup() {
12-
const store = useStore();
13-
const count = computed(() => store.state.count);
14-
const increment = () => {
15-
store.commit("increment");
16-
};
11+
name: "App.vue",
12+
computed: {
13+
postId() {
14+
return this.$route.params.postId;
15+
},
1716
18-
return { count, increment };
17+
count() {
18+
return this.$store.state.count;
19+
},
20+
},
21+
methods: {
22+
increment() {
23+
this.$store.commit("increment");
24+
},
1925
},
2026
};
2127
</script>

tests/unit/example.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ const createVuexStore = () => {
1717

1818
function factory() {
1919
const store = createVuexStore();
20+
2021
return mount(App, {
2122
global: {
2223
plugins: [store],
24+
mocks: {
25+
$route: {
26+
params: {
27+
postId: "1",
28+
},
29+
},
30+
},
2331
},
2432
});
2533
}
@@ -37,4 +45,9 @@ describe("App", () => {
3745
await wrapper.find("button").trigger("click");
3846
expect(wrapper.html()).toContain("Count: 2. Count is even");
3947
});
48+
49+
it("render router", async () => {
50+
const wrapper = factory();
51+
expect(wrapper.html()).toContain("PostID: 1");
52+
});
4053
});

0 commit comments

Comments
 (0)