lang_spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var _ = require('../../../../src/util')
  2. describe('Util - Language Enhancement', function () {
  3. it('toString', function () {
  4. expect(_.toString('hi')).toBe('hi')
  5. expect(_.toString(1.234)).toBe('1.234')
  6. expect(_.toString(null)).toBe('')
  7. expect(_.toString(undefined)).toBe('')
  8. })
  9. it('toNumber', function () {
  10. expect(_.toNumber('12')).toBe(12)
  11. expect(_.toNumber('1e5')).toBe(1e5)
  12. expect(_.toNumber('0x2F')).toBe(0x2F)
  13. expect(_.toNumber(null)).toBe(null)
  14. expect(_.toNumber(true)).toBe(true)
  15. expect(_.toNumber('hello')).toBe('hello')
  16. })
  17. it('strip quotes', function () {
  18. expect(_.stripQuotes('"123"')).toBe('123')
  19. expect(_.stripQuotes("'fff'")).toBe('fff')
  20. expect(_.stripQuotes("'fff")).toBe(false)
  21. })
  22. it('camelize', function () {
  23. expect(_.camelize('abc')).toBe('abc')
  24. expect(_.camelize('some-long-name')).toBe('someLongName')
  25. })
  26. it('classify', function () {
  27. expect(_.classify('abc')).toBe('Abc')
  28. expect(_.classify('some-long-name')).toBe('SomeLongName')
  29. expect(_.classify('what_about_this')).toBe('WhatAboutThis')
  30. expect(_.classify('how/about/that')).toBe('HowAboutThat')
  31. })
  32. it('bind', function () {
  33. var original = function (a) {
  34. return this.a + a
  35. }
  36. var ctx = { a: 'ctx a ' }
  37. var bound = _.bind(original, ctx)
  38. var res = bound('arg a')
  39. expect(res).toBe('ctx a arg a')
  40. })
  41. it('toArray', function () {
  42. // should make a copy of original array
  43. var arr = [1,2,3]
  44. var res = _.toArray(arr)
  45. expect(Array.isArray(res)).toBe(true)
  46. expect(res.toString()).toEqual('1,2,3')
  47. expect(res).not.toBe(arr)
  48. // should work on arguments
  49. ;(function () {
  50. var res = _.toArray(arguments)
  51. expect(Array.isArray(res)).toBe(true)
  52. expect(res.toString()).toEqual('1,2,3')
  53. })(1,2,3)
  54. })
  55. it('extend', function () {
  56. var from = {a:1,b:2}
  57. var to = {}
  58. var res = _.extend(to, from)
  59. expect(to.a).toBe(from.a)
  60. expect(to.b).toBe(from.b)
  61. expect(res).toBe(to)
  62. })
  63. it('isObject', function () {
  64. expect(_.isObject({})).toBe(true)
  65. expect(_.isObject([])).toBe(true)
  66. expect(_.isObject(null)).toBeFalsy()
  67. expect(_.isObject(123)).toBeFalsy()
  68. expect(_.isObject(true)).toBeFalsy()
  69. expect(_.isObject('hi')).toBeFalsy()
  70. expect(_.isObject(undefined)).toBeFalsy()
  71. expect(_.isObject(function(){})).toBeFalsy()
  72. })
  73. it('isPlainObject', function () {
  74. expect(_.isPlainObject({})).toBe(true)
  75. expect(_.isPlainObject([])).toBe(false)
  76. expect(_.isPlainObject(null)).toBe(false)
  77. expect(_.isPlainObject(null)).toBeFalsy()
  78. expect(_.isPlainObject(123)).toBeFalsy()
  79. expect(_.isPlainObject(true)).toBeFalsy()
  80. expect(_.isPlainObject('hi')).toBeFalsy()
  81. expect(_.isPlainObject(undefined)).toBeFalsy()
  82. expect(_.isPlainObject(function(){})).toBe(false)
  83. if (_.inBrowser) {
  84. expect(_.isPlainObject(window)).toBe(false)
  85. }
  86. })
  87. it('isArray', function () {
  88. expect(_.isArray([])).toBe(true)
  89. expect(_.isArray({})).toBe(false)
  90. expect(_.isArray(arguments)).toBe(false)
  91. })
  92. it('define', function () {
  93. var obj = {}
  94. _.define(obj, 'test', 123)
  95. expect(obj.test).toBe(123)
  96. var desc = Object.getOwnPropertyDescriptor(obj, 'test')
  97. expect(desc.enumerable).toBe(false)
  98. _.define(obj, 'test2', 123, true)
  99. expect(obj.test2).toBe(123)
  100. desc = Object.getOwnPropertyDescriptor(obj, 'test2')
  101. expect(desc.enumerable).toBe(true)
  102. })
  103. it('debounce', function (done) {
  104. var count = 0
  105. var fn = _.debounce(function () {
  106. count++
  107. }, 100)
  108. fn()
  109. setTimeout(fn, 10)
  110. setTimeout(fn, 20)
  111. setTimeout(function () {
  112. expect(count).toBe(0)
  113. }, 30)
  114. setTimeout(function () {
  115. expect(count).toBe(1)
  116. done()
  117. }, 200)
  118. })
  119. })