api.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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(document.querySelector('#' + testId + ' span').innerHTML, testId)
  14. })
  15. after(function () {
  16. Seed.config({
  17. prefix: 'sd'
  18. })
  19. })
  20. })
  21. describe('filter()', function () {
  22. var reverse = function (input) {
  23. return input.split('').reverse().join('')
  24. }
  25. it('should create custom filter', function () {
  26. var testId = 'filter-1',
  27. msg = '12345'
  28. Seed.filter('reverse', reverse)
  29. mock(testId, '{{ test | reverse }}')
  30. new Seed({
  31. el: '#' + testId,
  32. scope: { test: msg }
  33. })
  34. assert.strictEqual(document.querySelector('#' + testId).innerHTML, '54321')
  35. })
  36. it('should return filter function if only one arg is given', function () {
  37. var f = Seed.filter('reverse')
  38. assert.strictEqual(f, reverse)
  39. })
  40. })
  41. describe('directive()', function () {
  42. var dirTest
  43. it('should create custom directive with set function only', function () {
  44. var testId = 'directive-1',
  45. msg = 'wowow'
  46. Seed.directive('test', function (value) {
  47. this.el.setAttribute(testId, value + '123')
  48. })
  49. mock(testId, '<span sd-test="test"></span>')
  50. new Seed({
  51. el: '#' + testId,
  52. scope: { test: msg }
  53. })
  54. var el = document.querySelector('#' + testId + ' span')
  55. assert.strictEqual(el.getAttribute(testId), msg + '123')
  56. })
  57. it('should create custom directive with object', function () {
  58. var testId = 'directive-2',
  59. msg = 'wowaaaa?'
  60. dirTest = {
  61. bind: function (value) {
  62. this.el.setAttribute(testId + 'bind', value + 'bind')
  63. },
  64. update: function (value) {
  65. this.el.setAttribute(testId + 'update', value + 'update')
  66. },
  67. unbind: function () {
  68. this.el.removeAttribute(testId + 'bind')
  69. }
  70. }
  71. Seed.directive('test2', dirTest)
  72. mock(testId, '<span sd-test2="test"></span>')
  73. var vm = new Seed({
  74. el: '#' + testId,
  75. scope: { test: msg }
  76. }),
  77. el = document.querySelector('#' + testId + ' span')
  78. assert.strictEqual(el.getAttribute(testId + 'bind'), msg + 'bind', 'should have called bind()')
  79. assert.strictEqual(el.getAttribute(testId + 'update'), msg + 'update', 'should have called update()')
  80. vm.$destroy() // assuming this works
  81. assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()')
  82. })
  83. it('should return directive object/fn if only one arg is given', function () {
  84. var dir = Seed.directive('test2')
  85. assert.strictEqual(dir, dirTest)
  86. })
  87. })
  88. describe('viewmodel()', function () {
  89. var testId = 'api-vm-test',
  90. Test = Seed.extend({
  91. className: 'hihi',
  92. scope: { hi: 'ok' }
  93. }),
  94. utils = require('seed/src/utils')
  95. it('should register a VM constructor', function () {
  96. Seed.viewmodel(testId, Test)
  97. assert.strictEqual(utils.vms[testId], Test)
  98. })
  99. it('should retrieve the VM if has only one arg', function () {
  100. assert.strictEqual(Seed.viewmodel(testId), Test)
  101. })
  102. it('should work with sd-viewmodel', function () {
  103. mock(testId, '<div sd-viewmodel="' + testId + '">{{hi}}</div>')
  104. var t = new Seed({ el: '#' + testId }),
  105. child = t.$el.querySelector('div')
  106. assert.strictEqual(child.className, 'hihi')
  107. assert.strictEqual(child.textContent, 'ok')
  108. })
  109. })
  110. describe('partial()', function () {
  111. var testId = 'api-partial-test',
  112. partial = '<div class="partial-test"><a>{{hi}}</a></div><span>hahaha</span>',
  113. utils = require('seed/src/utils')
  114. it('should register the partial as a dom fragment', function () {
  115. Seed.partial(testId, partial)
  116. var converted = utils.partials[testId]
  117. assert.ok(converted instanceof window.DocumentFragment)
  118. assert.strictEqual(converted.querySelector('.partial-test a').innerHTML, '{{hi}}')
  119. assert.strictEqual(converted.querySelector('span').innerHTML, 'hahaha')
  120. })
  121. it('should retrieve the partial if has only one arg', function () {
  122. assert.strictEqual(utils.partials[testId], Seed.partial(testId))
  123. })
  124. it('should work with sd-partial as a directive', function () {
  125. var testId = 'api-partial-direcitve'
  126. Seed.partial(testId, partial)
  127. mock(testId, '<div class="directive" sd-partial="' + testId + '">hello</div>')
  128. var t = new Seed({
  129. el: '#' + testId,
  130. scope: { hi: 'hohoho' }
  131. })
  132. assert.strictEqual(t.$el.querySelector('.directive .partial-test a').textContent, 'hohoho')
  133. assert.strictEqual(t.$el.querySelector('.directive span').innerHTML, 'hahaha')
  134. })
  135. it('should work with sd-partial as an inline interpolation', function () {
  136. var testId = 'api-partial-inline'
  137. Seed.partial(testId, partial)
  138. mock(testId, '<div class="inline">{{>' + testId + '}}</div>')
  139. var t = new Seed({
  140. el: '#' + testId,
  141. scope: { hi: 'hohoho' }
  142. })
  143. assert.strictEqual(t.$el.querySelector('.inline .partial-test a').textContent, 'hohoho')
  144. assert.strictEqual(t.$el.querySelector('.inline span').innerHTML, 'hahaha')
  145. })
  146. })
  147. describe('transition()', function () {
  148. var testId = 'api-trans-test',
  149. transition = {},
  150. utils = require('seed/src/utils')
  151. it('should register a transition object', function () {
  152. Seed.transition(testId, transition)
  153. assert.strictEqual(utils.transitions[testId], transition)
  154. })
  155. it('should retrieve the transition if has only one arg', function () {
  156. assert.strictEqual(Seed.transition(testId), transition)
  157. })
  158. // it('should work with sd-transition', function () {
  159. // assert.ok(false)
  160. // })
  161. })
  162. describe('extend()', function () {
  163. it('should return a subclass of Seed', function () {
  164. var Test = Seed.extend({})
  165. assert.ok(Test.prototype instanceof Seed)
  166. })
  167. it('should allow further extensions', function () {
  168. var Parent = Seed.extend({
  169. scope: {
  170. test: 'hi'
  171. }
  172. })
  173. var Child = Parent.extend({
  174. scope: {
  175. test2: 'ho',
  176. test3: {
  177. hi: 1
  178. }
  179. }
  180. })
  181. assert.strictEqual(Child.super, Parent)
  182. var child = new Child({
  183. scope: {
  184. test3: {
  185. ho: 2
  186. }
  187. }
  188. })
  189. assert.strictEqual(child.test, 'hi')
  190. assert.strictEqual(child.test2, 'ho')
  191. // should overwrite past 1 level deep
  192. assert.strictEqual(child.test3.ho, 2)
  193. assert.notOk(child.test3.hi)
  194. })
  195. describe('Options', function () {
  196. describe('init', function () {
  197. it('should be called on the instance when instantiating', function () {
  198. var called = false,
  199. Test = Seed.extend({ init: function () {
  200. called = true
  201. }})
  202. new Test({ el: document.createElement('div') })
  203. assert.ok(called)
  204. })
  205. })
  206. describe('proto', function () {
  207. it('should be mixed to the exteded VM\'s prototype', function () {
  208. var mixins = {
  209. a: 1,
  210. b: 2,
  211. c: function () {}
  212. }
  213. var Test = Seed.extend({ proto: mixins })
  214. for (var key in mixins) {
  215. assert.strictEqual(Test.prototype[key], mixins[key])
  216. }
  217. })
  218. })
  219. describe('scope', function () {
  220. it('should be copied to each instance', function () {
  221. var testData = { a: 1 },
  222. Test = Seed.extend({
  223. scope: {
  224. test: testData
  225. }
  226. })
  227. var t1 = new Test(),
  228. t2 = new Test()
  229. assert.ok(t1.hasOwnProperty('test'))
  230. assert.strictEqual(t1.test, testData)
  231. assert.ok(t2.hasOwnProperty('test'))
  232. assert.strictEqual(t2.test, testData)
  233. })
  234. })
  235. describe('lazy', function () {
  236. it('should make text input fields only trigger on change', function () {
  237. var Test = Seed.extend({
  238. template: '<input type="text" sd-model="test">',
  239. lazy: true
  240. })
  241. var t = new Test({
  242. scope: {
  243. test: 'hi'
  244. }
  245. })
  246. var input = t.$el.querySelector('input')
  247. input.value = 'hohoho'
  248. input.dispatchEvent(mockKeyEvent('keyup'))
  249. assert.strictEqual(t.test, 'hi')
  250. input.dispatchEvent(mockHTMLEvent('change'))
  251. assert.strictEqual(t.test, 'hohoho')
  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. })