prop_spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: '<test testt="{{test}}" bb="{{b}}" v-ref="child"></test>',
  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('warn invalid keys', function () {
  58. var vm = new Vue({
  59. el: el,
  60. template: '<test a.b.c="{{test}}"></test>',
  61. components: {
  62. test: {
  63. props: ['a.b.c']
  64. }
  65. }
  66. })
  67. expect(hasWarned(_, 'Invalid prop key')).toBe(true)
  68. })
  69. it('teardown', function (done) {
  70. var vm = new Vue({
  71. el: el,
  72. data: {
  73. b: 'B'
  74. },
  75. template: '<test bb="{{b}}"></test>',
  76. components: {
  77. test: {
  78. props: ['bb'],
  79. template: '{{bb}}'
  80. }
  81. }
  82. })
  83. expect(el.firstChild.textContent).toBe('B')
  84. vm.b = 'BB'
  85. _.nextTick(function () {
  86. expect(el.firstChild.textContent).toBe('BB')
  87. vm._children[0]._directives[0].unbind()
  88. vm.b = 'BBB'
  89. _.nextTick(function () {
  90. expect(el.firstChild.textContent).toBe('BB')
  91. done()
  92. })
  93. })
  94. })
  95. it('block instance with replace:true', function () {
  96. var vm = new Vue({
  97. el: el,
  98. template: '<test b="{{a}}" c="{{d}}"></test>',
  99. data: {
  100. a: 'AAA',
  101. d: 'DDD'
  102. },
  103. components: {
  104. test: {
  105. props: ['b', 'c'],
  106. template: '<p>{{b}}</p><p>{{c}}</p>',
  107. replace: true
  108. }
  109. }
  110. })
  111. expect(el.innerHTML).toBe('<p>AAA</p><p>DDD</p>')
  112. })
  113. })
  114. }