directive.js 15 KB

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