utils.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. describe('UNIT: Utils', function () {
  2. var utils = require('vue/src/utils')
  3. describe('hash', function () {
  4. it('should return an Object with null prototype', function () {
  5. var hash = utils.hash()
  6. assert.strictEqual(Object.getPrototypeOf(hash), null)
  7. })
  8. })
  9. describe('attr', function () {
  10. var el = document.createElement('div'),
  11. testAttr = 'transition',
  12. full = 'v-' + testAttr
  13. el.setAttribute (full, 'test')
  14. it('should append the prefix and return the attribute value', function () {
  15. var val = utils.attr(el, testAttr)
  16. assert.strictEqual(val, 'test')
  17. })
  18. it('should remove the attribute', function () {
  19. assert.notOk(el.hasAttribute(full))
  20. })
  21. it('should work with different prefix', function () {
  22. Vue.config({ prefix: 'test' })
  23. var el = document.createElement('div')
  24. el.setAttribute('test-' + testAttr, 'test')
  25. var val = utils.attr(el, testAttr)
  26. assert.strictEqual(val, 'test')
  27. assert.notOk(el.hasAttribute('test-' + testAttr))
  28. Vue.config({ prefix: 'v' })
  29. })
  30. })
  31. describe('defProtected', function () {
  32. it('should define a protected property', function () {
  33. var a = {}
  34. utils.defProtected(a, 'test', 1)
  35. var keys = []
  36. for (var key in a) {
  37. keys.push(key)
  38. }
  39. assert.strictEqual(keys.length, 0, 'inenumerable')
  40. assert.strictEqual(JSON.stringify(a), '{}', 'unstringifiable')
  41. a.test = 2
  42. assert.strictEqual(a.test, 1, 'unconfigurable')
  43. })
  44. it('should take enumerable option', function () {
  45. var a = {}
  46. utils.defProtected(a, 'test', 1, true)
  47. var keys = []
  48. for (var key in a) {
  49. keys.push(key)
  50. }
  51. assert.strictEqual(keys.length, 1, 'enumerable')
  52. assert.strictEqual(keys[0], 'test')
  53. assert.strictEqual(JSON.stringify(a), '{"test":1}', 'stringifiable')
  54. })
  55. })
  56. describe('typeOf', function () {
  57. it('should return correct type', function () {
  58. var tof = utils.typeOf
  59. assert.equal(tof({}), 'Object')
  60. assert.equal(tof([]), 'Array')
  61. assert.equal(tof(1), 'Number')
  62. assert.equal(tof(''), 'String')
  63. assert.equal(tof(true), 'Boolean')
  64. // phantomjs weirdness
  65. assert.ok(tof(null) === 'Null' || tof(null) === 'DOMWindow')
  66. assert.ok(tof(undefined) === 'Undefined' || tof(undefined) === 'DOMWindow')
  67. })
  68. })
  69. describe('toText', function () {
  70. var txt = utils.toText
  71. it('should do nothing for strings, numbers and booleans', function () {
  72. assert.strictEqual(txt('hihi'), 'hihi')
  73. assert.strictEqual(txt(123), 123)
  74. assert.strictEqual(txt(true), true)
  75. assert.strictEqual(txt(false), false)
  76. })
  77. it('should output empty string if value is not string or number', function () {
  78. assert.strictEqual(txt({}), '')
  79. assert.strictEqual(txt([]), '')
  80. assert.strictEqual(txt(undefined), '')
  81. assert.strictEqual(txt(null), '')
  82. assert.strictEqual(txt(NaN), '')
  83. })
  84. })
  85. describe('extend', function () {
  86. it('should extend the obj with extension obj', function () {
  87. var a = {a: 1}, b = {a: {}, b: 2}
  88. utils.extend(a, b)
  89. assert.strictEqual(a.a, b.a)
  90. assert.strictEqual(a.b, b.b)
  91. })
  92. it('should respect the protective option', function () {
  93. var a = {a: 1}, b = {a: {}, b: 2}
  94. utils.extend(a, b, true)
  95. assert.strictEqual(a.a, 1)
  96. assert.strictEqual(a.b, b.b)
  97. })
  98. })
  99. describe('unique', function () {
  100. it('should filter an array with duplicates into unqiue ones', function () {
  101. var arr = [1, 2, 3, 1, 2, 3, 4, 5],
  102. res = utils.unique(arr),
  103. l = res.length
  104. assert.strictEqual(l, 5)
  105. while (l--) {
  106. assert.strictEqual(res[l], 5 - l)
  107. }
  108. })
  109. })
  110. describe('bind', function () {
  111. it('should bind the right context', function () {
  112. function test () {
  113. return this + 1
  114. }
  115. var bound = utils.bind(test, 2)
  116. assert.strictEqual(bound(), 3)
  117. })
  118. })
  119. describe('toFragment', function () {
  120. it('should convert a string tempalte to a documentFragment', function () {
  121. var template = '<div class="a">hi</div><p>ha</p>',
  122. frag = utils.toFragment(template)
  123. assert.ok(frag instanceof window.DocumentFragment)
  124. assert.equal(frag.querySelector('.a').textContent, 'hi')
  125. assert.equal(frag.querySelector('p').textContent, 'ha')
  126. })
  127. it('should also work if the string is an ID selector', function () {
  128. var id = 'utils-template-to-fragment',
  129. template = '<div class="a">hi</div><p>ha</p>',
  130. el = document.createElement('template')
  131. el.id = id
  132. el.innerHTML = template
  133. document.getElementById('test').appendChild(el)
  134. var frag = utils.toFragment('#' + id)
  135. assert.ok(frag instanceof window.DocumentFragment)
  136. assert.equal(frag.querySelector('.a').textContent, 'hi')
  137. assert.equal(frag.querySelector('p').textContent, 'ha')
  138. })
  139. })
  140. describe('toConstructor', function () {
  141. it('should convert an non-VM object to a VM constructor', function () {
  142. var a = { test: 1 },
  143. A = utils.toConstructor(a)
  144. assert.ok(A.prototype instanceof Vue)
  145. assert.strictEqual(A.options, a)
  146. })
  147. it('should return the argument if it is already a consutructor', function () {
  148. var A = utils.toConstructor(Vue)
  149. assert.strictEqual(A, Vue)
  150. })
  151. })
  152. describe('processOptions', function () {
  153. var options = {
  154. partials: {
  155. a: '#utils-template-to-fragment',
  156. b: '<div class="a">hi</div><p>ha</p>'
  157. },
  158. components: {
  159. a: { scope: { data: 1 } },
  160. b: { scope: { data: 2 } }
  161. },
  162. template: '<a>{{hi}}</a>'
  163. }
  164. it('should convert string partials to fragment nodes', function () {
  165. // call it here
  166. utils.processOptions(options)
  167. var partials = options.partials
  168. for (var key in partials) {
  169. var frag = partials[key]
  170. assert.ok(frag instanceof window.DocumentFragment)
  171. assert.equal(frag.querySelector('.a').textContent, 'hi')
  172. assert.equal(frag.querySelector('p').textContent, 'ha')
  173. }
  174. })
  175. it('should convert string template to fragment node', function () {
  176. assert.ok(options.template instanceof window.DocumentFragment)
  177. assert.equal(options.template.querySelector('a').textContent, '{{hi}}')
  178. })
  179. it('should convert plain object components & elements to constructors', function () {
  180. var components = options.components
  181. assert.ok(components.a.prototype instanceof Vue)
  182. assert.strictEqual(components.a.options.scope.data, 1)
  183. assert.ok(components.b.prototype instanceof Vue)
  184. assert.strictEqual(components.b.options.scope.data, 2)
  185. })
  186. })
  187. })