bind.spec.ts 17 KB

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