api.js 14 KB

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