prop_spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var _ = require('../../../../src/util')
  2. var Vue = require('../../../../src/vue')
  3. if (_.inBrowser) {
  4. describe('prop', function () {
  5. var el
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. spyOn(_, 'warn')
  9. })
  10. it('should work', function (done) {
  11. var vm = new Vue({
  12. el: el,
  13. data: {
  14. b: 'B',
  15. test: {
  16. a: 'A'
  17. }
  18. },
  19. template: '<div v-component="test" testt="{{test}}" bb="{{b}}" v-ref="child"></div>',
  20. components: {
  21. test: {
  22. props: ['testt', 'bb'],
  23. template: '{{testt.a}} {{bb}}'
  24. }
  25. }
  26. })
  27. expect(el.firstChild.textContent).toBe('A B')
  28. vm.test.a = 'AA'
  29. vm.b = 'BB'
  30. _.nextTick(function () {
  31. expect(el.firstChild.textContent).toBe('AA BB')
  32. vm.test = { a: 'AAA' }
  33. _.nextTick(function () {
  34. expect(el.firstChild.textContent).toBe('AAA BB')
  35. vm.$data = {
  36. b: 'BBB',
  37. test: {
  38. a: 'AAAA'
  39. }
  40. }
  41. _.nextTick(function () {
  42. expect(el.firstChild.textContent).toBe('AAAA BBB')
  43. // test two-way
  44. vm.$.child.bb = 'B'
  45. vm.$.child.testt = { a: 'A' }
  46. _.nextTick(function () {
  47. expect(el.firstChild.textContent).toBe('A B')
  48. expect(vm.test.a).toBe('A')
  49. expect(vm.test).toBe(vm.$.child.testt)
  50. expect(vm.b).toBe('B')
  51. done()
  52. })
  53. })
  54. })
  55. })
  56. })
  57. it('teardown', function (done) {
  58. var vm = new Vue({
  59. el: el,
  60. data: {
  61. b: 'B'
  62. },
  63. template: '<div v-component="test" bb="{{b}}"></div>',
  64. components: {
  65. test: {
  66. props: ['bb'],
  67. template: '{{bb}}'
  68. }
  69. }
  70. })
  71. expect(el.firstChild.textContent).toBe('B')
  72. vm.b = 'BB'
  73. _.nextTick(function () {
  74. expect(el.firstChild.textContent).toBe('BB')
  75. vm._children[0]._directives[0].unbind()
  76. vm.b = 'BBB'
  77. _.nextTick(function () {
  78. expect(el.firstChild.textContent).toBe('BB')
  79. done()
  80. })
  81. })
  82. })
  83. it('block instance with replace:true', function () {
  84. var vm = new Vue({
  85. el: el,
  86. template: '<div v-component="test" b="{{a}}" c="{{d}}"></div>',
  87. data: {
  88. a: 'AAA',
  89. d: 'DDD'
  90. },
  91. components: {
  92. test: {
  93. props: ['b', 'c'],
  94. template: '<p>{{b}}</p><p>{{c}}</p>',
  95. replace: true
  96. }
  97. }
  98. })
  99. expect(el.innerHTML).toBe('<!--v-start--><p>AAA</p><p>DDD</p><!--v-end--><!--v-component-->')
  100. })
  101. })
  102. }