bind.spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 :draggable="foo">hello</span></div>',
  69. data: { foo: true }
  70. }).$mount()
  71. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('true')
  72. vm.foo = 'again'
  73. waitForUpdate(() => {
  74. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('true')
  75. vm.foo = null
  76. }).then(() => {
  77. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('false')
  78. vm.foo = ''
  79. }).then(() => {
  80. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('true')
  81. vm.foo = false
  82. }).then(() => {
  83. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('false')
  84. vm.foo = 'false'
  85. }).then(() => {
  86. expect(vm.$el.firstChild.getAttribute('draggable')).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. it('.camel modifier', () => {
  129. const vm = new Vue({
  130. template: '<svg :view-box.camel="viewBox"></svg>',
  131. data: {
  132. viewBox: '0 0 1 1'
  133. }
  134. }).$mount()
  135. expect(vm.$el.getAttribute('viewBox')).toBe('0 0 1 1')
  136. })
  137. it('.sync modifier', done => {
  138. const vm = new Vue({
  139. template: `<test :foo-bar.sync="bar"/>`,
  140. data: {
  141. bar: 1
  142. },
  143. components: {
  144. test: {
  145. props: ['fooBar'],
  146. template: `<div @click="$emit('update:fooBar', 2)">{{ fooBar }}</div>`
  147. }
  148. }
  149. }).$mount()
  150. expect(vm.$el.textContent).toBe('1')
  151. triggerEvent(vm.$el, 'click')
  152. waitForUpdate(() => {
  153. expect(vm.$el.textContent).toBe('2')
  154. }).then(done)
  155. })
  156. it('bind object', done => {
  157. const vm = new Vue({
  158. template: '<input v-bind="test">',
  159. data: {
  160. test: {
  161. id: 'test',
  162. class: 'ok',
  163. value: 'hello'
  164. }
  165. }
  166. }).$mount()
  167. expect(vm.$el.getAttribute('id')).toBe('test')
  168. expect(vm.$el.getAttribute('class')).toBe('ok')
  169. expect(vm.$el.value).toBe('hello')
  170. vm.test.id = 'hi'
  171. vm.test.value = 'bye'
  172. waitForUpdate(() => {
  173. expect(vm.$el.getAttribute('id')).toBe('hi')
  174. expect(vm.$el.getAttribute('class')).toBe('ok')
  175. expect(vm.$el.value).toBe('bye')
  176. }).then(done)
  177. })
  178. it('.sync modifier with bind object', done => {
  179. const vm = new Vue({
  180. template: `<test v-bind.sync="test"/>`,
  181. data: {
  182. test: {
  183. fooBar: 1
  184. }
  185. },
  186. components: {
  187. test: {
  188. props: ['fooBar'],
  189. template: `<div @click="handleUpdate">{{ fooBar }}</div>`,
  190. methods: {
  191. handleUpdate () {
  192. this.$emit('update:fooBar', 2)
  193. }
  194. }
  195. }
  196. }
  197. }).$mount()
  198. expect(vm.$el.textContent).toBe('1')
  199. triggerEvent(vm.$el, 'click')
  200. waitForUpdate(() => {
  201. expect(vm.$el.textContent).toBe('2')
  202. vm.test.fooBar = 3
  203. }).then(() => {
  204. expect(vm.$el.textContent).toBe('3')
  205. }).then(done)
  206. })
  207. it('bind object with overwrite', done => {
  208. const vm = new Vue({
  209. template: '<input v-bind="test" id="foo" :class="test.value">',
  210. data: {
  211. test: {
  212. id: 'test',
  213. class: 'ok',
  214. value: 'hello'
  215. }
  216. }
  217. }).$mount()
  218. expect(vm.$el.getAttribute('id')).toBe('foo')
  219. expect(vm.$el.getAttribute('class')).toBe('hello')
  220. expect(vm.$el.value).toBe('hello')
  221. vm.test.id = 'hi'
  222. vm.test.value = 'bye'
  223. waitForUpdate(() => {
  224. expect(vm.$el.getAttribute('id')).toBe('foo')
  225. expect(vm.$el.getAttribute('class')).toBe('bye')
  226. expect(vm.$el.value).toBe('bye')
  227. }).then(done)
  228. })
  229. it('bind object with class/style', done => {
  230. const vm = new Vue({
  231. template: '<input class="a" style="color:red" v-bind="test">',
  232. data: {
  233. test: {
  234. id: 'test',
  235. class: ['b', 'c'],
  236. style: { fontSize: '12px' }
  237. }
  238. }
  239. }).$mount()
  240. expect(vm.$el.id).toBe('test')
  241. expect(vm.$el.className).toBe('a b c')
  242. expect(vm.$el.style.color).toBe('red')
  243. expect(vm.$el.style.fontSize).toBe('12px')
  244. vm.test.id = 'hi'
  245. vm.test.class = ['d']
  246. vm.test.style = { fontSize: '14px' }
  247. waitForUpdate(() => {
  248. expect(vm.$el.id).toBe('hi')
  249. expect(vm.$el.className).toBe('a d')
  250. expect(vm.$el.style.color).toBe('red')
  251. expect(vm.$el.style.fontSize).toBe('14px')
  252. }).then(done)
  253. })
  254. it('bind object as prop', done => {
  255. const vm = new Vue({
  256. template: '<input v-bind.prop="test">',
  257. data: {
  258. test: {
  259. id: 'test',
  260. className: 'ok',
  261. value: 'hello'
  262. }
  263. }
  264. }).$mount()
  265. expect(vm.$el.id).toBe('test')
  266. expect(vm.$el.className).toBe('ok')
  267. expect(vm.$el.value).toBe('hello')
  268. vm.test.id = 'hi'
  269. vm.test.className = 'okay'
  270. vm.test.value = 'bye'
  271. waitForUpdate(() => {
  272. expect(vm.$el.id).toBe('hi')
  273. expect(vm.$el.className).toBe('okay')
  274. expect(vm.$el.value).toBe('bye')
  275. }).then(done)
  276. })
  277. it('bind array', done => {
  278. const vm = new Vue({
  279. template: '<input v-bind="test">',
  280. data: {
  281. test: [
  282. { id: 'test', class: 'ok' },
  283. { value: 'hello' }
  284. ]
  285. }
  286. }).$mount()
  287. expect(vm.$el.getAttribute('id')).toBe('test')
  288. expect(vm.$el.getAttribute('class')).toBe('ok')
  289. expect(vm.$el.value).toBe('hello')
  290. vm.test[0].id = 'hi'
  291. vm.test[1].value = 'bye'
  292. waitForUpdate(() => {
  293. expect(vm.$el.getAttribute('id')).toBe('hi')
  294. expect(vm.$el.getAttribute('class')).toBe('ok')
  295. expect(vm.$el.value).toBe('bye')
  296. }).then(done)
  297. })
  298. it('warn expect object', () => {
  299. new Vue({
  300. template: '<input v-bind="test">',
  301. data: {
  302. test: 1
  303. }
  304. }).$mount()
  305. expect('v-bind without argument expects an Object or Array value').toHaveBeenWarned()
  306. })
  307. it('set value for option element', () => {
  308. const vm = new Vue({
  309. template: '<select><option :value="val">val</option></select>',
  310. data: {
  311. val: 'val'
  312. }
  313. }).$mount()
  314. // check value attribute
  315. expect(vm.$el.options[0].getAttribute('value')).toBe('val')
  316. })
  317. // a vdom patch edge case where the user has several un-keyed elements of the
  318. // same tag next to each other, and toggling them.
  319. it('properly update for toggling un-keyed children', done => {
  320. const vm = new Vue({
  321. template: `
  322. <div>
  323. <div v-if="ok" id="a" data-test="1"></div>
  324. <div v-if="!ok" id="b"></div>
  325. </div>
  326. `,
  327. data: {
  328. ok: true
  329. }
  330. }).$mount()
  331. expect(vm.$el.children[0].id).toBe('a')
  332. expect(vm.$el.children[0].getAttribute('data-test')).toBe('1')
  333. vm.ok = false
  334. waitForUpdate(() => {
  335. expect(vm.$el.children[0].id).toBe('b')
  336. expect(vm.$el.children[0].getAttribute('data-test')).toBe(null)
  337. }).then(done)
  338. })
  339. describe('bind object with special attribute', () => {
  340. function makeInstance (options) {
  341. return new Vue({
  342. template: `<div>${options.parentTemp}</div>`,
  343. data: {
  344. attrs: {
  345. [options.attr]: options.value
  346. }
  347. },
  348. components: {
  349. comp: {
  350. template: options.childTemp
  351. }
  352. }
  353. }).$mount()
  354. }
  355. it('key', () => {
  356. const vm = makeInstance({
  357. attr: 'key',
  358. value: 'test',
  359. parentTemp: '<div v-bind="attrs"></div>'
  360. })
  361. expect(vm._vnode.children[0].key).toBe('test')
  362. })
  363. it('ref', () => {
  364. const vm = makeInstance({
  365. attr: 'ref',
  366. value: 'test',
  367. parentTemp: '<div v-bind="attrs"></div>'
  368. })
  369. expect(vm.$refs.test).toBe(vm.$el.firstChild)
  370. })
  371. it('slot', () => {
  372. const vm = makeInstance({
  373. attr: 'slot',
  374. value: 'test',
  375. parentTemp: '<comp><span v-bind="attrs">123</span></comp>',
  376. childTemp: '<div>slot:<slot name="test"></slot></div>'
  377. })
  378. expect(vm.$el.innerHTML).toBe('<div>slot:<span>123</span></div>')
  379. })
  380. it('is', () => {
  381. const vm = makeInstance({
  382. attr: 'is',
  383. value: 'comp',
  384. parentTemp: '<component v-bind="attrs"></component>',
  385. childTemp: '<div>comp</div>'
  386. })
  387. expect(vm.$el.innerHTML).toBe('<div>comp</div>')
  388. })
  389. })
  390. })