2
0

filters.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. describe('Filters', function () {
  2. var filters = require('vue/src/filters')
  3. describe('capitalize', function () {
  4. var filter = filters.capitalize
  5. it('should capitalize a string', function () {
  6. var res = filter('fsefsfsef')
  7. assert.strictEqual(res.charAt(0), 'F')
  8. assert.strictEqual(res.slice(1), 'sefsfsef')
  9. })
  10. assertNumberAndFalsy(filter)
  11. })
  12. describe('uppercase', function () {
  13. var filter = filters.uppercase
  14. it('should uppercase a string', function () {
  15. var res = filter('fsefef')
  16. assert.strictEqual(res, 'FSEFEF')
  17. })
  18. assertNumberAndFalsy(filter)
  19. })
  20. describe('lowercase', function () {
  21. var filter = filters.lowercase
  22. it('should lowercase a string', function () {
  23. var res = filter('AweSoMe')
  24. assert.strictEqual(res, 'awesome')
  25. })
  26. assertNumberAndFalsy(filter)
  27. })
  28. describe('pluralize', function () {
  29. var filter = filters.pluralize
  30. it('should simply add "s" if arg length is 1', function () {
  31. var arg = 'item',
  32. res0 = filter(0, arg),
  33. res1 = filter(1, arg),
  34. res2 = filter(2, arg)
  35. assert.strictEqual(res0, 'items')
  36. assert.strictEqual(res1, 'item')
  37. assert.strictEqual(res2, 'items')
  38. })
  39. it('should use corresponding format when arg length is greater than 1', function () {
  40. var res0 = filter(0, 'st', 'nd', 'rd'),
  41. res1 = filter(1, 'st', 'nd', 'rd'),
  42. res2 = filter(2, 'st', 'nd', 'rd'),
  43. res3 = filter(3, 'st', 'nd', 'rd')
  44. assert.strictEqual(res0, 'rd')
  45. assert.strictEqual(res1, 'st')
  46. assert.strictEqual(res2, 'nd')
  47. assert.strictEqual(res3, 'rd')
  48. })
  49. })
  50. describe('currency', function () {
  51. var filter = filters.currency
  52. it('should format a number correctly', function () {
  53. var res1 = filter(1234),
  54. res2 = filter(1234.45),
  55. res3 = filter(123443434.4343434)
  56. assert.strictEqual(res1, '$1,234.00')
  57. assert.strictEqual(res2, '$1,234.45')
  58. assert.strictEqual(res3, '$123,443,434.43')
  59. })
  60. it('should format a negative number correctly', function () {
  61. var res1 = filter(-50),
  62. res2 = filter(-150.43),
  63. res3 = filter(-1500.4343434)
  64. assert.strictEqual(res1, '-$50.00')
  65. assert.strictEqual(res2, '-$150.43')
  66. assert.strictEqual(res3, '-$1,500.43')
  67. })
  68. it('should use the arg for the currency sign', function () {
  69. var res = filter(2134, '@')
  70. assert.strictEqual(res, '@2,134.00')
  71. })
  72. it('should return empty string on falsy values except 0', function () {
  73. var res1 = filter(false),
  74. res2 = filter(null),
  75. res3 = filter(undefined),
  76. res4 = filter(0)
  77. assert.strictEqual(res1, '')
  78. assert.strictEqual(res2, '')
  79. assert.strictEqual(res3, '')
  80. assert.strictEqual(res4, '$0.00')
  81. })
  82. it('should cast strings into float', function () {
  83. var res1 = filter('fesf'),
  84. res2 = filter('0.24')
  85. assert.strictEqual(res1, '')
  86. assert.strictEqual(res2, '$0.24')
  87. })
  88. })
  89. describe('key', function () {
  90. var filter = filters.key
  91. it('should return a function that only triggers when key matches', function () {
  92. var triggered = false,
  93. handler = filter(function () {
  94. triggered = true
  95. }, 'enter')
  96. handler({ keyCode: 0 })
  97. assert.notOk(triggered)
  98. handler({ keyCode: 13 })
  99. assert.ok(triggered)
  100. })
  101. it('should also work for direct keyCode', function () {
  102. var triggered = false,
  103. handler = filter(function () {
  104. triggered = true
  105. }, 13)
  106. handler({ keyCode: 0 })
  107. assert.notOk(triggered)
  108. handler({ keyCode: 13 })
  109. assert.ok(triggered)
  110. })
  111. })
  112. describe('filterBy', function () {
  113. var filter = filters.filterBy,
  114. arr = [
  115. { a: 1, b: { c: 'hello' }},
  116. { a: 1, b: 'hello'},
  117. { a: 1, b: 2 }
  118. ],
  119. vm = new Vue({
  120. data: {
  121. search: { key: 'hello', datakey: 'b.c' }
  122. }
  123. })
  124. it('should be computed', function () {
  125. assert.ok(filter.computed)
  126. })
  127. it('should recursively check for searchKey if no dataKey is provided', function () {
  128. var res = filter.call(vm, arr, 'search.key')
  129. assert.strictEqual(res.length, 2)
  130. assert.deepEqual(res, arr.slice(0, 2))
  131. })
  132. it('should check for datakey only if provided', function () {
  133. var res = filter.call(vm, arr, 'search.key', 'search.datakey')
  134. assert.strictEqual(res.length, 1)
  135. assert.strictEqual(res[0], arr[0])
  136. })
  137. it('should use literal searchKey if in single quotes', function () {
  138. var res = filter.call(vm, arr, "'hello'", "'b.c'")
  139. assert.strictEqual(res.length, 1)
  140. assert.strictEqual(res[0], arr[0])
  141. })
  142. it('should accept optional delimiter', function () {
  143. var res = filter.call(vm, arr, 'search.key', 'in', 'search.datakey')
  144. assert.strictEqual(res.length, 1)
  145. assert.strictEqual(res[0], arr[0])
  146. })
  147. it('should work with objects', function () {
  148. var obj = {
  149. a: arr[0],
  150. b: arr[1],
  151. c: arr[2]
  152. }
  153. var res = filter.call(vm, obj, "'a'", "'$key'")
  154. assert.strictEqual(res.length, 1)
  155. assert.strictEqual(res[0], arr[0])
  156. })
  157. })
  158. describe('orderBy', function () {
  159. var filter = filters.orderBy,
  160. arr = [
  161. { a: { b: 0 }, c: 'b'},
  162. { a: { b: 2 }, c: 'c'},
  163. { a: { b: 1 }, c: 'a'}
  164. ]
  165. it('should be computed', function () {
  166. assert.ok(filter.computed)
  167. })
  168. it('should sort based on sortKey', function () {
  169. var vm = new Vue({
  170. data: { sortby: 'a.b' }
  171. })
  172. var res = filter.call(vm, arr, 'sortby')
  173. assert.strictEqual(res[0].a.b, 0)
  174. assert.strictEqual(res[1].a.b, 1)
  175. assert.strictEqual(res[2].a.b, 2)
  176. })
  177. it('should sort based on sortKey and reverseKey', function () {
  178. var vm = new Vue({
  179. data: { sortby: 'a.b', reverse: true }
  180. })
  181. var res = filter.call(vm, arr, 'sortby', 'reverse')
  182. assert.strictEqual(res[0].a.b, 2)
  183. assert.strictEqual(res[1].a.b, 1)
  184. assert.strictEqual(res[2].a.b, 0)
  185. })
  186. it('should sort with literal args and special -1 syntax', function () {
  187. var res = filter.call(new Vue(), arr, "'c'", '-1')
  188. assert.strictEqual(res[0].c, 'c')
  189. assert.strictEqual(res[1].c, 'b')
  190. assert.strictEqual(res[2].c, 'a')
  191. })
  192. it('should accept negate reverse key', function () {
  193. var res = filter.call(new Vue({
  194. data: { reverse: true }
  195. }), arr, "'c'", '!reverse')
  196. assert.strictEqual(res[0].c, 'a')
  197. assert.strictEqual(res[1].c, 'b')
  198. assert.strictEqual(res[2].c, 'c')
  199. })
  200. it('should work with objects', function () {
  201. var obj = {
  202. a: arr[0],
  203. b: arr[1],
  204. c: arr[2]
  205. }
  206. var res = filter.call(new Vue(), obj, "'$key'", '-1')
  207. assert.strictEqual(res[0].c, 'a')
  208. assert.strictEqual(res[1].c, 'c')
  209. assert.strictEqual(res[2].c, 'b')
  210. })
  211. })
  212. })
  213. function assertNumberAndFalsy (filter) {
  214. it('should return a stringified number', function () {
  215. var res = filter(12345)
  216. assert.strictEqual(res, '12345')
  217. })
  218. it('should return empty string on falsy values except 0', function () {
  219. var res1 = filter(false),
  220. res2 = filter(null),
  221. res3 = filter(undefined),
  222. res4 = filter(0)
  223. assert.strictEqual(res1, '')
  224. assert.strictEqual(res2, '')
  225. assert.strictEqual(res3, '')
  226. assert.strictEqual(res4, '0')
  227. })
  228. }