utils.js 11 KB

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