utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var utils = require('seed/src/utils')
  2. describe('UNIT: Utils', function () {
  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('defProtected', function () {
  10. it('should define a protected property', function () {
  11. var a = {}
  12. utils.defProtected(a, 'test', 1)
  13. var keys = []
  14. for (var key in a) {
  15. keys.push(key)
  16. }
  17. assert.strictEqual(keys.length, 0, 'inenumerable')
  18. assert.strictEqual(JSON.stringify(a), '{}', 'unstringifiable')
  19. a.test = 2
  20. assert.strictEqual(a.test, 1, 'unconfigurable')
  21. })
  22. it('should take enumerable option', function () {
  23. var a = {}
  24. utils.defProtected(a, 'test', 1, true)
  25. var keys = []
  26. for (var key in a) {
  27. keys.push(key)
  28. }
  29. assert.strictEqual(keys.length, 1, 'enumerable')
  30. assert.strictEqual(keys[0], 'test')
  31. assert.strictEqual(JSON.stringify(a), '{"test":1}', 'stringifiable')
  32. })
  33. })
  34. describe('typeOf', function () {
  35. it('should return correct type', function () {
  36. var tof = utils.typeOf
  37. assert.equal(tof({}), 'Object')
  38. assert.equal(tof([]), 'Array')
  39. assert.equal(tof(1), 'Number')
  40. assert.equal(tof(''), 'String')
  41. assert.equal(tof(true), 'Boolean')
  42. // phantomjs weirdness
  43. assert.ok(tof(null) === 'Null' || tof(null) === 'DOMWindow')
  44. assert.ok(tof(undefined) === 'Undefined' || tof(undefined) === 'DOMWindow')
  45. })
  46. })
  47. describe('toText', function () {
  48. var txt = utils.toText
  49. it('should do nothing for strings and numbers', function () {
  50. assert.strictEqual(txt('hihi'), 'hihi')
  51. assert.strictEqual(txt(123), 123)
  52. })
  53. it('should output empty string if value is not string or number', function () {
  54. assert.strictEqual(txt({}), '')
  55. assert.strictEqual(txt([]), '')
  56. assert.strictEqual(txt(false), '')
  57. assert.strictEqual(txt(true), '')
  58. assert.strictEqual(txt(undefined), '')
  59. assert.strictEqual(txt(null), '')
  60. assert.strictEqual(txt(NaN), '')
  61. })
  62. })
  63. describe('extend', function () {
  64. it('should extend the obj with extension obj', function () {
  65. var a = {a: 1}, b = {a: {}, b: 2}
  66. utils.extend(a, b)
  67. assert.strictEqual(a.a, b.a)
  68. assert.strictEqual(a.b, b.b)
  69. })
  70. it('should respect the protective option', function () {
  71. var a = {a: 1}, b = {a: {}, b: 2}
  72. utils.extend(a, b, true)
  73. assert.strictEqual(a.a, 1)
  74. assert.strictEqual(a.b, b.b)
  75. })
  76. })
  77. describe('templateToFragment', function () {
  78. it('should convert a string tempalte to a documentFragment', function () {
  79. var template = '<div class="a">hi</div><p>ha</p>',
  80. frag = utils.templateToFragment(template)
  81. assert.ok(frag instanceof window.DocumentFragment)
  82. assert.equal(frag.querySelector('.a').textContent, 'hi')
  83. assert.equal(frag.querySelector('p').textContent, 'ha')
  84. })
  85. it('should also work if the string is an ID selector', function () {
  86. var id = 'utils-template-to-fragment',
  87. template = '<div class="a">hi</div><p>ha</p>',
  88. el = document.createElement('template')
  89. el.id = id
  90. el.innerHTML = template
  91. document.getElementById('test').appendChild(el)
  92. var frag = utils.templateToFragment('#' + id)
  93. assert.ok(frag instanceof window.DocumentFragment)
  94. assert.equal(frag.querySelector('.a').textContent, 'hi')
  95. assert.equal(frag.querySelector('p').textContent, 'ha')
  96. })
  97. })
  98. describe('convertPartials', function () {
  99. it('should convert a hash object of strings to fragments', function () {
  100. var partials = {
  101. a: '#utils-template-to-fragment',
  102. b: '<div class="a">hi</div><p>ha</p>'
  103. }
  104. utils.convertPartials(partials)
  105. for (var key in partials) {
  106. var frag = partials[key]
  107. assert.ok(frag instanceof window.DocumentFragment)
  108. assert.equal(frag.querySelector('.a').textContent, 'hi')
  109. assert.equal(frag.querySelector('p').textContent, 'ha')
  110. }
  111. })
  112. })
  113. })