directive.js 9.7 KB

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