utils.js 9.6 KB

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