utils.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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('toFragment', function () {
  100. it('should convert a string tempalte to a documentFragment', function () {
  101. var template = '<div class="a">hi</div><p>ha</p>',
  102. frag = utils.toFragment(template)
  103. assert.ok(frag instanceof window.DocumentFragment)
  104. assert.equal(frag.querySelector('.a').textContent, 'hi')
  105. assert.equal(frag.querySelector('p').textContent, 'ha')
  106. })
  107. it('should also work if the string is an ID selector', function () {
  108. var id = 'utils-template-to-fragment',
  109. template = '<div class="a">hi</div><p>ha</p>',
  110. el = document.createElement('template')
  111. el.id = id
  112. el.innerHTML = template
  113. document.getElementById('test').appendChild(el)
  114. var frag = utils.toFragment('#' + id)
  115. assert.ok(frag instanceof window.DocumentFragment)
  116. assert.equal(frag.querySelector('.a').textContent, 'hi')
  117. assert.equal(frag.querySelector('p').textContent, 'ha')
  118. })
  119. })
  120. describe('toConstructor', function () {
  121. it('should convert an non-VM object to a VM constructor', function () {
  122. var a = { test: 1 },
  123. A = utils.toConstructor(a)
  124. assert.ok(A.prototype instanceof Seed)
  125. assert.strictEqual(A.options, a)
  126. })
  127. it('should return the argument if it is already a consutructor', function () {
  128. var A = utils.toConstructor(Seed)
  129. assert.strictEqual(A, Seed)
  130. })
  131. })
  132. describe('processOptions', function () {
  133. var options = {
  134. partials: {
  135. a: '#utils-template-to-fragment',
  136. b: '<div class="a">hi</div><p>ha</p>'
  137. },
  138. components: {
  139. a: { scope: { data: 1 } },
  140. b: { scope: { data: 2 } }
  141. },
  142. template: '<a>{{hi}}</a>'
  143. }
  144. it('should convert string partials to fragment nodes', function () {
  145. // call it here
  146. utils.processOptions(options)
  147. var partials = options.partials
  148. for (var key in partials) {
  149. var frag = partials[key]
  150. assert.ok(frag instanceof window.DocumentFragment)
  151. assert.equal(frag.querySelector('.a').textContent, 'hi')
  152. assert.equal(frag.querySelector('p').textContent, 'ha')
  153. }
  154. })
  155. it('should convert string template to fragment node', function () {
  156. assert.ok(options.template instanceof window.DocumentFragment)
  157. assert.equal(options.template.querySelector('a').textContent, '{{hi}}')
  158. })
  159. it('should convert plain object components to constructors', function () {
  160. var components = options.components
  161. assert.ok(components.a.prototype instanceof Seed)
  162. assert.strictEqual(components.a.options.scope.data, 1)
  163. assert.ok(components.b.prototype instanceof Seed)
  164. assert.strictEqual(components.b.options.scope.data, 2)
  165. })
  166. })
  167. })