api.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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({
  10. el: '#' + testId,
  11. scope: { 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({
  25. el: '#' + testId,
  26. scope: { 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({
  50. el: '#' + testId,
  51. scope: { 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({
  70. el: '#' + testId,
  71. scope: { 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', value + 'bind')
  82. },
  83. update: function (value) {
  84. this.el.setAttribute(testId + 'update', value + '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({
  93. el: '#' + testId,
  94. scope: { 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. var testId = 'api-vm-test',
  109. Test = Seed.extend({
  110. className: 'hihi',
  111. scope: { hi: 'ok' }
  112. }),
  113. utils = require('seed/src/utils')
  114. it('should register a VM constructor', function () {
  115. Seed.vm(testId, Test)
  116. assert.strictEqual(utils.vms[testId], Test)
  117. })
  118. it('should retrieve the VM if has only one arg', function () {
  119. assert.strictEqual(Seed.vm(testId), Test)
  120. })
  121. it('should work with sd-viewmodel', function () {
  122. mock(testId, '<div sd-viewmodel="' + testId + '">{{hi}}</div>')
  123. var t = new Seed({ el: '#' + testId }),
  124. child = t.$el.querySelector('div')
  125. assert.strictEqual(child.className, 'hihi')
  126. assert.strictEqual(child.textContent, 'ok')
  127. })
  128. })
  129. describe('partial()', function () {
  130. var testId = 'api-partial-test',
  131. partial = '<div class="partial-test"><a>{{hi}}</a></div><span>hahaha</span>',
  132. utils = require('seed/src/utils')
  133. it('should register the partial as a dom fragment', function () {
  134. Seed.partial(testId, partial)
  135. var converted = utils.partials[testId]
  136. assert.ok(converted instanceof window.DocumentFragment)
  137. assert.strictEqual(converted.querySelector('.partial-test a').innerHTML, '{{hi}}')
  138. assert.strictEqual(converted.querySelector('span').innerHTML, 'hahaha')
  139. })
  140. it('should retrieve the partial if has only one arg', function () {
  141. assert.strictEqual(utils.partials[testId], Seed.partial(testId))
  142. })
  143. it('should work with sd-partial as a directive', function () {
  144. var testId = 'api-partial-direcitve'
  145. Seed.partial(testId, partial)
  146. mock(testId, '<div class="directive" sd-partial="' + testId + '">hello</div>')
  147. var t = new Seed({
  148. el: '#' + testId,
  149. scope: { hi: 'hohoho' }
  150. })
  151. assert.strictEqual(t.$el.querySelector('.directive .partial-test a').textContent, 'hohoho')
  152. assert.strictEqual(t.$el.querySelector('.directive span').innerHTML, 'hahaha')
  153. })
  154. it('should work with sd-partial as an inline interpolation', function () {
  155. var testId = 'api-partial-inline'
  156. Seed.partial(testId, partial)
  157. mock(testId, '<div class="inline">{{>' + testId + '}}</div>')
  158. var t = new Seed({
  159. el: '#' + testId,
  160. scope: { hi: 'hohoho' }
  161. })
  162. assert.strictEqual(t.$el.querySelector('.inline .partial-test a').textContent, 'hohoho')
  163. assert.strictEqual(t.$el.querySelector('.inline span').innerHTML, 'hahaha')
  164. })
  165. })
  166. describe('transition()', function () {
  167. var testId = 'api-trans-test',
  168. transition = {},
  169. utils = require('seed/src/utils')
  170. it('should register a transition object', function () {
  171. Seed.transition(testId, transition)
  172. assert.strictEqual(utils.transitions[testId], transition)
  173. })
  174. it('should retrieve the transition if has only one arg', function () {
  175. assert.strictEqual(Seed.transition(testId), transition)
  176. })
  177. // it('should work with sd-transition', function () {
  178. // assert.ok(false)
  179. // })
  180. })
  181. describe('extend()', function () {
  182. it('should return a subclass of Seed', function () {
  183. var Test = Seed.extend({})
  184. assert.ok(Test.prototype instanceof Seed)
  185. })
  186. it('should allow further extensions', function () {
  187. var Parent = Seed.extend({
  188. scope: {
  189. test: 'hi'
  190. }
  191. })
  192. var Child = Parent.extend({
  193. scope: {
  194. test2: 'ho',
  195. test3: {
  196. hi: 1
  197. }
  198. }
  199. })
  200. assert.strictEqual(Child.super, Parent)
  201. var child = new Child({
  202. scope: {
  203. test3: {
  204. ho: 2
  205. }
  206. }
  207. })
  208. assert.strictEqual(child.test, 'hi')
  209. assert.strictEqual(child.test2, 'ho')
  210. // should overwrite past 1 level deep
  211. assert.strictEqual(child.test3.ho, 2)
  212. assert.notOk(child.test3.hi)
  213. })
  214. describe('Options', function () {
  215. describe('init', function () {
  216. it('should be called on the instance when instantiating', function () {
  217. var called = false,
  218. Test = Seed.extend({ init: function () {
  219. called = true
  220. }})
  221. new Test({ el: document.createElement('div') })
  222. assert.ok(called)
  223. })
  224. })
  225. describe('proto', function () {
  226. it('should be mixed to the exteded VM\'s prototype', function () {
  227. var mixins = {
  228. a: 1,
  229. b: 2,
  230. c: function () {}
  231. }
  232. var Test = Seed.extend({ proto: mixins })
  233. for (var key in mixins) {
  234. assert.strictEqual(Test.prototype[key], mixins[key])
  235. }
  236. })
  237. })
  238. describe('scope', function () {
  239. it('should be copied to each instance', function () {
  240. var testData = { a: 1 },
  241. Test = Seed.extend({
  242. scope: {
  243. test: testData
  244. }
  245. })
  246. var t1 = new Test(),
  247. t2 = new Test()
  248. assert.ok(t1.hasOwnProperty('test'))
  249. assert.strictEqual(t1.test, testData)
  250. assert.ok(t2.hasOwnProperty('test'))
  251. assert.strictEqual(t2.test, testData)
  252. })
  253. })
  254. describe('element options', function () {
  255. it('should not accept el as an extension option', function () {
  256. var el = document.createElement('div'),
  257. Test = Seed.extend({ el: el }),
  258. t = new Test()
  259. assert.notStrictEqual(t.$el, el)
  260. })
  261. it('should create el with options: tagName, id, className and attributes', function () {
  262. var Test = Seed.extend({
  263. tagName: 'p',
  264. id: 'extend-test',
  265. className: 'extend',
  266. attributes: {
  267. 'test': 'hi',
  268. 'sd-text': 'hoho'
  269. },
  270. scope: {
  271. hoho: 'what'
  272. }
  273. })
  274. var t = new Test()
  275. assert.strictEqual(t.$el.nodeName, 'P', 'tagName should match')
  276. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  277. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  278. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  279. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  280. })
  281. it('should ignore tagName when el is passed as an instance option', function () {
  282. var el = document.createElement('div'),
  283. Test = Seed.extend({
  284. tagName: 'p',
  285. id: 'extend-test',
  286. className: 'extend',
  287. attributes: {
  288. 'test': 'hi',
  289. 'sd-text': 'hoho'
  290. },
  291. scope: {
  292. hoho: 'what'
  293. }
  294. }),
  295. t = new Test({
  296. el: el
  297. })
  298. assert.strictEqual(t.$el, el, 'should use instance el')
  299. assert.notStrictEqual(t.$el.nodeName, 'P', 'tagName should NOT match')
  300. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  301. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  302. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  303. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  304. })
  305. })
  306. describe('template', function () {
  307. var raw = '<span>{{hello}}</span><a>haha</a>'
  308. it('should take direct string template and work', function () {
  309. var Test = Seed.extend({
  310. tagName: 'p',
  311. template: raw,
  312. scope: {
  313. hello: 'Ahaha'
  314. }
  315. }),
  316. vm = new Test(),
  317. text1 = vm.$el.querySelector('span').textContent,
  318. text2 = vm.$el.querySelector('a').textContent
  319. assert.strictEqual(vm.$el.nodeName, 'P')
  320. assert.strictEqual(text1, 'Ahaha')
  321. assert.strictEqual(text2, 'haha')
  322. })
  323. it('should take a #id and work', function () {
  324. var testId = 'template-test',
  325. tpl = document.createElement('script')
  326. tpl.id = testId
  327. tpl.type = 'text/template'
  328. tpl.innerHTML = raw
  329. document.getElementById('test').appendChild(tpl)
  330. var Test = Seed.extend({
  331. template: '#' + testId,
  332. scope: { hello: testId }
  333. })
  334. var t = new Test()
  335. assert.strictEqual(t.$el.querySelector('span').textContent, testId)
  336. })
  337. it('should be overwritable', function () {
  338. var Test = Seed.extend({
  339. template: '<div>this should not happen</div>'
  340. })
  341. var t = new Test({
  342. template: raw,
  343. scope: {
  344. hello: 'overwritten!'
  345. }
  346. })
  347. assert.strictEqual(t.$el.querySelector('span').textContent, 'overwritten!')
  348. })
  349. })
  350. describe('directives', function () {
  351. it('should allow the VM to use private directives', function () {
  352. var Test = Seed.extend({
  353. directives: {
  354. test: function (value) {
  355. this.el.innerHTML = value ? 'YES' : 'NO'
  356. }
  357. }
  358. })
  359. var t = new Test({
  360. attributes: {
  361. 'sd-test': 'ok'
  362. },
  363. scope: {
  364. ok: true
  365. }
  366. })
  367. assert.strictEqual(t.$el.innerHTML, 'YES')
  368. t.ok = false
  369. assert.strictEqual(t.$el.innerHTML, 'NO')
  370. })
  371. })
  372. describe('filters', function () {
  373. it('should allow the VM to use private filters', function () {
  374. var Test = Seed.extend({
  375. filters: {
  376. test: function (value) {
  377. return value + '12345'
  378. }
  379. }
  380. })
  381. var t = new Test({
  382. template: '{{hi | test}}',
  383. scope: {
  384. hi: 'hohoho'
  385. }
  386. })
  387. assert.strictEqual(t.$el.textContent, 'hohoho12345')
  388. })
  389. })
  390. describe('vms', function () {
  391. it('should allow the VM to use private child VMs', function () {
  392. var Child = Seed.extend({
  393. scope: {
  394. name: 'child'
  395. }
  396. })
  397. var Parent = Seed.extend({
  398. template: '<p>{{name}}</p><div sd-viewmodel="child">{{name}}</div>',
  399. scope: {
  400. name: 'dad'
  401. },
  402. vms: {
  403. child: Child
  404. }
  405. })
  406. var p = new Parent()
  407. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  408. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  409. })
  410. })
  411. describe('partials', function () {
  412. it('should allow the VM to use private partials', function () {
  413. var Test = Seed.extend({
  414. attributes: {
  415. 'sd-partial': 'test'
  416. },
  417. partials: {
  418. test: '<a>{{a}}</a><p>{{b}}</p>'
  419. },
  420. scope: {
  421. a: 'hi',
  422. b: 'ho'
  423. }
  424. })
  425. var t = new Test()
  426. assert.strictEqual(t.$el.querySelector('a').textContent, 'hi')
  427. assert.strictEqual(t.$el.querySelector('p').textContent, 'ho')
  428. })
  429. })
  430. describe('transitions', function () {
  431. // it('should be tested', function () {
  432. // assert.ok(false)
  433. // })
  434. })
  435. })
  436. })
  437. })