bind.spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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>{{ dataFoo }} {{ 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.textContent).toBe('foo bar')
  234. })
  235. it('.sync modifier with bind object', done => {
  236. const vm = new Vue({
  237. template: `<test v-bind.sync="test"/>`,
  238. data: {
  239. test: {
  240. fooBar: 1
  241. }
  242. },
  243. components: {
  244. test: {
  245. props: ['fooBar'],
  246. template: `<div @click="handleUpdate">{{ fooBar }}</div>`,
  247. methods: {
  248. handleUpdate () {
  249. this.$emit('update:fooBar', 2)
  250. }
  251. }
  252. }
  253. }
  254. }).$mount()
  255. document.body.appendChild(vm.$el)
  256. expect(vm.$el.textContent).toBe('1')
  257. triggerEvent(vm.$el, 'click')
  258. waitForUpdate(() => {
  259. expect(vm.$el.textContent).toBe('2')
  260. vm.test.fooBar = 3
  261. }).then(() => {
  262. expect(vm.$el.textContent).toBe('3')
  263. document.body.removeChild(vm.$el)
  264. }).then(done)
  265. })
  266. it('bind object with overwrite', done => {
  267. const vm = new Vue({
  268. template: '<input v-bind="test" id="foo" :class="test.value">',
  269. data: {
  270. test: {
  271. id: 'test',
  272. class: 'ok',
  273. value: 'hello'
  274. }
  275. }
  276. }).$mount()
  277. expect(vm.$el.getAttribute('id')).toBe('foo')
  278. expect(vm.$el.getAttribute('class')).toBe('hello')
  279. expect(vm.$el.value).toBe('hello')
  280. vm.test.id = 'hi'
  281. vm.test.value = 'bye'
  282. waitForUpdate(() => {
  283. expect(vm.$el.getAttribute('id')).toBe('foo')
  284. expect(vm.$el.getAttribute('class')).toBe('bye')
  285. expect(vm.$el.value).toBe('bye')
  286. }).then(done)
  287. })
  288. it('bind object with class/style', done => {
  289. const vm = new Vue({
  290. template: '<input class="a" style="color:red" v-bind="test">',
  291. data: {
  292. test: {
  293. id: 'test',
  294. class: ['b', 'c'],
  295. style: { fontSize: '12px' }
  296. }
  297. }
  298. }).$mount()
  299. expect(vm.$el.id).toBe('test')
  300. expect(vm.$el.className).toBe('a b c')
  301. expect(vm.$el.style.color).toBe('red')
  302. expect(vm.$el.style.fontSize).toBe('12px')
  303. vm.test.id = 'hi'
  304. vm.test.class = ['d']
  305. vm.test.style = { fontSize: '14px' }
  306. waitForUpdate(() => {
  307. expect(vm.$el.id).toBe('hi')
  308. expect(vm.$el.className).toBe('a d')
  309. expect(vm.$el.style.color).toBe('red')
  310. expect(vm.$el.style.fontSize).toBe('14px')
  311. }).then(done)
  312. })
  313. it('bind object as prop', done => {
  314. const vm = new Vue({
  315. template: '<input v-bind.prop="test">',
  316. data: {
  317. test: {
  318. id: 'test',
  319. className: 'ok',
  320. value: 'hello'
  321. }
  322. }
  323. }).$mount()
  324. expect(vm.$el.id).toBe('test')
  325. expect(vm.$el.className).toBe('ok')
  326. expect(vm.$el.value).toBe('hello')
  327. vm.test.id = 'hi'
  328. vm.test.className = 'okay'
  329. vm.test.value = 'bye'
  330. waitForUpdate(() => {
  331. expect(vm.$el.id).toBe('hi')
  332. expect(vm.$el.className).toBe('okay')
  333. expect(vm.$el.value).toBe('bye')
  334. }).then(done)
  335. })
  336. it('bind array', done => {
  337. const vm = new Vue({
  338. template: '<input v-bind="test">',
  339. data: {
  340. test: [
  341. { id: 'test', class: 'ok' },
  342. { value: 'hello' }
  343. ]
  344. }
  345. }).$mount()
  346. expect(vm.$el.getAttribute('id')).toBe('test')
  347. expect(vm.$el.getAttribute('class')).toBe('ok')
  348. expect(vm.$el.value).toBe('hello')
  349. vm.test[0].id = 'hi'
  350. vm.test[1].value = 'bye'
  351. waitForUpdate(() => {
  352. expect(vm.$el.getAttribute('id')).toBe('hi')
  353. expect(vm.$el.getAttribute('class')).toBe('ok')
  354. expect(vm.$el.value).toBe('bye')
  355. }).then(done)
  356. })
  357. it('warn expect object', () => {
  358. new Vue({
  359. template: '<input v-bind="test">',
  360. data: {
  361. test: 1
  362. }
  363. }).$mount()
  364. expect('v-bind without argument expects an Object or Array value').toHaveBeenWarned()
  365. })
  366. it('set value for option element', () => {
  367. const vm = new Vue({
  368. template: '<select><option :value="val">val</option></select>',
  369. data: {
  370. val: 'val'
  371. }
  372. }).$mount()
  373. // check value attribute
  374. expect(vm.$el.options[0].getAttribute('value')).toBe('val')
  375. })
  376. // a vdom patch edge case where the user has several un-keyed elements of the
  377. // same tag next to each other, and toggling them.
  378. it('properly update for toggling un-keyed children', done => {
  379. const vm = new Vue({
  380. template: `
  381. <div>
  382. <div v-if="ok" id="a" data-test="1"></div>
  383. <div v-if="!ok" id="b"></div>
  384. </div>
  385. `,
  386. data: {
  387. ok: true
  388. }
  389. }).$mount()
  390. expect(vm.$el.children[0].id).toBe('a')
  391. expect(vm.$el.children[0].getAttribute('data-test')).toBe('1')
  392. vm.ok = false
  393. waitForUpdate(() => {
  394. expect(vm.$el.children[0].id).toBe('b')
  395. expect(vm.$el.children[0].getAttribute('data-test')).toBe(null)
  396. }).then(done)
  397. })
  398. describe('bind object with special attribute', () => {
  399. function makeInstance (options) {
  400. return new Vue({
  401. template: `<div>${options.parentTemp}</div>`,
  402. data: {
  403. attrs: {
  404. [options.attr]: options.value
  405. }
  406. },
  407. components: {
  408. comp: {
  409. template: options.childTemp
  410. }
  411. }
  412. }).$mount()
  413. }
  414. it('key', () => {
  415. const vm = makeInstance({
  416. attr: 'key',
  417. value: 'test',
  418. parentTemp: '<div v-bind="attrs"></div>'
  419. })
  420. expect(vm._vnode.children[0].key).toBe('test')
  421. })
  422. it('ref', () => {
  423. const vm = makeInstance({
  424. attr: 'ref',
  425. value: 'test',
  426. parentTemp: '<div v-bind="attrs"></div>'
  427. })
  428. expect(vm.$refs.test).toBe(vm.$el.firstChild)
  429. })
  430. it('slot', () => {
  431. const vm = makeInstance({
  432. attr: 'slot',
  433. value: 'test',
  434. parentTemp: '<comp><span v-bind="attrs">123</span></comp>',
  435. childTemp: '<div>slot:<slot name="test"></slot></div>'
  436. })
  437. expect(vm.$el.innerHTML).toBe('<div>slot:<span>123</span></div>')
  438. })
  439. it('is', () => {
  440. const vm = makeInstance({
  441. attr: 'is',
  442. value: 'comp',
  443. parentTemp: '<component v-bind="attrs"></component>',
  444. childTemp: '<div>comp</div>'
  445. })
  446. expect(vm.$el.innerHTML).toBe('<div>comp</div>')
  447. })
  448. })
  449. describe('dynamic arguments', () => {
  450. it('basic', done => {
  451. const vm = new Vue({
  452. template: `<div v-bind:[key]="value"></div>`,
  453. data: {
  454. key: 'id',
  455. value: 'hello'
  456. }
  457. }).$mount()
  458. expect(vm.$el.id).toBe('hello')
  459. vm.key = 'class'
  460. waitForUpdate(() => {
  461. expect(vm.$el.id).toBe('')
  462. expect(vm.$el.className).toBe('hello')
  463. // explicit null value
  464. vm.key = null
  465. }).then(() => {
  466. expect(vm.$el.className).toBe('')
  467. expect(vm.$el.id).toBe('')
  468. vm.key = undefined
  469. }).then(() => {
  470. expect(`Invalid value for dynamic directive argument`).toHaveBeenWarned()
  471. }).then(done)
  472. })
  473. it('shorthand', done => {
  474. const vm = new Vue({
  475. template: `<div :[key]="value"></div>`,
  476. data: {
  477. key: 'id',
  478. value: 'hello'
  479. }
  480. }).$mount()
  481. expect(vm.$el.id).toBe('hello')
  482. vm.key = 'class'
  483. waitForUpdate(() => {
  484. expect(vm.$el.className).toBe('hello')
  485. }).then(done)
  486. })
  487. it('with .prop modifier', done => {
  488. const vm = new Vue({
  489. template: `<div :[key].prop="value"></div>`,
  490. data: {
  491. key: 'id',
  492. value: 'hello'
  493. }
  494. }).$mount()
  495. expect(vm.$el.id).toBe('hello')
  496. vm.key = 'textContent'
  497. waitForUpdate(() => {
  498. expect(vm.$el.textContent).toBe('hello')
  499. }).then(done)
  500. })
  501. if (process.env.VBIND_PROP_SHORTHAND) {
  502. it('.prop shorthand', done => {
  503. const vm = new Vue({
  504. template: `<div .[key]="value"></div>`,
  505. data: {
  506. key: 'id',
  507. value: 'hello'
  508. }
  509. }).$mount()
  510. expect(vm.$el.id).toBe('hello')
  511. vm.key = 'textContent'
  512. waitForUpdate(() => {
  513. expect(vm.$el.textContent).toBe('hello')
  514. }).then(done)
  515. })
  516. }
  517. it('handle class and style', () => {
  518. const vm = new Vue({
  519. template: `<div :[key]="value" :[key2]="value2"></div>`,
  520. data: {
  521. key: 'class',
  522. value: ['hello', 'world'],
  523. key2: 'style',
  524. value2: {
  525. color: 'red'
  526. }
  527. }
  528. }).$mount()
  529. expect(vm.$el.className).toBe('hello world')
  530. expect(vm.$el.style.color).toBe('red')
  531. })
  532. it('handle shouldUseProp', done => {
  533. const vm = new Vue({
  534. template: `<input :[key]="value">`,
  535. data: {
  536. key: 'value',
  537. value: 'foo'
  538. }
  539. }).$mount()
  540. expect(vm.$el.value).toBe('foo')
  541. vm.value = 'bar'
  542. waitForUpdate(() => {
  543. expect(vm.$el.value).toBe('bar')
  544. }).then(done)
  545. })
  546. it('with .sync modifier', done => {
  547. const vm = new Vue({
  548. template: `<foo ref="child" :[key].sync="value"/>`,
  549. data: {
  550. key: 'foo',
  551. value: 'bar'
  552. },
  553. components: {
  554. foo: {
  555. props: ['foo'],
  556. template: `<div>{{ foo }}</div>`
  557. }
  558. }
  559. }).$mount()
  560. expect(vm.$el.textContent).toBe('bar')
  561. vm.$refs.child.$emit('update:foo', 'baz')
  562. waitForUpdate(() => {
  563. expect(vm.value).toBe('baz')
  564. expect(vm.$el.textContent).toBe('baz')
  565. }).then(done)
  566. })
  567. })
  568. })