vModel.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import path from 'node:path'
  2. import { setupPuppeteer } from './e2eUtils'
  3. const { page, click, isChecked, html, value } = setupPuppeteer()
  4. import { nextTick } from 'vue'
  5. beforeEach(async () => {
  6. await page().addScriptTag({
  7. path: path.resolve(__dirname, '../../dist/vue.global.js'),
  8. })
  9. await page().setContent(`<div id="app"></div>`)
  10. })
  11. // #12144
  12. test('checkbox click with v-model', async () => {
  13. await page().evaluate(() => {
  14. const { createApp } = (window as any).Vue
  15. createApp({
  16. template: `
  17. <label>
  18. <input
  19. id="first"
  20. type="checkbox"
  21. v-model="first"/>
  22. First
  23. </label>
  24. <br>
  25. <label>
  26. <input
  27. id="second"
  28. type="checkbox"
  29. v-model="second"
  30. @click="secondClick"/>
  31. Second
  32. </label>
  33. `,
  34. data() {
  35. return {
  36. first: true,
  37. second: false,
  38. }
  39. },
  40. methods: {
  41. secondClick(this: any) {
  42. this.first = false
  43. },
  44. },
  45. }).mount('#app')
  46. })
  47. expect(await isChecked('#first')).toBe(true)
  48. expect(await isChecked('#second')).toBe(false)
  49. await click('#second')
  50. await nextTick()
  51. expect(await isChecked('#first')).toBe(false)
  52. expect(await isChecked('#second')).toBe(true)
  53. })
  54. // #8638
  55. test('checkbox click with v-model array value', async () => {
  56. await page().evaluate(() => {
  57. const { createApp, ref } = (window as any).Vue
  58. createApp({
  59. template: `
  60. {{cls}}
  61. <input
  62. id="checkEl"
  63. type="checkbox"
  64. @click="change"
  65. v-model="inputModel"
  66. value="a"
  67. >
  68. `,
  69. setup() {
  70. const inputModel = ref([])
  71. const count = ref(0)
  72. const change = () => {
  73. count.value++
  74. }
  75. return {
  76. inputModel,
  77. change,
  78. cls: count,
  79. }
  80. },
  81. }).mount('#app')
  82. })
  83. expect(await isChecked('#checkEl')).toBe(false)
  84. expect(await html('#app')).toMatchInlineSnapshot(
  85. `"0 <input id="checkEl" type="checkbox" value="a">"`,
  86. )
  87. await click('#checkEl')
  88. await nextTick()
  89. expect(await isChecked('#checkEl')).toBe(true)
  90. expect(await html('#app')).toMatchInlineSnapshot(
  91. `"1 <input id="checkEl" type="checkbox" value="a">"`,
  92. )
  93. await click('#checkEl')
  94. await nextTick()
  95. expect(await isChecked('#checkEl')).toBe(false)
  96. expect(await html('#app')).toMatchInlineSnapshot(
  97. `"2 <input id="checkEl" type="checkbox" value="a">"`,
  98. )
  99. })
  100. // #8579
  101. test('select click with v-model', async () => {
  102. await page().evaluate(() => {
  103. const { createApp } = (window as any).Vue
  104. createApp({
  105. template: `
  106. <p>
  107. Changed: {{changed}}
  108. </p>
  109. <p>
  110. Chosen: {{chosen}}
  111. </p>
  112. <form @input="changed = true">
  113. <select id="selectEl" v-model="chosen">
  114. <option v-for="choice of choices" :key="choice" :value="choice">{{ choice }}</option>
  115. </select>
  116. </form>
  117. `,
  118. data() {
  119. return {
  120. choices: ['A', 'B'],
  121. chosen: 'A',
  122. changed: false,
  123. }
  124. },
  125. }).mount('#app')
  126. })
  127. expect(await value('#selectEl')).toBe('A')
  128. expect(await html('#app')).toMatchInlineSnapshot(
  129. `"<p> Changed: false</p><p> Chosen: A</p><form><select id="selectEl"><option value="A">A</option><option value="B">B</option></select></form>"`,
  130. )
  131. await page().select('#selectEl', 'B')
  132. await nextTick()
  133. expect(await value('#selectEl')).toBe('B')
  134. expect(await html('#app')).toMatchInlineSnapshot(
  135. `"<p> Changed: true</p><p> Chosen: B</p><form><select id="selectEl"><option value="A">A</option><option value="B">B</option></select></form>"`,
  136. )
  137. })