Skip to content

Commit 6afbf65

Browse files
authored
test: add test case for Q11 (webfansplz#544)
1 parent 0ffc04c commit 6afbf65

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

questions/11-next-dom-update/App.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { ref } from "vue"
33
44
const count = ref(0)
5+
const counter = ref(null)
56
67
function increment() {
78
count.value++
@@ -10,13 +11,12 @@ function increment() {
1011
* DOM is not yet updated, how can we make sure that the DOM gets updated
1112
* Make the output be true
1213
*/
13-
14-
console.log(+document.getElementById("counter").textContent === 1)
14+
console.log(+counter.value.textContent === 1)
1515
}
1616
</script>
1717

1818
<template>
19-
<button id="counter" @click="increment">
19+
<button ref="counter" @click="increment">
2020
{{ count }}
2121
</button>
2222
</template>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { mount } from "@vue/test-utils"
2+
import { describe, it, expect, vi } from "vitest"
3+
4+
import App from "./App.vue"
5+
6+
describe("next-dom-update", () => {
7+
it("should work'", async() => {
8+
let printLog = ''
9+
console.log = vi.fn(
10+
(log: string) => {
11+
printLog = log?.toString()?.trim()
12+
})
13+
const wrapper = mount(App)
14+
15+
expect(wrapper.text()).toMatchInlineSnapshot("\"0\"")
16+
17+
const button = wrapper.find("button")
18+
await button.trigger("click")
19+
20+
expect(wrapper.text()).toMatchInlineSnapshot('"1"')
21+
expect(printLog).toMatchInlineSnapshot('"true"')
22+
})
23+
})

0 commit comments

Comments
 (0)