utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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(undefined), '')
  86. assert.strictEqual(txt(null), '')
  87. assert.strictEqual(txt(NaN), '')
  88. })
  89. it('should stringify value if is object', function () {
  90. assert.strictEqual(txt({foo:"bar"}), '{"foo":"bar"}')
  91. })
  92. })
  93. describe('extend', function () {
  94. it('should extend the obj with extension obj', function () {
  95. var a = {a: 1}, b = {a: {}, b: 2}
  96. utils.extend(a, b)
  97. assert.strictEqual(a.a, b.a)
  98. assert.strictEqual(a.b, b.b)
  99. })
  100. it('should respect the protective option', function () {
  101. var a = {a: 1}, b = {a: {}, b: 2}
  102. utils.extend(a, b, true)
  103. assert.strictEqual(a.a, 1)
  104. assert.strictEqual(a.b, b.b)
  105. })
  106. })
  107. describe('unique', function () {
  108. it('should filter an array with duplicates into unqiue ones', function () {
  109. var arr = [1, 2, 3, 1, 2, 3, 4, 5],
  110. res = utils.unique(arr),
  111. l = res.length
  112. assert.strictEqual(l, 5)
  113. while (l--) {
  114. assert.strictEqual(res[l], 5 - l)
  115. }
  116. })
  117. })
  118. describe('bind', function () {
  119. it('should bind the right context', function () {
  120. function test () {
  121. return this + 1
  122. }
  123. var bound = utils.bind(test, 2)
  124. assert.strictEqual(bound(), 3)
  125. })
  126. })
  127. describe('toFragment', function () {
  128. it('should convert a string tempalte to a documentFragment', function () {
  129. var template = '<div class="a">hi</div><p>ha</p>',
  130. frag = utils.toFragment(template)
  131. assert.ok(frag instanceof window.DocumentFragment)
  132. assert.equal(frag.querySelector('.a').textContent, 'hi')
  133. assert.equal(frag.querySelector('p').textContent, 'ha')
  134. })
  135. it('should also work if the string is an ID selector', function () {
  136. var id = 'utils-template-to-fragment',
  137. template = '<div class="a">hi</div><p>ha</p>',
  138. el = document.createElement('template')
  139. el.id = id
  140. el.innerHTML = template
  141. document.getElementById('test').appendChild(el)
  142. var frag = utils.toFragment('#' + id)
  143. assert.ok(frag instanceof window.DocumentFragment)
  144. assert.equal(frag.querySelector('.a').textContent, 'hi')
  145. assert.equal(frag.querySelector('p').textContent, 'ha')
  146. })
  147. })
  148. describe('toConstructor', function () {
  149. it('should convert an non-VM object to a VM constructor', function () {
  150. var a = { test: 1 },
  151. A = utils.toConstructor(a)
  152. assert.ok(A.prototype instanceof Vue)
  153. assert.strictEqual(A.options, a)
  154. })
  155. it('should return the argument if it is already a consutructor', function () {
  156. var A = utils.toConstructor(Vue)
  157. assert.strictEqual(A, Vue)
  158. })
  159. })
  160. describe('processOptions', function () {
  161. var options = {
  162. partials: {
  163. a: '#utils-template-to-fragment',
  164. b: '<div class="a">hi</div><p>ha</p>'
  165. },
  166. components: {
  167. a: { data: { data: 1 } },
  168. b: { data: { data: 2 } }
  169. },
  170. template: '<a>{{hi}}</a>'
  171. }
  172. it('should convert string partials to fragment nodes', function () {
  173. // call it here
  174. utils.processOptions(options)
  175. var partials = options.partials
  176. for (var key in partials) {
  177. var frag = partials[key]
  178. assert.ok(frag instanceof window.DocumentFragment)
  179. assert.equal(frag.querySelector('.a').textContent, 'hi')
  180. assert.equal(frag.querySelector('p').textContent, 'ha')
  181. }
  182. })
  183. it('should convert string template to fragment node', function () {
  184. assert.ok(options.template instanceof window.DocumentFragment)
  185. assert.equal(options.template.querySelector('a').textContent, '{{hi}}')
  186. })
  187. it('should convert plain object components & elements to constructors', function () {
  188. var components = options.components
  189. assert.ok(components.a.prototype instanceof Vue)
  190. assert.strictEqual(components.a.options.data.data, 1)
  191. assert.ok(components.b.prototype instanceof Vue)
  192. assert.strictEqual(components.b.options.data.data, 2)
  193. })
  194. })
  195. describe('log', function () {
  196. if (!window.console) return
  197. it('should only log in debug mode', function () {
  198. // overwrite log temporarily
  199. var oldLog = console.log,
  200. logged
  201. console.log = function (msg) {
  202. logged = msg
  203. }
  204. utils.log('123')
  205. assert.notOk(logged)
  206. config.debug = true
  207. utils.log('123')
  208. assert.strictEqual(logged, '123')
  209. // teardown
  210. config.debug = false
  211. console.log = oldLog
  212. })
  213. })
  214. describe('warn', function () {
  215. if (!window.console) return
  216. it('should only warn when not in silent mode', function () {
  217. config.silent = true
  218. var oldWarn = console.warn,
  219. warned
  220. console.warn = function (msg) {
  221. warned = msg
  222. }
  223. utils.warn('123')
  224. assert.notOk(warned)
  225. config.silent = false
  226. utils.warn('123')
  227. assert.strictEqual(warned, '123')
  228. console.warn = oldWarn
  229. })
  230. it('should also trace in debug mode', function () {
  231. config.silent = false
  232. config.debug = true
  233. var oldTrace = console.trace,
  234. oldWarn = console.warn,
  235. traced
  236. console.warn = function () {}
  237. console.trace = function () {
  238. traced = true
  239. }
  240. utils.warn('testing trace')
  241. assert.ok(traced)
  242. config.silent = true
  243. config.debug = false
  244. console.trace = oldTrace
  245. console.warn = oldWarn
  246. })
  247. })
  248. describe('addClass', function () {
  249. var el = document.createElement('div')
  250. it('should work', function () {
  251. utils.addClass(el, 'hihi')
  252. assert.strictEqual(el.className, 'hihi')
  253. utils.addClass(el, 'hi')
  254. assert.strictEqual(el.className, 'hihi hi')
  255. })
  256. it('should not add duplicate', function () {
  257. utils.addClass(el, 'hi')
  258. assert.strictEqual(el.className, 'hihi hi')
  259. })
  260. })
  261. describe('removeClass', function () {
  262. it('should work', function () {
  263. var el = document.createElement('div')
  264. el.className = 'hihi hi ha'
  265. utils.removeClass(el, 'hi')
  266. assert.strictEqual(el.className, 'hihi ha')
  267. utils.removeClass(el, 'ha')
  268. assert.strictEqual(el.className, 'hihi')
  269. })
  270. })
  271. })