bind.spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. import Vue from 'vue'
  2. describe('Directive v-bind', () => {
  3. it('normal attr', done => {
  4. const vm = new Vue({
  5. template: '<div><span :test="foo">hello</span></div>',
  6. data: { foo: 'ok' }
  7. }).$mount()
  8. expect(vm.$el.firstChild.getAttribute('test')).toBe('ok')
  9. vm.foo = 'again'
  10. waitForUpdate(() => {
  11. expect(vm.$el.firstChild.getAttribute('test')).toBe('again')
  12. vm.foo = null
  13. }).then(() => {
  14. expect(vm.$el.firstChild.hasAttribute('test')).toBe(false)
  15. vm.foo = false
  16. }).then(() => {
  17. expect(vm.$el.firstChild.hasAttribute('test')).toBe(false)
  18. vm.foo = true
  19. }).then(() => {
  20. expect(vm.$el.firstChild.getAttribute('test')).toBe('true')
  21. vm.foo = 0
  22. }).then(() => {
  23. expect(vm.$el.firstChild.getAttribute('test')).toBe('0')
  24. }).then(done)
  25. })
  26. it('should set property for input value', done => {
  27. const vm = new Vue({
  28. template: `
  29. <div>
  30. <input type="text" :value="foo">
  31. <input type="checkbox" :checked="bar">
  32. </div>
  33. `,
  34. data: {
  35. foo: 'ok',
  36. bar: false
  37. }
  38. }).$mount()
  39. expect(vm.$el.firstChild.value).toBe('ok')
  40. expect(vm.$el.lastChild.checked).toBe(false)
  41. vm.bar = true
  42. waitForUpdate(() => {
  43. expect(vm.$el.lastChild.checked).toBe(true)
  44. }).then(done)
  45. })
  46. it('xlink', done => {
  47. const vm = new Vue({
  48. template: '<svg><a :xlink:special="foo"></a></svg>',
  49. data: {
  50. foo: 'ok'
  51. }
  52. }).$mount()
  53. const xlinkNS = 'http://www.w3.org/1999/xlink'
  54. expect(vm.$el.firstChild.getAttributeNS(xlinkNS, 'special')).toBe('ok')
  55. vm.foo = 'again'
  56. waitForUpdate(() => {
  57. expect(vm.$el.firstChild.getAttributeNS(xlinkNS, 'special')).toBe('again')
  58. vm.foo = null
  59. }).then(() => {
  60. expect(vm.$el.firstChild.hasAttributeNS(xlinkNS, 'special')).toBe(false)
  61. vm.foo = true
  62. }).then(() => {
  63. expect(vm.$el.firstChild.getAttributeNS(xlinkNS, 'special')).toBe('true')
  64. }).then(done)
  65. })
  66. it('enumerated attr', done => {
  67. const vm = new Vue({
  68. template: '<div><span :contenteditable="foo">hello</span></div>',
  69. data: { foo: true }
  70. }).$mount()
  71. expect(vm.$el.firstChild.getAttribute('contenteditable')).toBe('true')
  72. vm.foo = 'plaintext-only' // allow special values
  73. waitForUpdate(() => {
  74. expect(vm.$el.firstChild.getAttribute('contenteditable')).toBe('plaintext-only')
  75. vm.foo = null
  76. }).then(() => {
  77. expect(vm.$el.firstChild.getAttribute('contenteditable')).toBe('false')
  78. vm.foo = ''
  79. }).then(() => {
  80. expect(vm.$el.firstChild.getAttribute('contenteditable')).toBe('true')
  81. vm.foo = false
  82. }).then(() => {
  83. expect(vm.$el.firstChild.getAttribute('contenteditable')).toBe('false')
  84. vm.foo = 'false'
  85. }).then(() => {
  86. expect(vm.$el.firstChild.getAttribute('contenteditable')).toBe('false')
  87. }).then(done)
  88. })
  89. it('boolean attr', done => {
  90. const vm = new Vue({
  91. template: '<div><span :disabled="foo">hello</span></div>',
  92. data: { foo: true }
  93. }).$mount()
  94. expect(vm.$el.firstChild.getAttribute('disabled')).toBe('disabled')
  95. vm.foo = 'again'
  96. waitForUpdate(() => {
  97. expect(vm.$el.firstChild.getAttribute('disabled')).toBe('disabled')
  98. vm.foo = null
  99. }).then(() => {
  100. expect(vm.$el.firstChild.hasAttribute('disabled')).toBe(false)
  101. vm.foo = ''
  102. }).then(() => {
  103. expect(vm.$el.firstChild.hasAttribute('disabled')).toBe(true)
  104. }).then(done)
  105. })
  106. it('.prop modifier', () => {
  107. const vm = new Vue({
  108. template: '<div><span v-bind:text-content.prop="foo"></span><span :inner-html.prop="bar"></span></div>',
  109. data: {
  110. foo: 'hello',
  111. bar: '<span>qux</span>'
  112. }
  113. }).$mount()
  114. expect(vm.$el.children[0].textContent).toBe('hello')
  115. expect(vm.$el.children[1].innerHTML).toBe('<span>qux</span>')
  116. })
  117. it('.prop modifier with normal attribute binding', () => {
  118. const vm = new Vue({
  119. template: '<input :some.prop="some" :id="id">',
  120. data: {
  121. some: 'hello',
  122. id: false
  123. }
  124. }).$mount()
  125. expect(vm.$el.some).toBe('hello')
  126. expect(vm.$el.getAttribute('id')).toBe(null)
  127. })
  128. if (process.env.VBIND_PROP_SHORTHAND) {
  129. it('.prop modifier shorthand', () => {
  130. const vm = new Vue({
  131. template: '<div><span .text-content="foo"></span><span .inner-html="bar"></span></div>',
  132. data: {
  133. foo: 'hello',
  134. bar: '<span>qux</span>'
  135. }
  136. }).$mount()
  137. expect(vm.$el.children[0].textContent).toBe('hello')
  138. expect(vm.$el.children[1].innerHTML).toBe('<span>qux</span>')
  139. })
  140. }
  141. it('.camel modifier', () => {
  142. const vm = new Vue({
  143. template: '<svg :view-box.camel="viewBox"></svg>',
  144. data: {
  145. viewBox: '0 0 1 1'
  146. }
  147. }).$mount()
  148. expect(vm.$el.getAttribute('viewBox')).toBe('0 0 1 1')
  149. })
  150. it('.sync modifier', done => {
  151. const vm = new Vue({
  152. template: `<test :foo-bar.sync="bar"/>`,
  153. data: {
  154. bar: 1
  155. },
  156. components: {
  157. test: {
  158. props: ['fooBar'],
  159. template: `<div @click="$emit('update:fooBar', 2)">{{ fooBar }}</div>`
  160. }
  161. }
  162. }).$mount()
  163. document.body.appendChild(vm.$el)
  164. expect(vm.$el.textContent).toBe('1')
  165. triggerEvent(vm.$el, 'click')
  166. waitForUpdate(() => {
  167. expect(vm.$el.textContent).toBe('2')
  168. document.body.removeChild(vm.$el)
  169. }).then(done)
  170. })
  171. it('.sync modifier with kebab case event', done => {
  172. const vm = new Vue({
  173. template: `<test ref="test" :foo-bar.sync="bar"/>`,
  174. data: {
  175. bar: 1
  176. },
  177. components: {
  178. test: {
  179. props: ['fooBar'],
  180. template: `<div>{{ fooBar }}</div>`,
  181. methods: {
  182. update () {
  183. this.$emit('update:foo-bar', 2)
  184. }
  185. }
  186. }
  187. }
  188. }).$mount()
  189. expect(vm.$el.textContent).toBe('1')
  190. vm.$refs.test.update()
  191. waitForUpdate(() => {
  192. expect(vm.$el.textContent).toBe('2')
  193. }).then(done)
  194. })
  195. it('bind object', done => {
  196. const vm = new Vue({
  197. template: '<input v-bind="test">',
  198. data: {
  199. test: {
  200. id: 'test',
  201. class: 'ok',
  202. value: 'hello'
  203. }
  204. }
  205. }).$mount()
  206. expect(vm.$el.getAttribute('id')).toBe('test')
  207. expect(vm.$el.getAttribute('class')).toBe('ok')
  208. expect(vm.$el.value).toBe('hello')
  209. vm.test.id = 'hi'
  210. vm.test.value = 'bye'
  211. waitForUpdate(() => {
  212. expect(vm.$el.getAttribute('id')).toBe('hi')
  213. expect(vm.$el.getAttribute('class')).toBe('ok')
  214. expect(vm.$el.value).toBe('bye')
  215. }).then(done)
  216. })
  217. it('bind object with explicit overrides', () => {
  218. const vm = new Vue({
  219. template: `<test v-bind="test" data-foo="foo" dataBar="bar"/>`,
  220. components: {
  221. test: {
  222. template: '<div :data-foo="dataFoo" :data-bar="dataBar"></div>',
  223. props: ['dataFoo', 'dataBar']
  224. }
  225. },
  226. data: {
  227. test: {
  228. dataFoo: 'hi',
  229. dataBar: 'bye'
  230. }
  231. }
  232. }).$mount()
  233. expect(vm.$el.getAttribute('data-foo')).toBe('foo')
  234. expect(vm.$el.getAttribute('data-bar')).toBe('bar')
  235. })
  236. it('.sync modifier with bind object', done => {
  237. const vm = new Vue({
  238. template: `<test v-bind.sync="test"/>`,
  239. data: {
  240. test: {
  241. fooBar: 1
  242. }
  243. },
  244. components: {
  245. test: {
  246. props: ['fooBar'],
  247. template: `<div @click="handleUpdate">{{ fooBar }}</div>`,
  248. methods: {
  249. handleUpdate () {
  250. this.$emit('update:fooBar', 2)
  251. }
  252. }
  253. }
  254. }
  255. }).$mount()
  256. document.body.appendChild(vm.$el)
  257. expect(vm.$el.textContent).toBe('1')
  258. triggerEvent(vm.$el, 'click')
  259. waitForUpdate(() => {
  260. expect(vm.$el.textContent).toBe('2')
  261. vm.test.fooBar = 3
  262. }).then(() => {
  263. expect(vm.$el.textContent).toBe('3')
  264. document.body.removeChild(vm.$el)
  265. }).then(done)
  266. })
  267. it('bind object with overwrite', done => {
  268. const vm = new Vue({
  269. template: '<input v-bind="test" id="foo" :class="test.value">',
  270. data: {
  271. test: {
  272. id: 'test',
  273. class: 'ok',
  274. value: 'hello'
  275. }
  276. }
  277. }).$mount()
  278. expect(vm.$el.getAttribute('id')).toBe('foo')
  279. expect(vm.$el.getAttribute('class')).toBe('hello')
  280. expect(vm.$el.value).toBe('hello')
  281. vm.test.id = 'hi'
  282. vm.test.value = 'bye'
  283. waitForUpdate(() => {
  284. expect(vm.$el.getAttribute('id')).toBe('foo')
  285. expect(vm.$el.getAttribute('class')).toBe('bye')
  286. expect(vm.$el.value).toBe('bye')
  287. }).then(done)
  288. })
  289. it('bind object with class/style', done => {
  290. const vm = new Vue({
  291. template: '<input class="a" style="color:red" v-bind="test">',
  292. data: {
  293. test: {
  294. id: 'test',
  295. class: ['b', 'c'],
  296. style: { fontSize: '12px' }
  297. }
  298. }
  299. }).$mount()
  300. expect(vm.$el.id).toBe('test')
  301. expect(vm.$el.className).toBe('a b c')
  302. expect(vm.$el.style.color).toBe('red')
  303. expect(vm.$el.style.fontSize).toBe('12px')
  304. vm.test.id = 'hi'
  305. vm.test.class = ['d']
  306. vm.test.style = { fontSize: '14px' }
  307. waitForUpdate(() => {
  308. expect(vm.$el.id).toBe('hi')
  309. expect(vm.$el.className).toBe('a d')
  310. expect(vm.$el.style.color).toBe('red')
  311. expect(vm.$el.style.fontSize).toBe('14px')
  312. }).then(done)
  313. })
  314. it('bind object as prop', done => {
  315. const vm = new Vue({
  316. template: '<input v-bind.prop="test">',
  317. data: {
  318. test: {
  319. id: 'test',
  320. className: 'ok',
  321. value: 'hello'
  322. }
  323. }
  324. }).$mount()
  325. expect(vm.$el.id).toBe('test')
  326. expect(vm.$el.className).toBe('ok')
  327. expect(vm.$el.value).toBe('hello')
  328. vm.test.id = 'hi'
  329. vm.test.className = 'okay'
  330. vm.test.value = 'bye'
  331. waitForUpdate(() => {
  332. expect(vm.$el.id).toBe('hi')
  333. expect(vm.$el.className).toBe('okay')
  334. expect(vm.$el.value).toBe('bye')
  335. }).then(done)
  336. })
  337. it('bind array', done => {
  338. const vm = new Vue({
  339. template: '<input v-bind="test">',
  340. data: {
  341. test: [
  342. { id: 'test', class: 'ok' },
  343. { value: 'hello' }
  344. ]
  345. }
  346. }).$mount()
  347. expect(vm.$el.getAttribute('id')).toBe('test')
  348. expect(vm.$el.getAttribute('class')).toBe('ok')
  349. expect(vm.$el.value).toBe('hello')
  350. vm.test[0].id = 'hi'
  351. vm.test[1].value = 'bye'
  352. waitForUpdate(() => {
  353. expect(vm.$el.getAttribute('id')).toBe('hi')
  354. expect(vm.$el.getAttribute('class')).toBe('ok')
  355. expect(vm.$el.value).toBe('bye')
  356. }).then(done)
  357. })
  358. it('warn expect object', () => {
  359. new Vue({
  360. template: '<input v-bind="test">',
  361. data: {
  362. test: 1
  363. }
  364. }).$mount()
  365. expect('v-bind without argument expects an Object or Array value').toHaveBeenWarned()
  366. })
  367. it('set value for option element', () => {
  368. const vm = new Vue({
  369. template: '<select><option :value="val">val</option></select>',
  370. data: {
  371. val: 'val'
  372. }
  373. }).$mount()
  374. // check value attribute
  375. expect(vm.$el.options[0].getAttribute('value')).toBe('val')
  376. })
  377. // a vdom patch edge case where the user has several un-keyed elements of the
  378. // same tag next to each other, and toggling them.
  379. it('properly update for toggling un-keyed children', done => {
  380. const vm = new Vue({
  381. template: `
  382. <div>
  383. <div v-if="ok" id="a" data-test="1"></div>
  384. <div v-if="!ok" id="b"></div>
  385. </div>
  386. `,
  387. data: {
  388. ok: true
  389. }
  390. }).$mount()
  391. expect(vm.$el.children[0].id).toBe('a')
  392. expect(vm.$el.children[0].getAttribute('data-test')).toBe('1')
  393. vm.ok = false
  394. waitForUpdate(() => {
  395. expect(vm.$el.children[0].id).toBe('b')
  396. expect(vm.$el.children[0].getAttribute('data-test')).toBe(null)
  397. }).then(done)
  398. })
  399. describe('bind object with special attribute', () => {
  400. function makeInstance (options) {
  401. return new Vue({
  402. template: `<div>${options.parentTemp}</div>`,
  403. data: {
  404. attrs: {
  405. [options.attr]: options.value
  406. }
  407. },
  408. components: {
  409. comp: {
  410. template: options.childTemp
  411. }
  412. }
  413. }).$mount()
  414. }
  415. it('key', () => {
  416. const vm = makeInstance({
  417. attr: 'key',
  418. value: 'test',
  419. parentTemp: '<div v-bind="attrs"></div>'
  420. })
  421. expect(vm._vnode.children[0].key).toBe('test')
  422. })
  423. it('ref', () => {
  424. const vm = makeInstance({
  425. attr: 'ref',
  426. value: 'test',
  427. parentTemp: '<div v-bind="attrs"></div>'
  428. })
  429. expect(vm.$refs.test).toBe(vm.$el.firstChild)
  430. })
  431. it('slot', () => {
  432. const vm = makeInstance({
  433. attr: 'slot',
  434. value: 'test',
  435. parentTemp: '<comp><span v-bind="attrs">123</span></comp>',
  436. childTemp: '<div>slot:<slot name="test"></slot></div>'
  437. })
  438. expect(vm.$el.innerHTML).toBe('<div>slot:<span>123</span></div>')
  439. })
  440. it('is', () => {
  441. const vm = makeInstance({
  442. attr: 'is',
  443. value: 'comp',
  444. parentTemp: '<component v-bind="attrs"></component>',
  445. childTemp: '<div>comp</div>'
  446. })
  447. expect(vm.$el.innerHTML).toBe('<div>comp</div>')
  448. })
  449. })
  450. describe('dynamic arguments', () => {
  451. it('basic', done => {
  452. const vm = new Vue({
  453. template: `<div v-bind:[key]="value"></div>`,
  454. data: {
  455. key: 'id',
  456. value: 'hello'
  457. }
  458. }).$mount()
  459. expect(vm.$el.id).toBe('hello')
  460. vm.key = 'class'
  461. waitForUpdate(() => {
  462. expect(vm.$el.id).toBe('')
  463. expect(vm.$el.className).toBe('hello')
  464. // explicit null value
  465. vm.key = null
  466. }).then(() => {
  467. expect(vm.$el.className).toBe('')
  468. expect(vm.$el.id).toBe('')
  469. vm.key = undefined
  470. }).then(() => {
  471. expect(`Invalid value for dynamic directive argument`).toHaveBeenWarned()
  472. }).then(done)
  473. })
  474. it('shorthand', done => {
  475. const vm = new Vue({
  476. template: `<div :[key]="value"></div>`,
  477. data: {
  478. key: 'id',
  479. value: 'hello'
  480. }
  481. }).$mount()
  482. expect(vm.$el.id).toBe('hello')
  483. vm.key = 'class'
  484. waitForUpdate(() => {
  485. expect(vm.$el.className).toBe('hello')
  486. }).then(done)
  487. })
  488. it('with .prop modifier', done => {
  489. const vm = new Vue({
  490. template: `<div :[key].prop="value"></div>`,
  491. data: {
  492. key: 'id',
  493. value: 'hello'
  494. }
  495. }).$mount()
  496. expect(vm.$el.id).toBe('hello')
  497. vm.key = 'textContent'
  498. waitForUpdate(() => {
  499. expect(vm.$el.textContent).toBe('hello')
  500. }).then(done)
  501. })
  502. if (process.env.VBIND_PROP_SHORTHAND) {
  503. it('.prop shorthand', done => {
  504. const vm = new Vue({
  505. template: `<div .[key]="value"></div>`,
  506. data: {
  507. key: 'id',
  508. value: 'hello'
  509. }
  510. }).$mount()
  511. expect(vm.$el.id).toBe('hello')
  512. vm.key = 'textContent'
  513. waitForUpdate(() => {
  514. expect(vm.$el.textContent).toBe('hello')
  515. }).then(done)
  516. })
  517. }
  518. it('handle class and style', () => {
  519. const vm = new Vue({
  520. template: `<div :[key]="value" :[key2]="value2"></div>`,
  521. data: {
  522. key: 'class',
  523. value: ['hello', 'world'],
  524. key2: 'style',
  525. value2: {
  526. color: 'red'
  527. }
  528. }
  529. }).$mount()
  530. expect(vm.$el.className).toBe('hello world')
  531. expect(vm.$el.style.color).toBe('red')
  532. })
  533. it('handle shouldUseProp', done => {
  534. const vm = new Vue({
  535. template: `<input :[key]="value">`,
  536. data: {
  537. key: 'value',
  538. value: 'foo'
  539. }
  540. }).$mount()
  541. expect(vm.$el.value).toBe('foo')
  542. vm.value = 'bar'
  543. waitForUpdate(() => {
  544. expect(vm.$el.value).toBe('bar')
  545. }).then(done)
  546. })
  547. it('with .sync modifier', done => {
  548. const vm = new Vue({
  549. template: `<foo ref="child" :[key].sync="value"/>`,
  550. data: {
  551. key: 'foo',
  552. value: 'bar'
  553. },
  554. components: {
  555. foo: {
  556. props: ['foo'],
  557. template: `<div>{{ foo }}</div>`
  558. }
  559. }
  560. }).$mount()
  561. expect(vm.$el.textContent).toBe('bar')
  562. vm.$refs.child.$emit('update:foo', 'baz')
  563. waitForUpdate(() => {
  564. expect(vm.value).toBe('baz')
  565. expect(vm.$el.textContent).toBe('baz')
  566. }).then(done)
  567. })
  568. })
  569. })