api.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. describe('UNIT: API', function () {
  2. describe('filter()', function () {
  3. var reverse = function (input) {
  4. return input.split('').reverse().join('')
  5. }
  6. it('should create custom filter', function () {
  7. var testId = 'filter-1',
  8. msg = '12345'
  9. seed.filter('reverse', reverse)
  10. mock(testId, '{{ test | reverse }}')
  11. new seed.ViewModel({
  12. el: '#' + testId,
  13. data: { test: msg }
  14. })
  15. assert.strictEqual($('#' + testId), '54321')
  16. })
  17. it('should return filter function if only one arg is given', function () {
  18. var f = seed.filter('reverse')
  19. assert.strictEqual(f, reverse)
  20. })
  21. })
  22. describe('directive()', function () {
  23. var dirTest
  24. it('should create custom directive with set function only', function () {
  25. var testId = 'directive-1',
  26. msg = 'wowow'
  27. seed.directive('test', function (value) {
  28. this.el.setAttribute(testId, value + '123')
  29. })
  30. mock(testId, '<span sd-test="test"></span>')
  31. new seed.ViewModel({
  32. el: '#' + testId,
  33. data: { test: msg }
  34. })
  35. var el = document.querySelector('#' + testId + ' span')
  36. assert.strictEqual(el.getAttribute(testId), msg + '123')
  37. })
  38. it('should create custom directive with object', function () {
  39. var testId = 'directive-2',
  40. msg = 'wowaaaa?'
  41. dirTest = {
  42. bind: function (value) {
  43. this.el.setAttribute(testId + 'bind', msg + 'bind')
  44. },
  45. update: function (value) {
  46. this.el.setAttribute(testId + 'update', msg + 'update')
  47. },
  48. unbind: function () {
  49. this.el.removeAttribute(testId + 'bind')
  50. }
  51. }
  52. seed.directive('test2', dirTest)
  53. mock(testId, '<span sd-test2="test"></span>')
  54. var vm = new seed.ViewModel({
  55. el: '#' + testId,
  56. data: { test: msg }
  57. }),
  58. el = document.querySelector('#' + testId + ' span')
  59. assert.strictEqual(el.getAttribute(testId + 'bind'), msg + 'bind', 'should have called bind()')
  60. assert.strictEqual(el.getAttribute(testId + 'update'), msg + 'update', 'should have called update()')
  61. vm.$destroy() // assuming this works
  62. assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()')
  63. })
  64. it('should return directive object/fn if only one arg is given', function () {
  65. var dir = seed.directive('test2')
  66. assert.strictEqual(dir, dirTest)
  67. })
  68. })
  69. describe('ViewModel.extend()', function () {
  70. it('should return a subclass of seed.ViewModel', function () {
  71. var Test = seed.ViewModel.extend({})
  72. assert.ok(Test.prototype instanceof seed.ViewModel)
  73. })
  74. it('should allow further extensions', function () {
  75. var Parent = seed.ViewModel.extend({
  76. data: {
  77. test: 'hi'
  78. }
  79. })
  80. var Child = Parent.extend({
  81. data: {
  82. test2: 'ho'
  83. }
  84. })
  85. assert.strictEqual(Child.super, Parent)
  86. var child = new Child()
  87. assert.strictEqual(child.test, 'hi')
  88. assert.strictEqual(child.test2, 'ho')
  89. })
  90. describe('Options', function () {
  91. describe('init', function () {
  92. it('should be called on the instance when instantiating', function () {
  93. var called = false,
  94. Test = seed.ViewModel.extend({ init: function () {
  95. called = true
  96. }}),
  97. test = new Test({ el: document.createElement('div') })
  98. assert.ok(called)
  99. })
  100. })
  101. describe('props', function () {
  102. it('should be mixed to the exteded VM\'s prototype', function () {
  103. var props = {
  104. a: 1,
  105. b: 2,
  106. c: function () {}
  107. }
  108. var Test = seed.ViewModel.extend({ props: props })
  109. for (var key in props) {
  110. assert.strictEqual(Test.prototype[key], props[key])
  111. }
  112. })
  113. })
  114. describe('data', function () {
  115. it('should be copied to each instance', function () {
  116. var testData = { a: 1 },
  117. Test = seed.ViewModel.extend({
  118. data: {
  119. test: testData
  120. }
  121. })
  122. var t1 = new Test(),
  123. t2 = new Test()
  124. assert.ok(t1.hasOwnProperty('test'))
  125. assert.strictEqual(t1.test, testData)
  126. assert.ok(t2.hasOwnProperty('test'))
  127. assert.strictEqual(t2.test, testData)
  128. })
  129. })
  130. describe('element options', function () {
  131. it('should not accept el as an extension option', function () {
  132. var el = document.createElement('div'),
  133. Test = seed.ViewModel.extend({ el: el }),
  134. t = new Test()
  135. assert.notStrictEqual(t.$el, el)
  136. })
  137. it('should create el with options: tagName, id, className and attributes', function () {
  138. var Test = seed.ViewModel.extend({
  139. tagName: 'p',
  140. id: 'extend-test',
  141. className: 'extend',
  142. attributes: {
  143. 'test': 'hi',
  144. 'sd-text': 'hoho'
  145. },
  146. data: {
  147. hoho: 'what'
  148. }
  149. })
  150. var t = new Test()
  151. assert.strictEqual(t.$el.nodeName, 'P', 'tagName should match')
  152. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  153. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  154. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  155. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  156. })
  157. it('should ignore tagName when el is passed as an instance option', function () {
  158. var el = document.createElement('div'),
  159. Test = seed.ViewModel.extend({
  160. tagName: 'p',
  161. id: 'extend-test',
  162. className: 'extend',
  163. attributes: {
  164. 'test': 'hi',
  165. 'sd-text': 'hoho'
  166. },
  167. data: {
  168. hoho: 'what'
  169. }
  170. }),
  171. t = new Test({
  172. el: el
  173. })
  174. assert.strictEqual(t.$el, el, 'should use instance el')
  175. assert.notStrictEqual(t.$el.nodeName, 'P', 'tagName should NOT match')
  176. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  177. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  178. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  179. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  180. })
  181. })
  182. describe('template', function () {
  183. var raw = '<span>{{hello}}</span><a>haha</a>'
  184. it('should take direct string template and work', function () {
  185. var Test = seed.ViewModel.extend({
  186. tagName: 'p',
  187. template: raw,
  188. data: {
  189. hello: 'Ahaha'
  190. }
  191. }),
  192. vm = new Test(),
  193. text1 = vm.$el.querySelector('span').textContent,
  194. text2 = vm.$el.querySelector('a').textContent
  195. assert.strictEqual(vm.$el.nodeName, 'P')
  196. assert.strictEqual(text1, 'Ahaha')
  197. assert.strictEqual(text2, 'haha')
  198. })
  199. it('should take a #id and work', function () {
  200. var testId = 'template-test',
  201. tpl = document.createElement('script')
  202. tpl.id = testId
  203. tpl.type = 'text/template'
  204. tpl.innerHTML = raw
  205. document.getElementById('test').appendChild(tpl)
  206. var Test = seed.ViewModel.extend({
  207. template: '#' + testId,
  208. data: { hello: testId }
  209. })
  210. var t = new Test()
  211. assert.strictEqual(t.$el.querySelector('span').textContent, testId)
  212. })
  213. it('should be overwritable', function () {
  214. var Test = seed.ViewModel.extend({
  215. template: '<div>this should not happen</div>'
  216. })
  217. var t = new Test({
  218. template: raw,
  219. data: {
  220. hello: 'overwritten!'
  221. }
  222. })
  223. assert.strictEqual(t.$el.querySelector('span').textContent, 'overwritten!')
  224. })
  225. })
  226. })
  227. })
  228. describe('config()', function () {
  229. it('should work when changing prefix', function () {
  230. var testId = 'config-1'
  231. seed.config({
  232. prefix: 'test'
  233. })
  234. mock(testId, '<span test-text="test"></span>')
  235. new seed.ViewModel({
  236. el: '#' + testId,
  237. data: { test: testId }
  238. })
  239. assert.strictEqual($('#' + testId + ' span'), testId)
  240. })
  241. it('should work when changing interpolate tags', function () {
  242. var testId = 'config-2'
  243. seed.config({
  244. interpolateTags: {
  245. open: '<%',
  246. close: '%>'
  247. }
  248. })
  249. mock(testId, '<% test %>')
  250. new seed.ViewModel({
  251. el: '#' + testId,
  252. data: { test: testId }
  253. })
  254. assert.strictEqual($('#' + testId), testId)
  255. })
  256. after(function () {
  257. seed.config({
  258. prefix: 'sd',
  259. interpolateTags: {
  260. open: '{{',
  261. close: '}}'
  262. }
  263. })
  264. })
  265. })
  266. })