utils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. describe('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('get', function () {
  11. it('should get value', function () {
  12. var obj = { a: { b: { c: 123 }}}
  13. assert.strictEqual(utils.get(obj, 'a.b.c'), 123)
  14. })
  15. it('should work on keypath with brackets', function () {
  16. var obj = { a: { 'key-with-dash': { b: 123 } }}
  17. assert.strictEqual(utils.get(obj, 'a["key-with-dash"].b'), 123)
  18. assert.strictEqual(utils.get(obj, "a['key-with-dash'].b"), 123)
  19. })
  20. it('should return undefined if path does not exist', function () {
  21. var obj = { a: {}}
  22. assert.strictEqual(utils.get(obj, 'a.b.c'), undefined)
  23. })
  24. })
  25. describe('set', function () {
  26. it('should set value', function () {
  27. var obj = { a: { b: { c: 0 }}}
  28. utils.set(obj, 'a.b.c', 123)
  29. assert.strictEqual(obj.a.b.c, 123)
  30. })
  31. it('should work on keypath with brackets', function () {
  32. var obj = { a: { 'key-with-dash': { b: 1 }}}
  33. utils.set(obj, 'a["key-with-dash"].b', 2)
  34. assert.strictEqual(obj.a['key-with-dash'].b, 2)
  35. utils.set(obj, "a['key-with-dash'].b", 3)
  36. assert.strictEqual(obj.a['key-with-dash'].b, 3)
  37. })
  38. it('should set even if path does not exist', function () {
  39. var obj = {}
  40. utils.set(obj, 'a.b.c', 123)
  41. assert.strictEqual(obj.a.b.c, 123)
  42. })
  43. })
  44. describe('hash', function () {
  45. it('should return an Object with null prototype', function () {
  46. var hash = utils.hash()
  47. assert.strictEqual(Object.getPrototypeOf(hash), null)
  48. })
  49. })
  50. describe('attr', function () {
  51. var el = document.createElement('div'),
  52. testAttr = 'transition',
  53. full = 'v-' + testAttr
  54. el.setAttribute (full, 'test')
  55. it('should append the prefix and return the attribute value', function () {
  56. var val = utils.attr(el, testAttr)
  57. assert.strictEqual(val, 'test')
  58. })
  59. it('should remove the attribute', function () {
  60. assert.notOk(el.hasAttribute(full))
  61. })
  62. it('should work with different prefix', function () {
  63. Vue.config({ prefix: 'test' })
  64. var el = document.createElement('div')
  65. el.setAttribute('test-' + testAttr, 'test')
  66. var val = utils.attr(el, testAttr)
  67. assert.strictEqual(val, 'test')
  68. assert.notOk(el.hasAttribute('test-' + testAttr))
  69. Vue.config({ prefix: 'v' })
  70. })
  71. })
  72. describe('defProtected', function () {
  73. it('should define a protected property', function () {
  74. var a = {}
  75. utils.defProtected(a, 'test', 1)
  76. var keys = []
  77. for (var key in a) {
  78. keys.push(key)
  79. }
  80. assert.strictEqual(keys.length, 0, 'inenumerable')
  81. assert.strictEqual(JSON.stringify(a), '{}', 'unstringifiable')
  82. a.test = 2
  83. assert.strictEqual(a.test, 1, 'unconfigurable')
  84. })
  85. it('should take enumerable option', function () {
  86. var a = {}
  87. utils.defProtected(a, 'test', 1, true)
  88. var keys = []
  89. for (var key in a) {
  90. keys.push(key)
  91. }
  92. assert.strictEqual(keys.length, 1, 'enumerable')
  93. assert.strictEqual(keys[0], 'test')
  94. assert.strictEqual(JSON.stringify(a), '{"test":1}', 'stringifiable')
  95. })
  96. })
  97. describe('isObject', function () {
  98. it('should return correct result', function () {
  99. var iso = utils.isObject
  100. assert.ok(iso({}))
  101. assert.notOk(iso([]))
  102. assert.notOk(iso(1))
  103. assert.notOk(iso(true))
  104. assert.notOk(iso(null))
  105. assert.notOk(iso(undefined))
  106. })
  107. })
  108. describe('isTrueObject', function () {
  109. it('should return correct result', function () {
  110. var iso = utils.isTrueObject
  111. assert.ok(iso({}))
  112. assert.notOk(iso([]))
  113. assert.notOk(iso(1))
  114. assert.notOk(iso(true))
  115. assert.notOk(iso(null))
  116. assert.notOk(iso(undefined))
  117. assert.notOk(iso(document.createElement('div')))
  118. })
  119. })
  120. describe('guard', function () {
  121. it('should output empty string if value is null or undefined', function () {
  122. assert.strictEqual(utils.guard(undefined), '')
  123. assert.strictEqual(utils.guard(null), '')
  124. })
  125. it('should output stringified data if value is object', function () {
  126. assert.strictEqual(utils.guard({a:1}), '{"a":1}')
  127. assert.strictEqual(utils.guard([1,2,3]), '[1,2,3]')
  128. })
  129. })
  130. describe('extend', function () {
  131. it('should extend the obj with extension obj', function () {
  132. var a = {a: 1}, b = {a: {}, b: 2}
  133. utils.extend(a, b)
  134. assert.strictEqual(a.a, b.a)
  135. assert.strictEqual(a.b, b.b)
  136. })
  137. it('should always return the extended object', function () {
  138. var a = {a: 1}, b = {a: {}, b: 2}
  139. assert.strictEqual(a, utils.extend(a, b))
  140. assert.strictEqual(a, utils.extend(a, undefined))
  141. })
  142. })
  143. describe('unique', function () {
  144. it('should filter an array with duplicates into unqiue ones', function () {
  145. var arr = [1, 2, 3, 1, 2, 3, 4, 5],
  146. res = utils.unique(arr),
  147. l = res.length
  148. assert.strictEqual(l, 5)
  149. while (l--) {
  150. assert.strictEqual(res[l], 5 - l)
  151. }
  152. })
  153. })
  154. describe('bind', function () {
  155. it('should bind the right context', function () {
  156. function test () {
  157. return this + 1
  158. }
  159. var bound = utils.bind(test, 2)
  160. assert.strictEqual(bound(), 3)
  161. })
  162. })
  163. describe('toFragment', function () {
  164. it('should convert a string tempalte to a documentFragment', function () {
  165. var template = '<div class="a">hi</div><p>ha</p>',
  166. frag = utils.toFragment(template)
  167. assert.ok(frag instanceof window.DocumentFragment)
  168. assert.equal(frag.querySelector('.a').textContent, 'hi')
  169. assert.equal(frag.querySelector('p').textContent, 'ha')
  170. })
  171. it('should also work if the string is an ID selector', function () {
  172. var id = 'utils-template-to-fragment',
  173. template = '<div class="a">hi</div><p>ha</p>',
  174. el = document.createElement('template')
  175. el.id = id
  176. el.innerHTML = template
  177. document.getElementById('test').appendChild(el)
  178. var frag = utils.toFragment('#' + id)
  179. assert.ok(frag instanceof window.DocumentFragment)
  180. assert.equal(frag.querySelector('.a').textContent, 'hi')
  181. assert.equal(frag.querySelector('p').textContent, 'ha')
  182. })
  183. it('should work with table elements', function () {
  184. var frag = utils.toFragment('<td></td>')
  185. assert.ok(frag instanceof window.DocumentFragment)
  186. assert.equal(frag.firstChild.tagName, 'TD')
  187. })
  188. it('should work with SVG elements', function () {
  189. var frag = utils.toFragment('<polygon></polygon>')
  190. assert.ok(frag instanceof window.DocumentFragment)
  191. assert.equal(frag.firstChild.tagName.toLowerCase(), 'polygon')
  192. assert.equal(frag.firstChild.namespaceURI, 'http://www.w3.org/2000/svg')
  193. })
  194. })
  195. describe('toConstructor', function () {
  196. it('should convert an non-VM object to a VM constructor', function () {
  197. var a = { test: 1 },
  198. A = utils.toConstructor(a)
  199. assert.ok(A.prototype instanceof Vue)
  200. assert.strictEqual(A.options, a)
  201. })
  202. it('should return the argument if it is already a consutructor', function () {
  203. var A = utils.toConstructor(Vue)
  204. assert.strictEqual(A, Vue)
  205. })
  206. })
  207. describe('processOptions', function () {
  208. var options = {
  209. partials: {
  210. a: '#utils-template-to-fragment',
  211. b: '<div class="a">hi</div><p>ha</p>'
  212. },
  213. components: {
  214. a: { data: { data: 1 } },
  215. b: { data: { data: 2 } }
  216. },
  217. template: '<a>{{hi}}</a>'
  218. }
  219. it('should convert string partials to fragment nodes', function () {
  220. // call it here
  221. utils.processOptions(options)
  222. var partials = options.partials
  223. for (var key in partials) {
  224. var frag = partials[key]
  225. assert.ok(frag instanceof window.DocumentFragment)
  226. assert.equal(frag.querySelector('.a').textContent, 'hi')
  227. assert.equal(frag.querySelector('p').textContent, 'ha')
  228. }
  229. })
  230. it('should convert string template to fragment node', function () {
  231. assert.ok(options.template instanceof window.DocumentFragment)
  232. assert.equal(options.template.querySelector('a').textContent, '{{hi}}')
  233. })
  234. it('should convert plain object components & elements to constructors', function () {
  235. var components = options.components
  236. assert.ok(components.a.prototype instanceof Vue)
  237. assert.strictEqual(components.a.options.defaultData.data, 1)
  238. assert.ok(components.b.prototype instanceof Vue)
  239. assert.strictEqual(components.b.options.defaultData.data, 2)
  240. })
  241. })
  242. describe('log', function () {
  243. if (!window.console) return
  244. it('should only log in debug mode', function () {
  245. // overwrite log temporarily
  246. var oldLog = console.log,
  247. logged
  248. console.log = function (msg) {
  249. logged = msg
  250. }
  251. utils.log('123')
  252. assert.notOk(logged)
  253. config.debug = true
  254. utils.log('123')
  255. assert.strictEqual(logged, '123')
  256. // teardown
  257. config.debug = false
  258. console.log = oldLog
  259. })
  260. })
  261. describe('warn', function () {
  262. if (!window.console) return
  263. it('should only warn when not in silent mode', function () {
  264. config.silent = true
  265. var oldWarn = console.warn,
  266. warned
  267. console.warn = function (msg) {
  268. warned = msg
  269. }
  270. utils.warn('123')
  271. assert.notOk(warned)
  272. config.silent = false
  273. utils.warn('123')
  274. assert.strictEqual(warned, '123')
  275. console.warn = oldWarn
  276. })
  277. it('should also trace in debug mode', function () {
  278. config.silent = false
  279. config.debug = true
  280. var oldTrace = console.trace,
  281. oldWarn = console.warn,
  282. traced
  283. console.warn = function () {}
  284. console.trace = function () {
  285. traced = true
  286. }
  287. utils.warn('testing trace')
  288. assert.ok(traced)
  289. config.silent = true
  290. config.debug = false
  291. console.trace = oldTrace
  292. console.warn = oldWarn
  293. })
  294. })
  295. describe('addClass', function () {
  296. var el = document.createElement('div')
  297. it('should work', function () {
  298. utils.addClass(el, 'hihi')
  299. assert.strictEqual(el.className, 'hihi')
  300. utils.addClass(el, 'hi')
  301. assert.strictEqual(el.className, 'hihi hi')
  302. })
  303. it('should not add duplicate', function () {
  304. utils.addClass(el, 'hi')
  305. assert.strictEqual(el.className, 'hihi hi')
  306. })
  307. })
  308. describe('removeClass', function () {
  309. it('should work', function () {
  310. var el = document.createElement('div')
  311. el.className = 'hihi hi ha'
  312. utils.removeClass(el, 'hi')
  313. assert.strictEqual(el.className, 'hihi ha')
  314. utils.removeClass(el, 'ha')
  315. assert.strictEqual(el.className, 'hihi')
  316. })
  317. })
  318. describe('checkNumber', function () {
  319. // TODO
  320. })
  321. describe('objectToArray', function () {
  322. // TODO
  323. })
  324. })