directive.js 11 KB

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