directive.js 13 KB

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