utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. })