lang_spec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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('hyphenate', function () {
  27. expect(_.hyphenate('whatsUp')).toBe('whats-up')
  28. expect(_.hyphenate('a1BfC')).toBe('a1-bf-c')
  29. expect(_.hyphenate('already-With-Hyphen')).toBe('already-with-hyphen')
  30. })
  31. it('classify', function () {
  32. expect(_.classify('abc')).toBe('Abc')
  33. expect(_.classify('some-long-name')).toBe('SomeLongName')
  34. expect(_.classify('what_about_this')).toBe('WhatAboutThis')
  35. expect(_.classify('how/about/that')).toBe('HowAboutThat')
  36. })
  37. it('bind', function () {
  38. var original = function (a) {
  39. return this.a + a
  40. }
  41. var ctx = { a: 'ctx a ' }
  42. var bound = _.bind(original, ctx)
  43. var res = bound('arg a')
  44. expect(res).toBe('ctx a arg a')
  45. })
  46. it('toArray', function () {
  47. // should make a copy of original array
  48. var arr = [1, 2, 3]
  49. var res = _.toArray(arr)
  50. expect(Array.isArray(res)).toBe(true)
  51. expect(res.toString()).toEqual('1,2,3')
  52. expect(res).not.toBe(arr)
  53. // should work on arguments
  54. ;(function () {
  55. var res = _.toArray(arguments)
  56. expect(Array.isArray(res)).toBe(true)
  57. expect(res.toString()).toEqual('1,2,3')
  58. })(1, 2, 3)
  59. })
  60. it('extend', function () {
  61. var from = {a: 1, b: 2}
  62. var to = {}
  63. var res = _.extend(to, from)
  64. expect(to.a).toBe(from.a)
  65. expect(to.b).toBe(from.b)
  66. expect(res).toBe(to)
  67. })
  68. it('isObject', function () {
  69. expect(_.isObject({})).toBe(true)
  70. expect(_.isObject([])).toBe(true)
  71. expect(_.isObject(null)).toBeFalsy()
  72. expect(_.isObject(123)).toBeFalsy()
  73. expect(_.isObject(true)).toBeFalsy()
  74. expect(_.isObject('hi')).toBeFalsy()
  75. expect(_.isObject(undefined)).toBeFalsy()
  76. expect(_.isObject(function () {})).toBeFalsy()
  77. })
  78. it('isPlainObject', function () {
  79. expect(_.isPlainObject({})).toBe(true)
  80. expect(_.isPlainObject([])).toBe(false)
  81. expect(_.isPlainObject(null)).toBe(false)
  82. expect(_.isPlainObject(null)).toBeFalsy()
  83. expect(_.isPlainObject(123)).toBeFalsy()
  84. expect(_.isPlainObject(true)).toBeFalsy()
  85. expect(_.isPlainObject('hi')).toBeFalsy()
  86. expect(_.isPlainObject(undefined)).toBeFalsy()
  87. expect(_.isPlainObject(function () {})).toBe(false)
  88. if (_.inBrowser) {
  89. expect(_.isPlainObject(window)).toBe(false)
  90. }
  91. })
  92. it('isArray', function () {
  93. expect(_.isArray([])).toBe(true)
  94. expect(_.isArray({})).toBe(false)
  95. expect(_.isArray(arguments)).toBe(false)
  96. })
  97. it('define', function () {
  98. var obj = {}
  99. _.define(obj, 'test', 123)
  100. expect(obj.test).toBe(123)
  101. var desc = Object.getOwnPropertyDescriptor(obj, 'test')
  102. expect(desc.enumerable).toBe(false)
  103. _.define(obj, 'test2', 123, true)
  104. expect(obj.test2).toBe(123)
  105. desc = Object.getOwnPropertyDescriptor(obj, 'test2')
  106. expect(desc.enumerable).toBe(true)
  107. })
  108. it('debounce', function (done) {
  109. var count = 0
  110. var fn = _.debounce(function () {
  111. count++
  112. }, 100)
  113. fn()
  114. setTimeout(fn, 10)
  115. setTimeout(fn, 20)
  116. setTimeout(function () {
  117. expect(count).toBe(0)
  118. }, 30)
  119. setTimeout(function () {
  120. expect(count).toBe(1)
  121. done()
  122. }, 200)
  123. })
  124. it('looseEqual', function () {
  125. expect(_.looseEqual(1, '1')).toBe(true)
  126. expect(_.looseEqual(null, undefined)).toBe(true)
  127. expect(_.looseEqual({a: 1}, {a: 1})).toBe(true)
  128. expect(_.looseEqual({a: 1}, {a: 2})).toBe(false)
  129. expect(_.looseEqual({}, [])).toBe(false)
  130. })
  131. })