misc.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. describe('Misc Features', function () {
  2. var nextTick = require('vue/src/utils').nextTick
  3. describe('inline expression', function () {
  4. it('should evaluate the correct value', function (done) {
  5. var v = new Vue({
  6. template: '{{a + "123" + b}} and {{c}}'
  7. })
  8. v.a = 'A'
  9. v.b = 'B'
  10. v.c = 'C'
  11. nextTick(function () {
  12. assert.strictEqual(v.$el.textContent, 'A123B and C')
  13. done()
  14. })
  15. })
  16. })
  17. describe('expression inside attributes', function () {
  18. it('should interpolate the attribute', function (done) {
  19. var v = new Vue({
  20. attributes: {
  21. test: 'one {{msg}} three'
  22. },
  23. data: {
  24. msg: 'two'
  25. }
  26. })
  27. assert.strictEqual(v.$el.getAttribute('test'), 'one two three')
  28. v.msg = '2'
  29. nextTick(function () {
  30. assert.strictEqual(v.$el.getAttribute('test'), 'one 2 three')
  31. done()
  32. })
  33. })
  34. })
  35. describe('triple mustache', function () {
  36. it('should set unescaped HTML', function () {
  37. var v = new Vue({
  38. template: '{{{html}}}',
  39. data: {
  40. html: '<span>a</span><a>hi</a>'
  41. }
  42. })
  43. assert.strictEqual(v.$el.innerHTML, '<span>a</span><a>hi</a><!--v-html-->')
  44. })
  45. })
  46. describe('computed properties', function () {
  47. it('should be accessible like a normal attribtue', function () {
  48. var b = 2
  49. var v = new Vue({
  50. template: '{{nested.value.a}}',
  51. data: {
  52. a: 1,
  53. },
  54. computed: {
  55. test: {
  56. $get: function () {
  57. return this.a + b
  58. },
  59. $set: function (v) {
  60. b = v - this.a
  61. }
  62. },
  63. getOnly: function () {
  64. return this.a + 1
  65. }
  66. }
  67. })
  68. assert.strictEqual(v.test, 3)
  69. assert.strictEqual(v.getOnly, 2)
  70. v.a = 2
  71. assert.strictEqual(v.test, 4)
  72. assert.strictEqual(v.getOnly, 3)
  73. b = 3
  74. assert.strictEqual(v.test, 5)
  75. v.test = 10
  76. assert.strictEqual(b, 8)
  77. })
  78. it('should be bindable in templates, even nested', function (done) {
  79. var v = new Vue({
  80. template: '{{nested.value.a}}',
  81. data: { a: 1 },
  82. computed: {
  83. nested: function () {
  84. return {
  85. value: {
  86. a: this.a + 2
  87. }
  88. }
  89. }
  90. }
  91. })
  92. assert.strictEqual(v.$el.textContent, '3')
  93. v.a = 2
  94. nextTick(function () {
  95. assert.strictEqual(v.$el.textContent, '4')
  96. done()
  97. })
  98. })
  99. })
  100. describe('setting an object to empty', function () {
  101. it('should emit undefined for paths in the old object', function (done) {
  102. var v = new Vue({
  103. data: {
  104. a: {
  105. b: { c: 1 }
  106. }
  107. }
  108. })
  109. var emitted = false
  110. v.$watch('a.b.c', function (v) {
  111. assert.strictEqual(v, undefined)
  112. emitted = true
  113. })
  114. v.a = {}
  115. nextTick(function () {
  116. assert.ok(emitted)
  117. done()
  118. })
  119. })
  120. })
  121. describe('event delegation', function () {
  122. var inCalled = 0,
  123. outCalled = 0,
  124. innerHandler = function () {}
  125. var v = new Vue({
  126. template: '<div v-on="click:out"><div class="inner" v-on="click:in"></div></div>',
  127. methods: {
  128. 'in': function (e) {
  129. inCalled++
  130. innerHandler(e)
  131. },
  132. out: function () {
  133. outCalled++
  134. }
  135. }
  136. })
  137. v.$appendTo('#test')
  138. it('should work', function () {
  139. var e = mockMouseEvent('click')
  140. v.$el.querySelector('.inner').dispatchEvent(e)
  141. assert.strictEqual(inCalled, 1)
  142. assert.strictEqual(outCalled, 1)
  143. })
  144. it('should allow stopPropagation()', function () {
  145. innerHandler = function (e) {
  146. e.stopPropagation()
  147. }
  148. var e = mockMouseEvent('click')
  149. v.$el.querySelector('.inner').dispatchEvent(e)
  150. assert.strictEqual(inCalled, 2)
  151. assert.strictEqual(outCalled, 1)
  152. })
  153. })
  154. })