utils.js 11 KB

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