filters.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 use the arg for the currency sign', function () {
  61. var res = filter(2134, '@')
  62. assert.strictEqual(res, '@2,134.00')
  63. })
  64. it('should return empty string on falsy values except 0', function () {
  65. var res1 = filter(false),
  66. res2 = filter(null),
  67. res3 = filter(undefined),
  68. res4 = filter(0)
  69. assert.strictEqual(res1, '')
  70. assert.strictEqual(res2, '')
  71. assert.strictEqual(res3, '')
  72. assert.strictEqual(res4, '$0.00')
  73. })
  74. })
  75. describe('key', function () {
  76. var filter = filters.key
  77. it('should return a function that only triggers when key matches', function () {
  78. var triggered = false,
  79. handler = filter(function () {
  80. triggered = true
  81. }, 'enter')
  82. handler({ keyCode: 0 })
  83. assert.notOk(triggered)
  84. handler({ keyCode: 13 })
  85. assert.ok(triggered)
  86. })
  87. it('should also work for direct keyCode', function () {
  88. var triggered = false,
  89. handler = filter(function () {
  90. triggered = true
  91. }, 13)
  92. handler({ keyCode: 0 })
  93. assert.notOk(triggered)
  94. handler({ keyCode: 13 })
  95. assert.ok(triggered)
  96. })
  97. })
  98. describe('filterBy', function () {
  99. var filter = filters.filterBy,
  100. arr = [
  101. { a: 1, b: { c: 'hello' }},
  102. { a: 1, b: 'hello'},
  103. { a: 1, b: 2 }
  104. ],
  105. vm = new Vue({
  106. data: {
  107. search: { key: 'hello', datakey: 'b.c' }
  108. }
  109. })
  110. it('should be computed', function () {
  111. assert.ok(filter.computed)
  112. })
  113. it('should recursively check for searchKey if no dataKey is provided', function () {
  114. var res = filter.call(vm, arr, 'search.key')
  115. assert.strictEqual(res.length, 2)
  116. assert.deepEqual(res, arr.slice(0, 2))
  117. })
  118. it('should check for datakey only if provided', function () {
  119. var res = filter.call(vm, arr, 'search.key', 'search.datakey')
  120. assert.strictEqual(res.length, 1)
  121. assert.strictEqual(res[0], arr[0])
  122. })
  123. it('should use literal searchKey if in single quotes', function () {
  124. var res = filter.call(vm, arr, "'hello'", "'b.c'")
  125. assert.strictEqual(res.length, 1)
  126. assert.strictEqual(res[0], arr[0])
  127. })
  128. it('should accept optional delimiter', function () {
  129. var res = filter.call(vm, arr, 'search.key', 'in', 'search.datakey')
  130. assert.strictEqual(res.length, 1)
  131. assert.strictEqual(res[0], arr[0])
  132. })
  133. it('should work with objects', function () {
  134. var obj = {
  135. a: arr[0],
  136. b: arr[1],
  137. c: arr[2]
  138. }
  139. var res = filter.call(vm, obj, "'a'", "'$key'")
  140. assert.strictEqual(res.length, 1)
  141. assert.strictEqual(res[0], arr[0])
  142. })
  143. })
  144. describe('orderBy', function () {
  145. var filter = filters.orderBy,
  146. arr = [
  147. { a: { b: 0 }, c: 'b'},
  148. { a: { b: 2 }, c: 'c'},
  149. { a: { b: 1 }, c: 'a'}
  150. ]
  151. it('should be computed', function () {
  152. assert.ok(filter.computed)
  153. })
  154. it('should sort based on sortKey', function () {
  155. var vm = new Vue({
  156. data: { sortby: 'a.b' }
  157. })
  158. var res = filter.call(vm, arr, 'sortby')
  159. assert.strictEqual(res[0].a.b, 0)
  160. assert.strictEqual(res[1].a.b, 1)
  161. assert.strictEqual(res[2].a.b, 2)
  162. })
  163. it('should sort based on sortKey and reverseKey', function () {
  164. var vm = new Vue({
  165. data: { sortby: 'a.b', reverse: true }
  166. })
  167. var res = filter.call(vm, arr, 'sortby', 'reverse')
  168. assert.strictEqual(res[0].a.b, 2)
  169. assert.strictEqual(res[1].a.b, 1)
  170. assert.strictEqual(res[2].a.b, 0)
  171. })
  172. it('should sort with literal args and special -1 syntax', function () {
  173. var res = filter.call(new Vue(), arr, "'c'", '-1')
  174. assert.strictEqual(res[0].c, 'c')
  175. assert.strictEqual(res[1].c, 'b')
  176. assert.strictEqual(res[2].c, 'a')
  177. })
  178. it('should accept negate reverse key', function () {
  179. var res = filter.call(new Vue({
  180. data: { reverse: true }
  181. }), arr, "'c'", '!reverse')
  182. assert.strictEqual(res[0].c, 'a')
  183. assert.strictEqual(res[1].c, 'b')
  184. assert.strictEqual(res[2].c, 'c')
  185. })
  186. it('should work with objects', function () {
  187. var obj = {
  188. a: arr[0],
  189. b: arr[1],
  190. c: arr[2]
  191. }
  192. var res = filter.call(new Vue(), obj, "'$key'", '-1')
  193. assert.strictEqual(res[0].c, 'a')
  194. assert.strictEqual(res[1].c, 'c')
  195. assert.strictEqual(res[2].c, 'b')
  196. })
  197. })
  198. })
  199. function assertNumberAndFalsy (filter) {
  200. it('should return a stringified number', function () {
  201. var res = filter(12345)
  202. assert.strictEqual(res, '12345')
  203. })
  204. it('should return empty string on falsy values except 0', function () {
  205. var res1 = filter(false),
  206. res2 = filter(null),
  207. res3 = filter(undefined),
  208. res4 = filter(0)
  209. assert.strictEqual(res1, '')
  210. assert.strictEqual(res2, '')
  211. assert.strictEqual(res3, '')
  212. assert.strictEqual(res4, '0')
  213. })
  214. }