api.js 6.2 KB

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