api.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. describe('UNIT: API', function () {
  2. describe('ViewModel.extend()', function () {
  3. it('should return a subclass of seed.ViewModel', function () {
  4. var Test = seed.ViewModel.extend({})
  5. assert.ok(Test.prototype instanceof seed.ViewModel)
  6. })
  7. it('should mixin options.props', function () {
  8. var props = {
  9. a: 1,
  10. b: 2,
  11. c: function () {}
  12. }
  13. var Test = seed.ViewModel.extend({ props: props })
  14. for (var key in props) {
  15. assert.strictEqual(Test.prototype[key], props[key])
  16. }
  17. })
  18. it('should register VM in utils if options.id exists', function () {
  19. var Test = seed.ViewModel.extend({
  20. id: 'test',
  21. data: {
  22. test: 'I have a viewmodel!'
  23. },
  24. props: {
  25. hello: function () {
  26. return 'hello'
  27. }
  28. }
  29. }),
  30. utils = require('seed/src/utils')
  31. assert.strictEqual(utils.getVM('test'), Test)
  32. })
  33. it('should take options.template and work', function () {
  34. var Test = seed.ViewModel.extend({
  35. tagName: 'p',
  36. template: '<span>{{hello}}</span><a>haha</a>',
  37. data: {
  38. hello: 'Ahaha'
  39. }
  40. }),
  41. vm = new Test(),
  42. text1 = vm.$el.querySelector('span').textContent,
  43. text2 = vm.$el.querySelector('a').textContent
  44. assert.ok(Test.prototype.templateNode instanceof HTMLElement)
  45. assert.strictEqual(vm.$el.nodeName, 'P')
  46. assert.strictEqual(text1, 'Ahaha')
  47. assert.strictEqual(text2, 'haha')
  48. })
  49. it('should call options.init when instantiating', function () {
  50. var called = false,
  51. Test = seed.ViewModel.extend({ init: function () {
  52. called = true
  53. }}),
  54. test = new Test({ el: document.createElement('div') })
  55. assert.ok(called)
  56. })
  57. })
  58. describe('compile()', function () {
  59. it('should directly compile if arg is a node', function () {
  60. var testId = 'compile-1'
  61. mock(testId, '{{test}}')
  62. var vm = seed.compile('#' + testId, { data: { test: testId } })
  63. assert.ok(vm instanceof seed.ViewModel)
  64. assert.strictEqual($('#' + testId), testId)
  65. })
  66. it('should use correct VM constructor if sd-viewmodel is present', function () {
  67. var testId = 'compile-2'
  68. mock(testId, '{{test}}', {
  69. 'sd-viewmodel': 'test' // see register VM test above
  70. })
  71. var vm = seed.compile('#' + testId)
  72. assert.ok(vm instanceof seed.ViewModel)
  73. assert.strictEqual(vm.hello(), 'hello', 'should inherit options.props')
  74. assert.strictEqual($('#' + testId), 'I have a viewmodel!', 'should inherit options.data')
  75. })
  76. })
  77. describe('config()', function () {
  78. it('should work when changing prefix', function () {
  79. var testId = 'config-1'
  80. seed.config({
  81. prefix: 'test'
  82. })
  83. mock(testId, '<span test-text="test"></span>')
  84. seed.compile('#' + testId, { data: { test: testId }})
  85. assert.strictEqual($('#' + testId + ' span'), testId)
  86. })
  87. it('should work when changing interpolate tags', function () {
  88. var testId = 'config-2'
  89. seed.config({
  90. interpolateTags: {
  91. open: '<%',
  92. close: '%>'
  93. }
  94. })
  95. mock(testId, '<% test %>')
  96. seed.compile('#' + testId, { data: { test: testId }})
  97. assert.strictEqual($('#' + testId), testId)
  98. })
  99. after(function () {
  100. seed.config({
  101. prefix: 'sd',
  102. interpolateTags: {
  103. open: '{{',
  104. close: '}}'
  105. }
  106. })
  107. })
  108. })
  109. describe('filter()', function () {
  110. var reverse = function (input) {
  111. return input.split('').reverse().join('')
  112. }
  113. it('should create custom filter', function () {
  114. var testId = 'filter-1',
  115. msg = '12345'
  116. seed.filter('reverse', reverse)
  117. mock(testId, '{{ test | reverse }}')
  118. seed.compile('#' + testId, { data: { test: msg }})
  119. assert.strictEqual($('#' + testId), '54321')
  120. })
  121. it('should return filter function if only one arg is given', function () {
  122. var f = seed.filter('reverse')
  123. assert.strictEqual(f, reverse)
  124. })
  125. })
  126. describe('directive()', function () {
  127. var dirTest
  128. it('should create custom directive with set function only', function () {
  129. var testId = 'directive-1',
  130. msg = 'wowow'
  131. seed.directive('test', function (value) {
  132. this.el.setAttribute(testId, value + '123')
  133. })
  134. mock(testId, '<span sd-test="test"></span>')
  135. seed.compile('#' + testId, { data: { test: msg }})
  136. var el = document.querySelector('#' + testId + ' span')
  137. assert.strictEqual(el.getAttribute(testId), msg + '123')
  138. })
  139. it('should create custom directive with object', function () {
  140. var testId = 'directive-2',
  141. msg = 'wowaaaa?'
  142. dirTest = {
  143. bind: function (value) {
  144. this.el.setAttribute(testId + 'bind', msg + 'bind')
  145. },
  146. update: function (value) {
  147. this.el.setAttribute(testId + 'update', msg + 'update')
  148. },
  149. unbind: function () {
  150. this.el.removeAttribute(testId + 'bind')
  151. }
  152. }
  153. seed.directive('test2', dirTest)
  154. mock(testId, '<span sd-test2="test"></span>')
  155. var vm = seed.compile('#' + testId, { data: { test: msg }}),
  156. el = document.querySelector('#' + testId + ' span')
  157. assert.strictEqual(el.getAttribute(testId + 'bind'), msg + 'bind', 'should have called bind()')
  158. assert.strictEqual(el.getAttribute(testId + 'update'), msg + 'update', 'should have called update()')
  159. vm.$destroy() // assuming this works
  160. assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()')
  161. })
  162. it('should return directive object/fn if only one arg is given', function () {
  163. var dir = seed.directive('test2')
  164. assert.strictEqual(dir, dirTest)
  165. })
  166. })
  167. })