2
0

vModel.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import {
  2. createApp,
  3. h,
  4. nextTick,
  5. createComponent,
  6. vModelDynamic,
  7. applyDirectives,
  8. VNode
  9. } from '@vue/runtime-dom'
  10. const triggerEvent = (type: string, el: Element) => {
  11. const event = new Event(type)
  12. el.dispatchEvent(event)
  13. }
  14. const withVModel = (node: VNode, arg: any, mods?: any) =>
  15. applyDirectives(node, [[vModelDynamic, arg, '', mods]])
  16. const setValue = function(this: any, value: any) {
  17. this.value = value
  18. }
  19. let app: any, root: any
  20. beforeEach(() => {
  21. app = createApp()
  22. root = document.createElement('div') as any
  23. })
  24. describe('vModel', () => {
  25. it('should work with text input', async () => {
  26. const component = createComponent({
  27. data() {
  28. return { value: null }
  29. },
  30. render() {
  31. return [
  32. withVModel(
  33. h('input', {
  34. 'onUpdate:modelValue': setValue.bind(this)
  35. }),
  36. this.value
  37. )
  38. ]
  39. }
  40. })
  41. app.mount(component, root)
  42. const input = root.querySelector('input')
  43. const data = root._vnode.component.data
  44. input.value = 'foo'
  45. triggerEvent('input', input)
  46. await nextTick()
  47. expect(data.value).toEqual('foo')
  48. data.value = 'bar'
  49. await nextTick()
  50. expect(input.value).toEqual('bar')
  51. })
  52. it('should work with textarea', async () => {
  53. const component = createComponent({
  54. data() {
  55. return { value: null }
  56. },
  57. render() {
  58. return [
  59. withVModel(
  60. h('textarea', {
  61. 'onUpdate:modelValue': setValue.bind(this)
  62. }),
  63. this.value
  64. )
  65. ]
  66. }
  67. })
  68. app.mount(component, root)
  69. const input = root.querySelector('textarea')
  70. const data = root._vnode.component.data
  71. input.value = 'foo'
  72. triggerEvent('input', input)
  73. await nextTick()
  74. expect(data.value).toEqual('foo')
  75. data.value = 'bar'
  76. await nextTick()
  77. expect(input.value).toEqual('bar')
  78. })
  79. it('should support modifiers', async () => {
  80. const component = createComponent({
  81. data() {
  82. return { number: null, trim: null, lazy: null }
  83. },
  84. render() {
  85. return [
  86. withVModel(
  87. h('input', {
  88. class: 'number',
  89. 'onUpdate:modelValue': (val: any) => {
  90. this.number = val
  91. }
  92. }),
  93. this.number,
  94. {
  95. number: true
  96. }
  97. ),
  98. withVModel(
  99. h('input', {
  100. class: 'trim',
  101. 'onUpdate:modelValue': (val: any) => {
  102. this.trim = val
  103. }
  104. }),
  105. this.trim,
  106. {
  107. trim: true
  108. }
  109. ),
  110. withVModel(
  111. h('input', {
  112. class: 'lazy',
  113. 'onUpdate:modelValue': (val: any) => {
  114. this.lazy = val
  115. }
  116. }),
  117. this.lazy,
  118. {
  119. lazy: true
  120. }
  121. )
  122. ]
  123. }
  124. })
  125. app.mount(component, root)
  126. const number = root.querySelector('.number')
  127. const trim = root.querySelector('.trim')
  128. const lazy = root.querySelector('.lazy')
  129. const data = root._vnode.component.data
  130. number.value = '+01.2'
  131. triggerEvent('input', number)
  132. await nextTick()
  133. expect(data.number).toEqual(1.2)
  134. trim.value = ' hello, world '
  135. triggerEvent('input', trim)
  136. await nextTick()
  137. expect(data.trim).toEqual('hello, world')
  138. lazy.value = 'foo'
  139. triggerEvent('change', lazy)
  140. await nextTick()
  141. expect(data.lazy).toEqual('foo')
  142. })
  143. it('should work with checkbox', async () => {
  144. const component = createComponent({
  145. data() {
  146. return { value: null }
  147. },
  148. render() {
  149. return [
  150. withVModel(
  151. h('input', {
  152. type: 'checkbox',
  153. 'onUpdate:modelValue': setValue.bind(this)
  154. }),
  155. this.value
  156. )
  157. ]
  158. }
  159. })
  160. app.mount(component, root)
  161. const input = root.querySelector('input')
  162. const data = root._vnode.component.data
  163. input.checked = true
  164. triggerEvent('change', input)
  165. await nextTick()
  166. expect(data.value).toEqual(true)
  167. data.value = false
  168. await nextTick()
  169. expect(input.checked).toEqual(false)
  170. })
  171. it(`should support array as a checkbox model`, async () => {
  172. const component = createComponent({
  173. data() {
  174. return { value: [] }
  175. },
  176. render() {
  177. return [
  178. withVModel(
  179. h('input', {
  180. type: 'checkbox',
  181. class: 'foo',
  182. value: 'foo',
  183. 'onUpdate:modelValue': setValue.bind(this)
  184. }),
  185. this.value
  186. ),
  187. withVModel(
  188. h('input', {
  189. type: 'checkbox',
  190. class: 'bar',
  191. value: 'bar',
  192. 'onUpdate:modelValue': setValue.bind(this)
  193. }),
  194. this.value
  195. )
  196. ]
  197. }
  198. })
  199. app.mount(component, root)
  200. const foo = root.querySelector('.foo')
  201. const bar = root.querySelector('.bar')
  202. const data = root._vnode.component.data
  203. foo.checked = true
  204. triggerEvent('change', foo)
  205. await nextTick()
  206. expect(data.value).toMatchObject(['foo'])
  207. bar.checked = true
  208. triggerEvent('change', bar)
  209. await nextTick()
  210. expect(data.value).toMatchObject(['foo', 'bar'])
  211. bar.checked = false
  212. triggerEvent('change', bar)
  213. await nextTick()
  214. expect(data.value).toMatchObject(['foo'])
  215. foo.checked = false
  216. triggerEvent('change', foo)
  217. await nextTick()
  218. expect(data.value).toMatchObject([])
  219. data.value = ['foo']
  220. await nextTick()
  221. expect(bar.checked).toEqual(false)
  222. expect(foo.checked).toEqual(true)
  223. data.value = ['bar']
  224. await nextTick()
  225. expect(foo.checked).toEqual(false)
  226. expect(bar.checked).toEqual(true)
  227. data.value = []
  228. await nextTick()
  229. expect(foo.checked).toEqual(false)
  230. expect(bar.checked).toEqual(false)
  231. })
  232. it('should work with radio', async () => {
  233. const component = createComponent({
  234. data() {
  235. return { value: null }
  236. },
  237. render() {
  238. return [
  239. withVModel(
  240. h('input', {
  241. type: 'radio',
  242. class: 'foo',
  243. value: 'foo',
  244. 'onUpdate:modelValue': setValue.bind(this)
  245. }),
  246. this.value
  247. ),
  248. withVModel(
  249. h('input', {
  250. type: 'radio',
  251. class: 'bar',
  252. value: 'bar',
  253. 'onUpdate:modelValue': setValue.bind(this)
  254. }),
  255. this.value
  256. )
  257. ]
  258. }
  259. })
  260. app.mount(component, root)
  261. const foo = root.querySelector('.foo')
  262. const bar = root.querySelector('.bar')
  263. const data = root._vnode.component.data
  264. foo.checked = true
  265. triggerEvent('change', foo)
  266. await nextTick()
  267. expect(data.value).toEqual('foo')
  268. bar.checked = true
  269. triggerEvent('change', bar)
  270. await nextTick()
  271. expect(data.value).toEqual('bar')
  272. data.value = null
  273. await nextTick()
  274. expect(foo.checked).toEqual(false)
  275. expect(bar.checked).toEqual(false)
  276. data.value = 'foo'
  277. await nextTick()
  278. expect(foo.checked).toEqual(true)
  279. expect(bar.checked).toEqual(false)
  280. data.value = 'bar'
  281. await nextTick()
  282. expect(foo.checked).toEqual(false)
  283. expect(bar.checked).toEqual(true)
  284. })
  285. it('should work with single select', async () => {
  286. const component = createComponent({
  287. data() {
  288. return { value: null }
  289. },
  290. render() {
  291. return [
  292. withVModel(
  293. h(
  294. 'select',
  295. {
  296. value: null,
  297. 'onUpdate:modelValue': setValue.bind(this)
  298. },
  299. [h('option', { value: 'foo' }), h('option', { value: 'bar' })]
  300. ),
  301. this.value
  302. )
  303. ]
  304. }
  305. })
  306. app.mount(component, root)
  307. const input = root.querySelector('select')
  308. const foo = root.querySelector('option[value=foo]')
  309. const bar = root.querySelector('option[value=bar]')
  310. const data = root._vnode.component.data
  311. foo.selected = true
  312. triggerEvent('change', input)
  313. await nextTick()
  314. expect(data.value).toEqual('foo')
  315. foo.selected = false
  316. bar.selected = true
  317. triggerEvent('change', input)
  318. await nextTick()
  319. expect(data.value).toEqual('bar')
  320. foo.selected = false
  321. bar.selected = false
  322. data.value = 'foo'
  323. await nextTick()
  324. expect(input.value).toEqual('foo')
  325. expect(foo.selected).toEqual(true)
  326. expect(bar.selected).toEqual(false)
  327. foo.selected = true
  328. bar.selected = false
  329. data.value = 'bar'
  330. await nextTick()
  331. expect(input.value).toEqual('bar')
  332. expect(foo.selected).toEqual(false)
  333. expect(bar.selected).toEqual(true)
  334. })
  335. it('should work with multiple select', async () => {
  336. const component = createComponent({
  337. data() {
  338. return { value: [] }
  339. },
  340. render() {
  341. return [
  342. withVModel(
  343. h(
  344. 'select',
  345. {
  346. value: null,
  347. multiple: true,
  348. 'onUpdate:modelValue': setValue.bind(this)
  349. },
  350. [h('option', { value: 'foo' }), h('option', { value: 'bar' })]
  351. ),
  352. this.value
  353. )
  354. ]
  355. }
  356. })
  357. app.mount(component, root)
  358. const input = root.querySelector('select')
  359. const foo = root.querySelector('option[value=foo]')
  360. const bar = root.querySelector('option[value=bar]')
  361. const data = root._vnode.component.data
  362. foo.selected = true
  363. triggerEvent('change', input)
  364. await nextTick()
  365. expect(data.value).toMatchObject(['foo'])
  366. foo.selected = false
  367. bar.selected = true
  368. triggerEvent('change', input)
  369. await nextTick()
  370. expect(data.value).toMatchObject(['bar'])
  371. foo.selected = true
  372. bar.selected = true
  373. triggerEvent('change', input)
  374. await nextTick()
  375. expect(data.value).toMatchObject(['foo', 'bar'])
  376. foo.selected = false
  377. bar.selected = false
  378. data.value = ['foo']
  379. await nextTick()
  380. expect(input.value).toEqual('foo')
  381. expect(foo.selected).toEqual(true)
  382. expect(bar.selected).toEqual(false)
  383. foo.selected = false
  384. bar.selected = false
  385. data.value = ['foo', 'bar']
  386. await nextTick()
  387. expect(foo.selected).toEqual(true)
  388. expect(bar.selected).toEqual(true)
  389. })
  390. })