directive.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 (type, id) {
  7. return Vue.options[type][id]
  8. },
  9. vm: {
  10. constructor: {}
  11. }
  12. }
  13. function makeSpy () {
  14. var spy = function () {
  15. spy.called++
  16. spy.args = [].slice.call(arguments)
  17. }
  18. spy.called = 0
  19. return spy
  20. }
  21. function build (name, exp, compiler) {
  22. var ast = Directive.parse(exp)[0]
  23. return new Directive(name, ast, directives[name], compiler, {})
  24. }
  25. describe('.parse()', function () {
  26. it('key', function () {
  27. var res = Directive.parse('key')
  28. assert.equal(res.length , 1)
  29. assert.equal(res[0].key, 'key')
  30. assert.equal(res[0].expression, 'key')
  31. })
  32. it('arg:key', function () {
  33. var res = Directive.parse('arg:key')
  34. assert.equal(res.length , 1)
  35. assert.equal(res[0].key, 'key')
  36. assert.equal(res[0].arg, 'arg')
  37. assert.equal(res[0].expression, 'arg:key')
  38. })
  39. it('arg : key | abc', function () {
  40. var res = Directive.parse(' arg : key | abc de')
  41. assert.equal(res.length , 1)
  42. assert.equal(res[0].key, 'key')
  43. assert.equal(res[0].arg, 'arg')
  44. assert.equal(res[0].expression, 'arg : key | abc de')
  45. assert.equal(res[0].filters.length, 1)
  46. assert.equal(res[0].filters[0].name, 'abc')
  47. assert.equal(res[0].filters[0].args.length, 1)
  48. assert.equal(res[0].filters[0].args[0], 'de')
  49. })
  50. it('a || b | c', function () {
  51. var res = Directive.parse('a || b | c')
  52. assert.equal(res.length, 1)
  53. assert.equal(res[0].key, 'a || b')
  54. assert.equal(res[0].expression, 'a || b | c')
  55. assert.equal(res[0].filters.length, 1)
  56. assert.equal(res[0].filters[0].name, 'c')
  57. assert.notOk(res[0].filters[0].args)
  58. })
  59. it('a ? b : c', function () {
  60. var res = Directive.parse('a ? b : c')
  61. assert.equal(res.length, 1)
  62. assert.equal(res[0].key, 'a ? b : c')
  63. assert.notOk(res[0].filters)
  64. })
  65. it('"a:b:c||d|e|f" || d ? a : b', function () {
  66. var res = Directive.parse('"a:b:c||d|e|f" || d ? a : b')
  67. assert.equal(res.length, 1)
  68. assert.equal(res[0].key, '"a:b:c||d|e|f" || d ? a : b')
  69. assert.notOk(res[0].filters)
  70. assert.notOk(res[0].arg)
  71. })
  72. it('a, b, c', function () {
  73. var res = Directive.parse('a, b, c')
  74. assert.equal(res.length, 3)
  75. assert.equal(res[0].key, 'a')
  76. assert.equal(res[1].key, 'b')
  77. assert.equal(res[2].key, 'c')
  78. })
  79. it('a:b | c, d:e | f, g:h | i', function () {
  80. var res = Directive.parse('a:b | c, d:e | f, g:h | i')
  81. assert.equal(res.length, 3)
  82. assert.equal(res[0].arg, 'a')
  83. assert.equal(res[0].key, 'b')
  84. assert.equal(res[0].filters.length, 1)
  85. assert.equal(res[0].filters[0].name, 'c')
  86. assert.notOk(res[0].filters[0].args)
  87. assert.equal(res[1].arg, 'd')
  88. assert.equal(res[1].key, 'e')
  89. assert.equal(res[1].filters.length, 1)
  90. assert.equal(res[1].filters[0].name, 'f')
  91. assert.notOk(res[1].filters[0].args)
  92. assert.equal(res[2].arg, 'g')
  93. assert.equal(res[2].key, 'h')
  94. assert.equal(res[2].filters.length, 1)
  95. assert.equal(res[2].filters[0].name, 'i')
  96. assert.notOk(res[2].filters[0].args)
  97. })
  98. it('click:test(c.indexOf(d,f),"e,f"), input: d || [e,f], ok:{a:1,b:2}', function () {
  99. var res = Directive.parse('click:test(c.indexOf(d,f),"e,f"), input: d || [e,f], ok:{a:1,b:2}')
  100. assert.equal(res.length, 3)
  101. assert.equal(res[0].arg, 'click')
  102. assert.equal(res[0].key, 'test(c.indexOf(d,f),"e,f")')
  103. assert.equal(res[1].arg, 'input')
  104. assert.equal(res[1].key, 'd || [e,f]')
  105. assert.notOk(res[1].filters)
  106. assert.equal(res[2].arg, 'ok')
  107. assert.equal(res[2].key, '{a:1,b:2}')
  108. })
  109. })
  110. describe('.inlineFilters()', function () {
  111. it('should inline computed filters', function () {
  112. var filters = Directive.parse('a | a | b | c d')[0].filters
  113. var exp = Directive.inlineFilters('this.a + this.b', filters)
  114. var mock = new Vue({
  115. filters: {
  116. a: function (v) {
  117. return v + 'a'
  118. },
  119. b: function (v) {
  120. return v + 'b'
  121. },
  122. c: function (v, d) {
  123. return v + 'c' + d
  124. }
  125. },
  126. data: {
  127. a: 'a',
  128. b: 'b'
  129. }
  130. })
  131. var getter = new Function('return ' + exp)
  132. var res = getter.call(mock)
  133. assert.strictEqual(res, 'ababcd')
  134. })
  135. })
  136. describe('instantiation', function () {
  137. it('should copy the definition as update if the def is a function', function () {
  138. var testDir = function () {}
  139. directives.test = testDir
  140. var d = build('test', 'abc', compiler)
  141. assert.strictEqual(d.update, testDir)
  142. })
  143. it('should copy methods if the def is an object', function () {
  144. var obj = {
  145. bind: function () {},
  146. update: function () {},
  147. unbind: function () {},
  148. custom: function () {}
  149. }
  150. directives.obj = obj
  151. var d = build('obj', 'abc', compiler)
  152. assert.strictEqual(d.update, obj.update)
  153. assert.strictEqual(d.unbind, obj.unbind)
  154. assert.strictEqual(d.bind, obj.bind)
  155. assert.strictEqual(d.custom, obj.custom, 'should copy any custom methods')
  156. })
  157. it('should trim the expression', function () {
  158. var exp = ' fsfsef | fsef a ',
  159. d = build('text', exp, compiler)
  160. assert.strictEqual(d.expression, exp.trim())
  161. })
  162. it('should extract correct key', function () {
  163. var d = build('text', '"fsefse | fsefsef" && bc', compiler),
  164. e = build('text', '"fsefsf & fsefs" | test', compiler),
  165. f = build('text', '"fsef:fsefsf" || ff', compiler)
  166. assert.strictEqual(d.key, '"fsefse | fsefsef" && bc', 'pipe inside quotes and &&')
  167. assert.strictEqual(e.key, '"fsefsf & fsefs"', '& inside quotes with filter')
  168. assert.strictEqual(f.key, '"fsef:fsefsf" || ff', ': inside quotes and ||')
  169. })
  170. it('should extract correct argument', function () {
  171. var d = build('text', 'todo:todos', compiler),
  172. e = build('text', '$todo:todos + abc', compiler),
  173. f = build('text', '-todo-fsef:todos | fsf fsef', compiler)
  174. assert.strictEqual(d.arg, 'todo', 'simple')
  175. assert.strictEqual(e.arg, '$todo', 'expression')
  176. assert.strictEqual(f.arg, '-todo-fsef', 'with hyphens and filters')
  177. })
  178. it('should be able to determine whether the key is an expression', function () {
  179. var d = build('text', 'abc', compiler),
  180. e = build('text', '!abc', compiler),
  181. f = build('text', 'abc + bcd * 5 / 2', compiler),
  182. g = build('text', 'abc && (bcd || eee)', compiler),
  183. h = build('text', 'test(abc)', compiler),
  184. i = build('text', 'a.b', compiler),
  185. j = build('text', 'a.$b', compiler)
  186. assert.ok(!d.isExp, 'non-expression')
  187. assert.ok(e.isExp, 'negation')
  188. assert.ok(f.isExp, 'math')
  189. assert.ok(g.isExp, 'logic')
  190. assert.ok(h.isExp, 'function invocation')
  191. assert.ok(!i.isExp, 'dot syntax')
  192. assert.ok(!j.isExp, 'dot syntax with $')
  193. })
  194. it('should have a filter prop of null if no filters are present', function () {
  195. var d = build('text', 'abc', compiler),
  196. e = build('text', 'abc |', compiler),
  197. f = build('text', 'abc ||', compiler),
  198. g = build('text', 'abc | | ', compiler),
  199. h = build('text', 'abc | unknown | nothing at all | whaaat', compiler)
  200. assert.strictEqual(d.filters, null)
  201. assert.strictEqual(e.filters, null, 'single')
  202. assert.strictEqual(f.filters, null, 'double')
  203. assert.strictEqual(g.filters, null, 'with spaces')
  204. assert.strictEqual(h.filters, null, 'with unknown filters')
  205. })
  206. it('should extract correct filters (single filter)', function () {
  207. var d = build('text', 'abc || a + "b|c" | uppercase', compiler),
  208. f = d.filters[0]
  209. assert.strictEqual(f.name, 'uppercase')
  210. assert.strictEqual(f.args, null)
  211. assert.strictEqual(f.apply('test'), 'TEST')
  212. })
  213. it('should extract correct filters (single filter with args)', function () {
  214. var d = build('text', 'abc + \'b | c | d\' | pluralize item \'arg with spaces\'', compiler),
  215. f = d.filters[0]
  216. assert.strictEqual(f.name, 'pluralize', 'name')
  217. assert.strictEqual(f.args.length, 2, 'args length')
  218. assert.strictEqual(f.args[0], 'item', 'args value 1')
  219. assert.strictEqual(f.args[1], '\'arg with spaces\'', 'args value 2')
  220. })
  221. it('should extract correct filters (multiple filters)', function () {
  222. // intentional double pipe
  223. var d = build('text', 'abc | uppercase "a,b,c" | pluralize item | lowercase', compiler),
  224. f1 = d.filters[0],
  225. f2 = d.filters[1],
  226. f3 = d.filters[2]
  227. assert.strictEqual(d.filters.length, 3)
  228. assert.strictEqual(f1.name, 'uppercase')
  229. assert.strictEqual(f1.args[0], '"a,b,c"')
  230. assert.strictEqual(f2.name, 'pluralize')
  231. assert.strictEqual(f2.args[0], 'item')
  232. assert.strictEqual(f3.name, 'lowercase')
  233. })
  234. it('should compile the key if the key contains interpolation tags', function () {
  235. compiler.eval = makeSpy()
  236. build('model', '{{abc}} | uppercase', compiler)
  237. assert.ok(compiler.eval.called)
  238. assert.equal(compiler.eval.args[0], '{{abc}}')
  239. })
  240. })
  241. describe('.$applyFilters()', function () {
  242. it('should work', function () {
  243. var d = build('text', 'abc | pluralize item | capitalize', compiler),
  244. v = d.$applyFilters(2)
  245. assert.strictEqual(v, 'Items')
  246. })
  247. })
  248. describe('.$update()', function () {
  249. var d = build('text', 'abc', compiler),
  250. updated = false
  251. d.update = function () {
  252. updated = true
  253. }
  254. it('should call user update() for first time update, even with undefined', function () {
  255. d.$update(undefined, true)
  256. assert.strictEqual(updated, true)
  257. })
  258. it('should user update() when a different value is given', function () {
  259. updated = false
  260. d.$update(123)
  261. assert.strictEqual(d.value, 123)
  262. assert.strictEqual(updated, true)
  263. })
  264. it('should not call user update() if the value is the same', function () {
  265. updated = false
  266. d.$update(123)
  267. assert.ok(!updated)
  268. })
  269. it('should call $applyFilter() is there are filters', function () {
  270. var filterApplied = false
  271. d.filters = []
  272. d.$applyFilters = function () {
  273. filterApplied = true
  274. }
  275. d.$update(234)
  276. assert.ok(filterApplied)
  277. })
  278. })
  279. describe('.$unbind()', function () {
  280. var d = build('text', 'abc', compiler),
  281. unbound = false,
  282. val
  283. d.unbind = function (v) {
  284. val = v
  285. unbound = true
  286. }
  287. it('should not work if it has no element yet', function () {
  288. d.el = null
  289. d.$unbind()
  290. assert.strictEqual(unbound, false)
  291. })
  292. it('should call user unbind() and null everything if it has an element', function () {
  293. d.el = true
  294. d.$unbind()
  295. assert.strictEqual(unbound, true)
  296. assert.ok(d.el === null && d.vm === null && d.binding === null && d.compiler === null)
  297. })
  298. it('should not execute if called more than once', function () {
  299. unbound = false
  300. d.$unbind()
  301. assert.notOk(unbound)
  302. })
  303. })
  304. describe('empty directive', function () {
  305. it('should copy as bind() if the def is a function', function () {
  306. var called = 0
  307. function call () {
  308. called++
  309. }
  310. Vue.directive('empty-dir-test1', call)
  311. var d = build('empty-dir-test1', '', compiler)
  312. d.bind()
  313. assert.strictEqual(called, 1)
  314. })
  315. it('should copy/delegate bind and unbind if the def is an object', function () {
  316. var called = 0
  317. function call () {
  318. called++
  319. }
  320. Vue.directive('empty-dir-test2', {
  321. bind: call,
  322. unbind: call
  323. })
  324. var d = build('empty-dir-test2', '', compiler, true)
  325. d.bind()
  326. d.unbind()
  327. assert.strictEqual(called, 2)
  328. })
  329. })
  330. })