2
0

model_spec.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. var _ = require('../../../../src/util')
  2. var Vue = require('../../../../src/vue')
  3. // unset jQuery to bypass jQuery check for normal test cases
  4. jQuery = null
  5. /**
  6. * Mock event helper
  7. */
  8. function trigger (target, event, process) {
  9. var e = document.createEvent('HTMLEvents')
  10. e.initEvent(event, true, true)
  11. if (process) process(e)
  12. target.dispatchEvent(e)
  13. }
  14. /**
  15. * setting <select>'s value in IE9 doesn't work
  16. * we have to manually loop through the options
  17. */
  18. function updateSelect (el, value) {
  19. var options = el.options
  20. var i = options.length
  21. while (i--) {
  22. /* eslint-disable eqeqeq */
  23. if (options[i].value == value) {
  24. /* eslint-enable eqeqeq */
  25. options[i].selected = true
  26. break
  27. }
  28. }
  29. }
  30. if (_.inBrowser) {
  31. describe('v-model', function () {
  32. var el
  33. beforeEach(function () {
  34. el = document.createElement('div')
  35. el.style.display = 'none'
  36. document.body.appendChild(el)
  37. spyOn(_, 'warn')
  38. })
  39. it('radio buttons', function (done) {
  40. var vm = new Vue({
  41. el: el,
  42. data: {
  43. test: 'a'
  44. },
  45. template:
  46. '<input type="radio" value="a" v-model="test" name="test">' +
  47. '<input type="radio" value="b" v-model="test" name="test">'
  48. })
  49. expect(el.childNodes[0].checked).toBe(true)
  50. expect(el.childNodes[1].checked).toBe(false)
  51. vm.test = 'b'
  52. _.nextTick(function () {
  53. expect(el.childNodes[0].checked).toBe(false)
  54. expect(el.childNodes[1].checked).toBe(true)
  55. el.childNodes[0].click()
  56. expect(el.childNodes[0].checked).toBe(true)
  57. expect(el.childNodes[1].checked).toBe(false)
  58. expect(vm.test).toBe('a')
  59. vm._directives[1].unbind()
  60. el.childNodes[1].click()
  61. expect(vm.test).toBe('a')
  62. done()
  63. })
  64. })
  65. it('radio default value', function () {
  66. var vm = new Vue({
  67. el: el,
  68. data: {},
  69. template: '<input type="radio" checked value="a" v-model="test">'
  70. })
  71. expect(vm.test).toBe('a')
  72. })
  73. it('checkbox', function (done) {
  74. var vm = new Vue({
  75. el: el,
  76. data: {
  77. test: true
  78. },
  79. template: '<input type="checkbox" v-model="test">'
  80. })
  81. expect(el.firstChild.checked).toBe(true)
  82. vm.test = false
  83. _.nextTick(function () {
  84. expect(el.firstChild.checked).toBe(false)
  85. expect(vm.test).toBe(false)
  86. el.firstChild.click()
  87. expect(el.firstChild.checked).toBe(true)
  88. expect(vm.test).toBe(true)
  89. vm._directives[0].unbind()
  90. el.firstChild.click()
  91. expect(el.firstChild.checked).toBe(false)
  92. expect(vm.test).toBe(true)
  93. done()
  94. })
  95. })
  96. it('checkbox default value', function () {
  97. var vm = new Vue({
  98. el: el,
  99. data: {},
  100. template: '<input type="checkbox" checked v-model="test">'
  101. })
  102. expect(vm.test).toBe(true)
  103. })
  104. it('select', function (done) {
  105. var vm = new Vue({
  106. el: el,
  107. data: {
  108. test: 'b'
  109. },
  110. template:
  111. '<select v-model="test">' +
  112. '<option>a</option>' +
  113. '<option>b</option>' +
  114. '<option>c</option>' +
  115. '</select>'
  116. })
  117. expect(vm.test).toBe('b')
  118. expect(el.firstChild.value).toBe('b')
  119. expect(el.firstChild.childNodes[1].selected).toBe(true)
  120. vm.test = 'c'
  121. _.nextTick(function () {
  122. expect(el.firstChild.value).toBe('c')
  123. expect(el.firstChild.childNodes[2].selected).toBe(true)
  124. updateSelect(el.firstChild, 'a')
  125. trigger(el.firstChild, 'change')
  126. expect(vm.test).toBe('a')
  127. done()
  128. })
  129. })
  130. it('select persist non-selected on append', function () {
  131. var vm = new Vue({
  132. el: el,
  133. data: {
  134. test: null
  135. },
  136. replace: true,
  137. template:
  138. '<select v-model="test">' +
  139. '<option>a</option>' +
  140. '<option>b</option>' +
  141. '<option>c</option>' +
  142. '</select>'
  143. })
  144. expect(vm.$el.value).toBe('')
  145. expect(vm.$el.selectedIndex).toBe(-1)
  146. vm.$remove()
  147. vm.$appendTo(document.body)
  148. expect(vm.$el.value).toBe('')
  149. expect(vm.$el.selectedIndex).toBe(-1)
  150. })
  151. it('select template default value', function () {
  152. var vm = new Vue({
  153. el: el,
  154. data: {
  155. test: 'a'
  156. },
  157. template:
  158. '<select v-model="test">' +
  159. '<option>a</option>' +
  160. '<option selected>b</option>' +
  161. '</select>'
  162. })
  163. expect(vm.test).toBe('b')
  164. expect(el.firstChild.value).toBe('b')
  165. expect(el.firstChild.childNodes[1].selected).toBe(true)
  166. })
  167. it('select + empty default value', function () {
  168. var vm = new Vue({
  169. el: el,
  170. template: '<select v-model="test"><option value="" selected>null</option><<option value="1">1</option></select>'
  171. })
  172. expect(vm.test).toBe('')
  173. trigger(vm.$el.firstChild, 'change')
  174. expect(vm.test).toBe('')
  175. })
  176. it('select + multiple', function (done) {
  177. var vm = new Vue({
  178. el: el,
  179. data: {
  180. test: [2] // test number soft equal
  181. },
  182. template:
  183. '<select v-model="test" multiple>' +
  184. '<option>1</option>' +
  185. '<option>2</option>' +
  186. '<option>3</option>' +
  187. '</select>'
  188. })
  189. var opts = el.firstChild.options
  190. expect(opts[0].selected).toBe(false)
  191. expect(opts[1].selected).toBe(true)
  192. expect(opts[2].selected).toBe(false)
  193. vm.test = [1, '3'] // mix of number/string
  194. _.nextTick(function () {
  195. expect(opts[0].selected).toBe(true)
  196. expect(opts[1].selected).toBe(false)
  197. expect(opts[2].selected).toBe(true)
  198. opts[0].selected = false
  199. opts[1].selected = true
  200. trigger(el.firstChild, 'change')
  201. expect(vm.test[0]).toBe('2')
  202. expect(vm.test[1]).toBe('3')
  203. done()
  204. })
  205. })
  206. it('select + multiple default value', function () {
  207. var vm = new Vue({
  208. el: el,
  209. data: {},
  210. template:
  211. '<select v-model="test" multiple>' +
  212. '<option>a</option>' +
  213. '<option selected>b</option>' +
  214. '<option selected>c</option>' +
  215. '</select>'
  216. })
  217. expect(vm.test[0]).toBe('b')
  218. expect(vm.test[1]).toBe('c')
  219. })
  220. it('select + options', function (done) {
  221. var vm = new Vue({
  222. el: el,
  223. data: {
  224. test: 'b',
  225. opts: ['a', 'b', 'c']
  226. },
  227. template: '<select v-model="test" options="opts"></select>'
  228. })
  229. var opts = el.firstChild.options
  230. expect(opts.length).toBe(3)
  231. expect(opts[0].selected).toBe(false)
  232. expect(opts[1].selected).toBe(true)
  233. expect(opts[2].selected).toBe(false)
  234. vm.opts = ['b', 'c']
  235. _.nextTick(function () {
  236. expect(opts.length).toBe(2)
  237. expect(opts[0].selected).toBe(true)
  238. expect(opts[1].selected).toBe(false)
  239. // should teardown option watcher when unbind
  240. expect(vm._watchers.length).toBe(2)
  241. vm._directives[0]._teardown()
  242. expect(vm._watchers.length).toBe(0)
  243. done()
  244. })
  245. })
  246. it('select + options + text', function () {
  247. new Vue({
  248. el: el,
  249. data: {
  250. test: 'b',
  251. opts: [
  252. { text: 'Select an option', value: null, disabled: true },
  253. { text: 'A', value: 'a' },
  254. { text: 'B', value: 'b' }
  255. ]
  256. },
  257. template: '<select v-model="test" options="opts"></select>'
  258. })
  259. expect(el.firstChild.innerHTML).toBe(
  260. '<option disabled="">Select an option</option>' +
  261. '<option value="a">A</option>' +
  262. '<option value="b">B</option>'
  263. )
  264. var opts = el.firstChild.options
  265. expect(opts[0].disabled).toBe(true)
  266. expect(opts[0].selected).toBe(false)
  267. expect(opts[1].selected).toBe(false)
  268. expect(opts[2].selected).toBe(true)
  269. })
  270. it('select + options + optgroup', function () {
  271. new Vue({
  272. el: el,
  273. data: {
  274. test: 'b',
  275. opts: [
  276. { label: 'A', options: ['a', 'b'] },
  277. { label: 'B', options: ['c'] }
  278. ]
  279. },
  280. template: '<select v-model="test" options="opts"></select>'
  281. })
  282. expect(el.firstChild.innerHTML).toBe(
  283. '<optgroup label="A">' +
  284. '<option value="a">a</option><option value="b">b</option>' +
  285. '</optgroup>' +
  286. '<optgroup label="B">' +
  287. '<option value="c">c</option>' +
  288. '</optgroup>'
  289. )
  290. var opts = el.firstChild.options
  291. expect(opts[0].selected).toBe(false)
  292. expect(opts[1].selected).toBe(true)
  293. expect(opts[2].selected).toBe(false)
  294. })
  295. it('select + number', function () {
  296. var vm = new Vue({
  297. el: el,
  298. data: {
  299. test: '1'
  300. },
  301. template: '<select v-model="test" number><option value="1">1</option></select>'
  302. })
  303. expect(vm.test).toBe('1')
  304. trigger(vm.$el.firstChild, 'change')
  305. expect(vm.test).toBe(1)
  306. })
  307. it('select + number + multiple', function () {
  308. var vm = new Vue({
  309. el: el,
  310. data: {
  311. test: []
  312. },
  313. template: '<select v-model="test" multiple number><option>1</option><option>2</option></select>'
  314. })
  315. ;[].forEach.call(el.querySelectorAll('option'), function (o) {
  316. o.selected = true
  317. })
  318. trigger(el.firstChild, 'change')
  319. expect(vm.test[0]).toBe(1)
  320. expect(vm.test[1]).toBe(2)
  321. })
  322. it('select + number initial value', function () {
  323. var vm = new Vue({
  324. el: el,
  325. data: {
  326. test: '1'
  327. },
  328. template: '<select v-model="test" number><option value="1" selected>1</option></select>'
  329. })
  330. expect(vm.test).toBe(1)
  331. })
  332. it('select + options + filter', function () {
  333. new Vue({
  334. el: el,
  335. data: {
  336. opts: ['a', 'b']
  337. },
  338. filters: {
  339. aFilter: function (opts) {
  340. return opts.map(function (val, i) {
  341. return val + i
  342. })
  343. }
  344. },
  345. template: '<select v-model="test" options="opts | aFilter"></select>'
  346. })
  347. expect(el.firstChild.innerHTML).toBe(
  348. '<option value="a0">a0</option>' +
  349. '<option value="b1">b1</option>'
  350. )
  351. })
  352. it('text', function (done) {
  353. var vm = new Vue({
  354. el: el,
  355. data: {
  356. test: 'b'
  357. },
  358. template: '<input v-model="test">'
  359. })
  360. expect(el.firstChild.value).toBe('b')
  361. vm.test = 'a'
  362. _.nextTick(function () {
  363. expect(el.firstChild.value).toBe('a')
  364. el.firstChild.value = 'c'
  365. trigger(el.firstChild, 'input')
  366. expect(vm.test).toBe('c')
  367. vm._directives[0].unbind()
  368. el.firstChild.value = 'd'
  369. trigger(el.firstChild, 'input')
  370. expect(vm.test).toBe('c')
  371. done()
  372. })
  373. })
  374. it('text default value', function () {
  375. var vm = new Vue({
  376. el: el,
  377. data: {
  378. test: 'b'
  379. },
  380. template: '<input v-model="test | test" value="a">',
  381. filters: {
  382. test: {
  383. read: function (v) {
  384. return v.slice(0, -1)
  385. },
  386. write: function (v) {
  387. return v + 'c'
  388. }
  389. }
  390. }
  391. })
  392. expect(vm.test).toBe('ac')
  393. expect(el.firstChild.value).toBe('a')
  394. })
  395. it('text lazy', function () {
  396. var vm = new Vue({
  397. el: el,
  398. data: {
  399. test: 'b'
  400. },
  401. template: '<input v-model="test" lazy>'
  402. })
  403. expect(el.firstChild.value).toBe('b')
  404. expect(vm.test).toBe('b')
  405. el.firstChild.value = 'c'
  406. trigger(el.firstChild, 'input')
  407. expect(vm.test).toBe('b')
  408. trigger(el.firstChild, 'change')
  409. expect(vm.test).toBe('c')
  410. })
  411. it('text with filters', function (done) {
  412. var vm = new Vue({
  413. el: el,
  414. data: {
  415. test: 'b'
  416. },
  417. filters: {
  418. test: {
  419. write: function (val) {
  420. return val.toLowerCase()
  421. }
  422. }
  423. },
  424. template: '<input v-model="test | uppercase | test">'
  425. })
  426. expect(el.firstChild.value).toBe('B')
  427. el.firstChild.value = 'cc'
  428. trigger(el.firstChild, 'input')
  429. _.nextTick(function () {
  430. expect(el.firstChild.value).toBe('CC')
  431. expect(vm.test).toBe('cc')
  432. done()
  433. })
  434. })
  435. // when there's only write filter, should allow
  436. // out of sync between the input field and actual data
  437. it('text with only write filter', function (done) {
  438. var vm = new Vue({
  439. el: el,
  440. data: {
  441. test: 'b'
  442. },
  443. filters: {
  444. test: {
  445. write: function (val) {
  446. return val.toUpperCase()
  447. }
  448. }
  449. },
  450. template: '<input v-model="test | test">'
  451. })
  452. el.firstChild.value = 'cc'
  453. trigger(el.firstChild, 'input')
  454. _.nextTick(function () {
  455. expect(el.firstChild.value).toBe('cc')
  456. expect(vm.test).toBe('CC')
  457. done()
  458. })
  459. })
  460. it('number', function () {
  461. var vm = new Vue({
  462. el: el,
  463. data: {
  464. test: 1
  465. },
  466. template: '<input v-model="test" value="2" number>'
  467. })
  468. expect(vm.test).toBe(2)
  469. el.firstChild.value = 3
  470. trigger(el.firstChild, 'input')
  471. expect(vm.test).toBe(3)
  472. })
  473. it('IE9 cut and delete', function (done) {
  474. var ie9 = _.isIE9
  475. _.isIE9 = true
  476. var vm = new Vue({
  477. el: el,
  478. data: {
  479. test: 'aaa'
  480. },
  481. template: '<input v-model="test">'
  482. })
  483. var input = el.firstChild
  484. input.value = 'aa'
  485. trigger(input, 'cut')
  486. _.nextTick(function () {
  487. expect(vm.test).toBe('aa')
  488. input.value = 'a'
  489. trigger(input, 'keyup', function (e) {
  490. e.keyCode = 8
  491. })
  492. expect(vm.test).toBe('a')
  493. // teardown
  494. vm._directives[0].unbind()
  495. input.value = 'bbb'
  496. trigger(input, 'keyup', function (e) {
  497. e.keyCode = 8
  498. })
  499. expect(vm.test).toBe('a')
  500. _.isIE9 = ie9
  501. done()
  502. })
  503. })
  504. if (!_.isAndroid) {
  505. it('text + compositionevents', function (done) {
  506. var vm = new Vue({
  507. el: el,
  508. data: {
  509. test: 'aaa',
  510. test2: 'bbb'
  511. },
  512. template: '<input v-model="test"><input v-model="test2 | uppercase">'
  513. })
  514. var input = el.firstChild
  515. var input2 = el.childNodes[1]
  516. trigger(input, 'compositionstart')
  517. trigger(input2, 'compositionstart')
  518. input.value = input2.value = 'ccc'
  519. // input before composition unlock should not call set
  520. trigger(input, 'input')
  521. trigger(input2, 'input')
  522. expect(vm.test).toBe('aaa')
  523. expect(vm.test2).toBe('bbb')
  524. // after composition unlock it should work
  525. trigger(input, 'compositionend')
  526. trigger(input2, 'compositionend')
  527. trigger(input, 'input')
  528. trigger(input2, 'input')
  529. expect(vm.test).toBe('ccc')
  530. expect(vm.test2).toBe('ccc')
  531. // IE complains about "unspecified error" when calling
  532. // setSelectionRange() on an input element that's been
  533. // removed from the DOM, so we wait until the
  534. // selection range callback has fired to end this test.
  535. _.nextTick(done)
  536. })
  537. }
  538. it('textarea', function () {
  539. var vm = new Vue({
  540. el: el,
  541. data: {
  542. test: 'b',
  543. b: 'BB'
  544. },
  545. template: '<textarea v-model="test">a {{b}} c</textarea>'
  546. })
  547. expect(vm.test).toBe('a BB c')
  548. expect(el.firstChild.value).toBe('a BB c')
  549. })
  550. it('warn invalid tag', function () {
  551. new Vue({
  552. el: el,
  553. template: '<div v-model="test"></div>'
  554. })
  555. expect(hasWarned(_, 'does not support element type')).toBe(true)
  556. })
  557. it('warn invalid option value', function () {
  558. new Vue({
  559. el: el,
  560. data: { a: 123 },
  561. template: '<select v-model="test" options="a"></select>'
  562. })
  563. expect(hasWarned(_, 'Invalid options value')).toBe(true)
  564. })
  565. it('warn read-only filters', function () {
  566. new Vue({
  567. el: el,
  568. template: '<input v-model="abc | test">',
  569. filters: {
  570. test: function (v) {
  571. return v
  572. }
  573. }
  574. })
  575. expect(hasWarned(_, 'read-only filter')).toBe(true)
  576. })
  577. it('support jQuery change event', function (done) {
  578. // restore jQuery
  579. jQuery = $
  580. var vm = new Vue({
  581. el: el,
  582. data: {
  583. test: 'b'
  584. },
  585. template: '<input v-model="test" lazy>'
  586. })
  587. expect(el.firstChild.value).toBe('b')
  588. vm.test = 'a'
  589. _.nextTick(function () {
  590. expect(el.firstChild.value).toBe('a')
  591. el.firstChild.value = 'c'
  592. jQuery(el.firstChild).trigger('change')
  593. expect(vm.test).toBe('c')
  594. vm._directives[0].unbind()
  595. el.firstChild.value = 'd'
  596. jQuery(el.firstChild).trigger('change')
  597. expect(vm.test).toBe('c')
  598. // unset jQuery
  599. jQuery = null
  600. done()
  601. })
  602. })
  603. it('support debounce', function (done) {
  604. var spy = jasmine.createSpy()
  605. var vm = new Vue({
  606. el: el,
  607. data: {
  608. test: 'a'
  609. },
  610. watch: {
  611. test: spy
  612. },
  613. template: '<input v-model="test" debounce="100">'
  614. })
  615. el.firstChild.value = 'b'
  616. trigger(el.firstChild, 'input')
  617. setTimeout(function () {
  618. el.firstChild.value = 'c'
  619. trigger(el.firstChild, 'input')
  620. }, 10)
  621. setTimeout(function () {
  622. el.firstChild.value = 'd'
  623. trigger(el.firstChild, 'input')
  624. }, 20)
  625. setTimeout(function () {
  626. expect(spy.calls.count()).toBe(0)
  627. expect(vm.test).toBe('a')
  628. }, 30)
  629. setTimeout(function () {
  630. expect(spy.calls.count()).toBe(1)
  631. expect(vm.test).toBe('d')
  632. done()
  633. }, 200)
  634. })
  635. })
  636. }