directive.js 9.7 KB

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