util-spec.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import {
  2. asHalfPixel,
  3. ceil10,
  4. diffDomain,
  5. getOption,
  6. hasValue,
  7. isArray,
  8. isDefined,
  9. isEmpty,
  10. isFunction,
  11. isString,
  12. isUndefined,
  13. isValue,
  14. notEmpty,
  15. sanitise,
  16. isNumber,
  17. flattenArray,
  18. getIEVersion,
  19. isIE
  20. } from '../src/util'
  21. describe('util.js tests', function() {
  22. 'use strict'
  23. var undefined_var
  24. var html_str = '<div>Hello there</div>'
  25. var html_entities_str = '&lt;div&gt;Hello there&lt;/div&gt;'
  26. var empty_string = ''
  27. var nonempty_string = 'hello there'
  28. var nonempty_number = 1234.2
  29. var zero_number = 0
  30. var null_var = null
  31. var empty_object = {}
  32. var nonempty_object = {
  33. a: 1
  34. }
  35. var empty_array = []
  36. var nonempty_array = [1, 3]
  37. var nonempty_function = function(a) {
  38. return a
  39. }
  40. describe('asHalfPixel, ceil10 and diffDomain functions', function() {
  41. it('asHalfPixel should return correct value', function() {
  42. expect(asHalfPixel(nonempty_number)).toBe(1235.5)
  43. })
  44. it('ceil10 should return correct value', function() {
  45. expect(ceil10(nonempty_number)).toBe(1240)
  46. })
  47. it('diffDomain should return correct value', function() {
  48. expect(diffDomain(nonempty_array)).toBe(2)
  49. })
  50. })
  51. describe('getOption and hasValue functions', function() {
  52. it('getOption should return value if options dict has specified key', function() {
  53. expect(getOption(nonempty_object, 'a', 'b')).toBe(1)
  54. })
  55. it('getOption should return default value if options dict lacks specified key', function() {
  56. expect(getOption(empty_object, 'a', 'b')).toBe('b')
  57. })
  58. it('hasValue should return true if dict has requested value', function() {
  59. expect(hasValue(nonempty_object, 1)).toBe(true)
  60. })
  61. it('hasValue should return false if dict lacks requested value', function() {
  62. expect(hasValue(nonempty_object, 2)).toBe(false)
  63. })
  64. })
  65. describe('sanitise function', function() {
  66. it('should replace < and > tags', function() {
  67. expect(sanitise(html_str)).toBe(html_entities_str)
  68. })
  69. it('should not modify a string not containing < and > tags', function() {
  70. expect(sanitise(html_entities_str)).toBe(html_entities_str)
  71. })
  72. it('should not modify an imput whose type is not string', function() {
  73. expect(sanitise(nonempty_number)).toBe(nonempty_number)
  74. })
  75. })
  76. describe('flattenArray', function() {
  77. it('returns empty array for undefined value', function() {
  78. expect(flattenArray(undefined_var)).toEqual([])
  79. })
  80. it('returns flattened arrays (1 array)', function() {
  81. expect(flattenArray([[1]])).toEqual([1])
  82. })
  83. it('returns flatten arrays (n arrays)', function() {
  84. expect(flattenArray([[1], ['2']])).toEqual([1, '2'])
  85. })
  86. })
  87. describe('isArray, isDefined, isEmpty, isFunction, isNumber, isString, isUndefined, isValue and notEmpty functions', function() {
  88. it('isArray should return true for array var', function() {
  89. expect(isArray(nonempty_array)).toBe(true)
  90. })
  91. it('isDefined should return true for defined var', function() {
  92. expect(isDefined(nonempty_string)).toBe(true)
  93. })
  94. it('isFunction should return true for function var', function() {
  95. expect(isFunction(nonempty_function)).toBe(true)
  96. })
  97. it('isString should return true for string var', function() {
  98. expect(isString(nonempty_string)).toBe(true)
  99. })
  100. it('isUndefined should return true for undefined var', function() {
  101. expect(isUndefined(undefined_var)).toBe(true)
  102. })
  103. it('isValue should return true for value var', function() {
  104. expect(isValue(nonempty_number)).toBe(1234.2)
  105. })
  106. it('isArray should return false for non array var', function() {
  107. expect(isArray(nonempty_string)).toBe(false)
  108. })
  109. it('isDefined should return false for non defined var', function() {
  110. expect(isDefined(undefined_var)).toBe(false)
  111. })
  112. it('isFunction should return false for non function var', function() {
  113. expect(isFunction(nonempty_string)).toBe(false)
  114. })
  115. it('isString should return false for non string var', function() {
  116. expect(isString(undefined_var)).toBe(false)
  117. })
  118. it('isUndefined should return false for non undefined var', function() {
  119. expect(isUndefined(nonempty_string)).toBe(false)
  120. })
  121. it('isValue should return false for null var', function() {
  122. expect(isValue(null_var)).toBe(false)
  123. })
  124. it('isNumber should return false for undefined var', function() {
  125. expect(isNumber(undefined_var)).toBe(false)
  126. })
  127. it('isNumber should return false for null var', function() {
  128. expect(isNumber(null_var)).toBe(false)
  129. })
  130. it('isNumber should return false for string var', function() {
  131. expect(isNumber(nonempty_string)).toBe(false)
  132. })
  133. it('isNumber should return true for nonempty_number', function() {
  134. expect(isNumber(nonempty_number)).toBe(true)
  135. })
  136. it('isNumber should return true for zero_number', function() {
  137. expect(isNumber(zero_number)).toBe(true)
  138. })
  139. it('isEmpty should return false for nonempty_array', function() {
  140. expect(isEmpty(nonempty_array)).toBe(false)
  141. })
  142. it('isEmpty should return false for nonempty_number', function() {
  143. expect(isEmpty(nonempty_number)).toBe(false)
  144. })
  145. it('isEmpty should return false for nonempty_object', function() {
  146. expect(isEmpty(nonempty_object)).toBe(false)
  147. })
  148. it('isEmpty should return false for nonempty_string', function() {
  149. expect(isEmpty(nonempty_string)).toBe(false)
  150. })
  151. it('isEmpty should return true for empty_array', function() {
  152. expect(isEmpty(empty_array)).toBe(true)
  153. })
  154. it('isEmpty should return true for empty_object', function() {
  155. expect(isEmpty(empty_object)).toBe(true)
  156. })
  157. it('isEmpty should return true for empty_string', function() {
  158. expect(isEmpty(empty_string)).toBe(true)
  159. })
  160. it('isEmpty should return true for null_var', function() {
  161. expect(isEmpty(null_var)).toBe(true)
  162. })
  163. it('isEmpty should return true for undefined_var', function() {
  164. expect(isEmpty(undefined_var)).toBe(true)
  165. })
  166. it('notEmpty should return false for empty_array', function() {
  167. expect(notEmpty(empty_array)).toBe(false)
  168. })
  169. it('notEmpty should return false for empty_object', function() {
  170. expect(notEmpty(empty_object)).toBe(false)
  171. })
  172. it('notEmpty should return false for empty_string', function() {
  173. expect(notEmpty(empty_string)).toBe(false)
  174. })
  175. it('notEmpty should return false for null_var', function() {
  176. expect(notEmpty(null_var)).toBe(false)
  177. })
  178. it('notEmpty should return false for undefined_var', function() {
  179. expect(notEmpty(undefined_var)).toBe(false)
  180. })
  181. it('notEmpty should return true for nonempty_array', function() {
  182. expect(notEmpty(nonempty_array)).toBe(true)
  183. })
  184. it('notEmpty should return true for nonempty_number', function() {
  185. expect(notEmpty(nonempty_number)).toBe(true)
  186. })
  187. it('notEmpty should return true for nonempty_object', function() {
  188. expect(notEmpty(nonempty_object)).toBe(true)
  189. })
  190. it('notEmpty should return true for nonempty_string', function() {
  191. expect(notEmpty(nonempty_string)).toBe(true)
  192. })
  193. })
  194. describe('getIEVersion and isIE', function() {
  195. it('getIEVersion should return 10 for user agent string "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)"', function() {
  196. expect(
  197. getIEVersion(
  198. 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'
  199. )
  200. ).toBe(10)
  201. })
  202. it('getIEVersion should return 11 for user agent string "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"', function() {
  203. expect(
  204. getIEVersion(
  205. 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'
  206. )
  207. ).toBe(11)
  208. })
  209. it('getIEVersion should return false for user agent string "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0"', function() {
  210. expect(
  211. getIEVersion(
  212. 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0'
  213. )
  214. ).toBe(false)
  215. })
  216. it('getIEVersion should return a number or false if parameter "agent" is not used', function() {
  217. expect(getIEVersion()).toMatch(/(\d+|false)/)
  218. })
  219. it('isIE should return false for version number 6', function() {
  220. expect(isIE(6)).toBe(false)
  221. })
  222. it('isIE should return true or false if parameter "version" is not used', function() {
  223. expect(isIE()).toMatch(/(true|false)/)
  224. })
  225. })
  226. })