directive.js 10 KB

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