utils.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. describe('UNIT: Utils', function () {
  2. var utils = require('seed/src/utils')
  3. describe('hash', function () {
  4. it('should return an Object with null prototype', function () {
  5. var hash = utils.hash()
  6. assert.strictEqual(Object.getPrototypeOf(hash), null)
  7. })
  8. })
  9. describe('attr', function () {
  10. var el = document.createElement('div'),
  11. testAttr = 'transition',
  12. full = 'sd-' + testAttr
  13. el.setAttribute (full, 'test')
  14. it('should append the prefix and return the attribute value', function () {
  15. var val = utils.attr(el, testAttr)
  16. assert.strictEqual(val, 'test')
  17. })
  18. it('should remove the attribute', function () {
  19. assert.notOk(el.hasAttribute(full))
  20. })
  21. it('should work with different prefix', function () {
  22. Seed.config({ prefix: 'test' })
  23. var el = document.createElement('div')
  24. el.setAttribute('test-' + testAttr, 'test')
  25. var val = utils.attr(el, testAttr)
  26. assert.strictEqual(val, 'test')
  27. assert.notOk(el.hasAttribute('test-' + testAttr))
  28. Seed.config({ prefix: 'sd' })
  29. })
  30. })
  31. describe('defProtected', function () {
  32. it('should define a protected property', function () {
  33. var a = {}
  34. utils.defProtected(a, 'test', 1)
  35. var keys = []
  36. for (var key in a) {
  37. keys.push(key)
  38. }
  39. assert.strictEqual(keys.length, 0, 'inenumerable')
  40. assert.strictEqual(JSON.stringify(a), '{}', 'unstringifiable')
  41. a.test = 2
  42. assert.strictEqual(a.test, 1, 'unconfigurable')
  43. })
  44. it('should take enumerable option', function () {
  45. var a = {}
  46. utils.defProtected(a, 'test', 1, true)
  47. var keys = []
  48. for (var key in a) {
  49. keys.push(key)
  50. }
  51. assert.strictEqual(keys.length, 1, 'enumerable')
  52. assert.strictEqual(keys[0], 'test')
  53. assert.strictEqual(JSON.stringify(a), '{"test":1}', 'stringifiable')
  54. })
  55. })
  56. describe('typeOf', function () {
  57. it('should return correct type', function () {
  58. var tof = utils.typeOf
  59. assert.equal(tof({}), 'Object')
  60. assert.equal(tof([]), 'Array')
  61. assert.equal(tof(1), 'Number')
  62. assert.equal(tof(''), 'String')
  63. assert.equal(tof(true), 'Boolean')
  64. // phantomjs weirdness
  65. assert.ok(tof(null) === 'Null' || tof(null) === 'DOMWindow')
  66. assert.ok(tof(undefined) === 'Undefined' || tof(undefined) === 'DOMWindow')
  67. })
  68. })
  69. describe('toText', function () {
  70. var txt = utils.toText
  71. it('should do nothing for strings, numbers and booleans', function () {
  72. assert.strictEqual(txt('hihi'), 'hihi')
  73. assert.strictEqual(txt(123), 123)
  74. assert.strictEqual(txt(true), true)
  75. assert.strictEqual(txt(false), false)
  76. })
  77. it('should output empty string if value is not string or number', function () {
  78. assert.strictEqual(txt({}), '')
  79. assert.strictEqual(txt([]), '')
  80. assert.strictEqual(txt(undefined), '')
  81. assert.strictEqual(txt(null), '')
  82. assert.strictEqual(txt(NaN), '')
  83. })
  84. })
  85. describe('extend', function () {
  86. it('should extend the obj with extension obj', function () {
  87. var a = {a: 1}, b = {a: {}, b: 2}
  88. utils.extend(a, b)
  89. assert.strictEqual(a.a, b.a)
  90. assert.strictEqual(a.b, b.b)
  91. })
  92. it('should respect the protective option', function () {
  93. var a = {a: 1}, b = {a: {}, b: 2}
  94. utils.extend(a, b, true)
  95. assert.strictEqual(a.a, 1)
  96. assert.strictEqual(a.b, b.b)
  97. })
  98. })
  99. describe('unique', function () {
  100. it('should filter an array with duplicates into unqiue ones', function () {
  101. var arr = [1, 2, 3, 1, 2, 3, 4, 5],
  102. res = utils.unique(arr),
  103. l = res.length
  104. assert.strictEqual(l, 5)
  105. while (l--) {
  106. assert.strictEqual(res[l], 5 - l)
  107. }
  108. })
  109. })
  110. describe('toFragment', function () {
  111. it('should convert a string tempalte to a documentFragment', function () {
  112. var template = '<div class="a">hi</div><p>ha</p>',
  113. frag = utils.toFragment(template)
  114. assert.ok(frag instanceof window.DocumentFragment)
  115. assert.equal(frag.querySelector('.a').textContent, 'hi')
  116. assert.equal(frag.querySelector('p').textContent, 'ha')
  117. })
  118. it('should also work if the string is an ID selector', function () {
  119. var id = 'utils-template-to-fragment',
  120. template = '<div class="a">hi</div><p>ha</p>',
  121. el = document.createElement('template')
  122. el.id = id
  123. el.innerHTML = template
  124. document.getElementById('test').appendChild(el)
  125. var frag = utils.toFragment('#' + id)
  126. assert.ok(frag instanceof window.DocumentFragment)
  127. assert.equal(frag.querySelector('.a').textContent, 'hi')
  128. assert.equal(frag.querySelector('p').textContent, 'ha')
  129. })
  130. })
  131. describe('toConstructor', function () {
  132. it('should convert an non-VM object to a VM constructor', function () {
  133. var a = { test: 1 },
  134. A = utils.toConstructor(a)
  135. assert.ok(A.prototype instanceof Seed)
  136. assert.strictEqual(A.options, a)
  137. })
  138. it('should return the argument if it is already a consutructor', function () {
  139. var A = utils.toConstructor(Seed)
  140. assert.strictEqual(A, Seed)
  141. })
  142. })
  143. describe('processOptions', function () {
  144. var options = {
  145. partials: {
  146. a: '#utils-template-to-fragment',
  147. b: '<div class="a">hi</div><p>ha</p>'
  148. },
  149. components: {
  150. a: { scope: { data: 1 } },
  151. b: { scope: { data: 2 } }
  152. },
  153. template: '<a>{{hi}}</a>'
  154. }
  155. it('should convert string partials to fragment nodes', function () {
  156. // call it here
  157. utils.processOptions(options)
  158. var partials = options.partials
  159. for (var key in partials) {
  160. var frag = partials[key]
  161. assert.ok(frag instanceof window.DocumentFragment)
  162. assert.equal(frag.querySelector('.a').textContent, 'hi')
  163. assert.equal(frag.querySelector('p').textContent, 'ha')
  164. }
  165. })
  166. it('should convert string template to fragment node', function () {
  167. assert.ok(options.template instanceof window.DocumentFragment)
  168. assert.equal(options.template.querySelector('a').textContent, '{{hi}}')
  169. })
  170. it('should convert plain object components to constructors', function () {
  171. var components = options.components
  172. assert.ok(components.a.prototype instanceof Seed)
  173. assert.strictEqual(components.a.options.scope.data, 1)
  174. assert.ok(components.b.prototype instanceof Seed)
  175. assert.strictEqual(components.b.options.scope.data, 2)
  176. })
  177. })
  178. })