directive.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. describe('Directive', function () {
  2. var Directive = require('vue/src/directive'),
  3. directives = require('vue/src/directives')
  4. var compiler = {
  5. options: {},
  6. getOption: function () {},
  7. vm: {
  8. constructor: {}
  9. }
  10. }
  11. describe('.split()', function () {
  12. it('should return array with one empty string for empty string', function () {
  13. var e = Directive.split('')
  14. assert.strictEqual(e.length, 1)
  15. assert.strictEqual(e[0], '')
  16. })
  17. it('should return array with the string if it\'s a single clause', function () {
  18. var e,
  19. test1 = 'fsef(a, b, c)',
  20. test2 = 'ffsef + "fse,fsef"',
  21. test3 = 'fsef + \'fesfsfe\'',
  22. test4 = '\"fsefsf,fsef,fsef\"',
  23. test5 = '(a, b)'
  24. e = Directive.split(test1)
  25. assert.strictEqual(e.length, 1)
  26. assert.strictEqual(e[0], test1)
  27. e = Directive.split(test2)
  28. assert.strictEqual(e.length, 1)
  29. assert.strictEqual(e[0], test2)
  30. e = Directive.split(test3)
  31. assert.strictEqual(e.length, 1)
  32. assert.strictEqual(e[0], test3)
  33. e = Directive.split(test4)
  34. assert.strictEqual(e.length, 1)
  35. assert.strictEqual(e[0], test4)
  36. e = Directive.split(test5)
  37. assert.strictEqual(e.length, 1)
  38. assert.strictEqual(e[0], test5)
  39. })
  40. it('should return split multiple clauses correctly', function () {
  41. var e,
  42. test1 = ['(fse,fggg)', 'fsf:({a:1,b:2}, [1,2,3])'],
  43. test2 = ['asf-fsef:fsf', '"efs,s(e,f)sf"'],
  44. test3 = ['\'fsef,sef\'', 'fse:fsf(a,b,c)'],
  45. test4 = ['\"fsef,fsef\"', 'sefsef\'fesfsf']
  46. e = Directive.split(test1.join(','))
  47. assert.strictEqual(e.length, 2, 'expression with {}, [] inside ()')
  48. assert.strictEqual(e[0], test1[0])
  49. assert.strictEqual(e[1], test1[1])
  50. e = Directive.split(test2.join(','))
  51. assert.strictEqual(e.length, 2, 'expression with double quotes')
  52. assert.strictEqual(e[0], test2[0])
  53. assert.strictEqual(e[1], test2[1])
  54. e = Directive.split(test3.join(','))
  55. assert.strictEqual(e.length, 2, 'expression with single quotes')
  56. assert.strictEqual(e[0], test3[0])
  57. assert.strictEqual(e[1], test3[1])
  58. e = Directive.split(test4.join(','))
  59. assert.strictEqual(e.length, 2, 'expression with escaped quotes')
  60. assert.strictEqual(e[0], test4[0])
  61. assert.strictEqual(e[1], test4[1])
  62. })
  63. })
  64. describe('.parse()', function () {
  65. it('should return undefined if directive name does not have correct prefix', function () {
  66. var d = Directive.parse('ds-test', 'abc', compiler)
  67. assert.strictEqual(d, undefined)
  68. })
  69. it('should return undefined if directive is unknown', function () {
  70. var d = Directive.parse('directive-that-does-not-exist', 'abc', compiler)
  71. assert.ok(d === undefined)
  72. })
  73. it('should return undefined if the expression is invalid', function () {
  74. var e = Directive.parse('text', ' ', compiler),
  75. f = Directive.parse('text', '|', compiler),
  76. g = Directive.parse('text', ' | ', compiler)
  77. assert.strictEqual(e, undefined, 'spaces')
  78. assert.strictEqual(f, undefined, 'single pipe')
  79. assert.strictEqual(g, undefined, 'pipe with spaces')
  80. })
  81. it('should return a simple Directive if expression is empty', function () {
  82. var d = Directive.parse('text', '', compiler)
  83. assert.ok(d instanceof Directive)
  84. assert.ok(d.isEmpty)
  85. })
  86. it('should return an instance of Directive if args are good', function () {
  87. var d = Directive.parse('text', 'abc', compiler)
  88. assert.ok(d instanceof Directive)
  89. })
  90. })
  91. describe('instantiation', function () {
  92. it('should copy the definition as _update if the def is a function', function () {
  93. var testDir = function () {}
  94. directives.test = testDir
  95. var d = Directive.parse('test', 'abc', compiler)
  96. assert.strictEqual(d._update, testDir)
  97. })
  98. it('should copy methods if the def is an object', function () {
  99. var obj = {
  100. bind: function () {},
  101. update: function () {},
  102. unbind: function () {},
  103. custom: function () {}
  104. }
  105. directives.obj = obj
  106. var d = Directive.parse('obj', 'abc', compiler)
  107. assert.strictEqual(d._update, obj.update, 'update should be copied as _update')
  108. assert.strictEqual(d._unbind, obj.unbind, 'unbind should be copied as _unbind')
  109. assert.strictEqual(d.bind, obj.bind)
  110. assert.strictEqual(d.custom, obj.custom, 'should copy any custom methods')
  111. })
  112. it('should trim the expression', function () {
  113. var exp = ' fsfsef | fsef a ',
  114. d = Directive.parse('text', exp, compiler)
  115. assert.strictEqual(d.expression, exp.trim())
  116. })
  117. it('should extract correct key', function () {
  118. var d = Directive.parse('text', '"fsefse | fsefsef" && bc', compiler),
  119. e = Directive.parse('text', '"fsefsf & fsefs" | test', compiler),
  120. f = Directive.parse('text', '"fsef:fsefsf" || ff', compiler)
  121. assert.strictEqual(d.key, '"fsefse | fsefsef" && bc', 'pipe inside quotes and &&')
  122. assert.strictEqual(e.key, '"fsefsf & fsefs"', '& inside quotes with filter')
  123. assert.strictEqual(f.key, '"fsef:fsefsf" || ff', ': inside quotes and ||')
  124. })
  125. it('should extract correct argument', function () {
  126. var d = Directive.parse('text', 'todo:todos', compiler),
  127. e = Directive.parse('text', '$todo:todos + abc', compiler),
  128. f = Directive.parse('text', '-todo-fsef:todos | fsf fsef', compiler)
  129. assert.strictEqual(d.arg, 'todo', 'simple')
  130. assert.strictEqual(e.arg, '$todo', 'expression')
  131. assert.strictEqual(f.arg, '-todo-fsef', 'with hyphens and filters')
  132. })
  133. it('should be able to determine whether the key is an expression', function () {
  134. var d = Directive.parse('text', 'abc', compiler),
  135. e = Directive.parse('text', '!abc', compiler),
  136. f = Directive.parse('text', 'abc + bcd * 5 / 2', compiler),
  137. g = Directive.parse('text', 'abc && (bcd || eee)', compiler),
  138. h = Directive.parse('text', 'test(abc)', compiler),
  139. i = Directive.parse('text', 'a.b', compiler),
  140. j = Directive.parse('text', 'a.$b', compiler)
  141. assert.ok(!d.isExp, 'non-expression')
  142. assert.ok(e.isExp, 'negation')
  143. assert.ok(f.isExp, 'math')
  144. assert.ok(g.isExp, 'logic')
  145. assert.ok(h.isExp, 'function invocation')
  146. assert.ok(!i.isExp, 'dot syntax')
  147. assert.ok(!j.isExp, 'dot syntax with $')
  148. })
  149. it('should have a filter prop of null if no filters are present', function () {
  150. var d = Directive.parse('text', 'abc', compiler),
  151. e = Directive.parse('text', 'abc |', compiler),
  152. f = Directive.parse('text', 'abc ||', compiler),
  153. g = Directive.parse('text', 'abc | | ', compiler),
  154. h = Directive.parse('text', 'abc | unknown | nothing at all | whaaat', compiler)
  155. assert.strictEqual(d.filters, null)
  156. assert.strictEqual(e.filters, null, 'single')
  157. assert.strictEqual(f.filters, null, 'double')
  158. assert.strictEqual(g.filters, null, 'with spaces')
  159. assert.strictEqual(h.filters, null, 'with unknown filters')
  160. })
  161. it('should extract correct filters (single filter)', function () {
  162. var d = Directive.parse('text', 'abc || a + "b|c" | uppercase', compiler),
  163. f = d.filters[0]
  164. assert.strictEqual(f.name, 'uppercase')
  165. assert.strictEqual(f.args, null)
  166. assert.strictEqual(f.apply('test'), 'TEST')
  167. })
  168. it('should extract correct filters (single filter with args)', function () {
  169. var d = Directive.parse('text', 'abc + \'b | c | d\' | pluralize item \'arg with spaces\'', compiler),
  170. f = d.filters[0]
  171. assert.strictEqual(f.name, 'pluralize', 'name')
  172. assert.strictEqual(f.args.length, 2, 'args length')
  173. assert.strictEqual(f.args[0], 'item', 'args value 1')
  174. assert.strictEqual(f.args[1], 'arg with spaces', 'args value 2')
  175. })
  176. it('should extract correct filters (multiple filters)', function () {
  177. // intentional double pipe
  178. var d = Directive.parse('text', 'abc | uppercase | pluralize item || lowercase', compiler),
  179. f1 = d.filters[0],
  180. f2 = d.filters[1],
  181. f3 = d.filters[2]
  182. assert.strictEqual(d.filters.length, 3)
  183. assert.strictEqual(f1.name, 'uppercase')
  184. assert.strictEqual(f2.name, 'pluralize')
  185. assert.strictEqual(f2.args[0], 'item')
  186. assert.strictEqual(f3.name, 'lowercase')
  187. })
  188. })
  189. describe('.applyFilters()', function () {
  190. it('should work', function () {
  191. var d = Directive.parse('text', 'abc | pluralize item | capitalize', compiler),
  192. v = d.applyFilters(2)
  193. assert.strictEqual(v, 'Items')
  194. })
  195. })
  196. describe('.update()', function () {
  197. var d = Directive.parse('text', 'abc', compiler),
  198. updated = false
  199. d._update = function () {
  200. updated = true
  201. }
  202. it('should call _update() for first time update, even with undefined', function () {
  203. d.update(undefined, true)
  204. assert.strictEqual(updated, true)
  205. })
  206. it('should _update() when a different value is given', function () {
  207. updated = false
  208. d.update(123)
  209. assert.strictEqual(d.value, 123)
  210. assert.strictEqual(updated, true)
  211. })
  212. it('should not _update() if the value is the same', function () {
  213. updated = false
  214. d.update(123)
  215. assert.ok(!updated)
  216. })
  217. it('should call applyFilter() is there are filters', function () {
  218. var filterApplied = false
  219. d.filters = []
  220. d.applyFilters = function () {
  221. filterApplied = true
  222. }
  223. d.update(234)
  224. assert.ok(filterApplied)
  225. })
  226. })
  227. describe('.unbind()', function () {
  228. var d = Directive.parse('text', 'abc', compiler),
  229. unbound = false,
  230. val
  231. d._unbind = function (v) {
  232. val = v
  233. unbound = true
  234. }
  235. it('should not work if it has no element yet', function () {
  236. d.unbind()
  237. assert.strictEqual(unbound, false)
  238. })
  239. it('should call _unbind() and null everything if it has an element', function () {
  240. d.el = true
  241. d.unbind()
  242. assert.strictEqual(unbound, true)
  243. assert.ok(d.el === null && d.vm === null && d.binding === null && d.compiler === null)
  244. })
  245. it('should not execute if called more than once', function () {
  246. unbound = false
  247. d.unbind()
  248. assert.notOk(unbound)
  249. })
  250. })
  251. describe('simple directive', function () {
  252. it('should copy as bind() if the def is a function', function () {
  253. var called = 0
  254. function call () {
  255. called++
  256. }
  257. Vue.directive('simple-dir-test1', call)
  258. var d = Directive.parse('simple-dir-test1', '', compiler)
  259. d.bind()
  260. assert.strictEqual(called, 1)
  261. })
  262. it('should copy/delegate bind and unbind if the def is an object', function () {
  263. var called = 0
  264. function call () {
  265. called++
  266. }
  267. Vue.directive('simple-dir-test2', {
  268. bind: call,
  269. unbind: call
  270. })
  271. var d = Directive.parse('simple-dir-test2', '', compiler, true)
  272. d.bind()
  273. d.unbind()
  274. assert.strictEqual(called, 2)
  275. })
  276. })
  277. })