api.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. // IE treats <% ... %> as a tag... wtf
  18. Seed.config({
  19. interpolateTags: {
  20. open: '(%',
  21. close: '%)'
  22. }
  23. })
  24. mock(testId, '(% test %)')
  25. new Seed({
  26. el: '#' + testId,
  27. scope: { test: testId }
  28. })
  29. assert.strictEqual($('#' + testId), testId)
  30. })
  31. after(function () {
  32. Seed.config({
  33. prefix: 'sd',
  34. interpolateTags: {
  35. open: '{{',
  36. close: '}}'
  37. }
  38. })
  39. })
  40. })
  41. describe('filter()', function () {
  42. var reverse = function (input) {
  43. return input.split('').reverse().join('')
  44. }
  45. it('should create custom filter', function () {
  46. var testId = 'filter-1',
  47. msg = '12345'
  48. Seed.filter('reverse', reverse)
  49. mock(testId, '{{ test | reverse }}')
  50. new Seed({
  51. el: '#' + testId,
  52. scope: { test: msg }
  53. })
  54. assert.strictEqual($('#' + testId), '54321')
  55. })
  56. it('should return filter function if only one arg is given', function () {
  57. var f = Seed.filter('reverse')
  58. assert.strictEqual(f, reverse)
  59. })
  60. })
  61. describe('directive()', function () {
  62. var dirTest
  63. it('should create custom directive with set function only', function () {
  64. var testId = 'directive-1',
  65. msg = 'wowow'
  66. Seed.directive('test', function (value) {
  67. this.el.setAttribute(testId, value + '123')
  68. })
  69. mock(testId, '<span sd-test="test"></span>')
  70. new Seed({
  71. el: '#' + testId,
  72. scope: { test: msg }
  73. })
  74. var el = document.querySelector('#' + testId + ' span')
  75. assert.strictEqual(el.getAttribute(testId), msg + '123')
  76. })
  77. it('should create custom directive with object', function () {
  78. var testId = 'directive-2',
  79. msg = 'wowaaaa?'
  80. dirTest = {
  81. bind: function (value) {
  82. this.el.setAttribute(testId + 'bind', value + 'bind')
  83. },
  84. update: function (value) {
  85. this.el.setAttribute(testId + 'update', value + 'update')
  86. },
  87. unbind: function () {
  88. this.el.removeAttribute(testId + 'bind')
  89. }
  90. }
  91. Seed.directive('test2', dirTest)
  92. mock(testId, '<span sd-test2="test"></span>')
  93. var vm = new Seed({
  94. el: '#' + testId,
  95. scope: { test: msg }
  96. }),
  97. el = document.querySelector('#' + testId + ' span')
  98. assert.strictEqual(el.getAttribute(testId + 'bind'), msg + 'bind', 'should have called bind()')
  99. assert.strictEqual(el.getAttribute(testId + 'update'), msg + 'update', 'should have called update()')
  100. vm.$destroy() // assuming this works
  101. assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()')
  102. })
  103. it('should return directive object/fn if only one arg is given', function () {
  104. var dir = Seed.directive('test2')
  105. assert.strictEqual(dir, dirTest)
  106. })
  107. })
  108. describe('vm()', function () {
  109. var testId = 'api-vm-test',
  110. Test = Seed.extend({
  111. className: 'hihi',
  112. scope: { hi: 'ok' }
  113. }),
  114. utils = require('seed/src/utils')
  115. it('should register a VM constructor', function () {
  116. Seed.vm(testId, Test)
  117. assert.strictEqual(utils.vms[testId], Test)
  118. })
  119. it('should retrieve the VM if has only one arg', function () {
  120. assert.strictEqual(Seed.vm(testId), Test)
  121. })
  122. it('should work with sd-viewmodel', function () {
  123. mock(testId, '<div sd-viewmodel="' + testId + '">{{hi}}</div>')
  124. var t = new Seed({ el: '#' + testId }),
  125. child = t.$el.querySelector('div')
  126. assert.strictEqual(child.className, 'hihi')
  127. assert.strictEqual(child.textContent, 'ok')
  128. })
  129. })
  130. describe('partial()', function () {
  131. var testId = 'api-partial-test',
  132. partial = '<div class="partial-test"><a>{{hi}}</a></div><span>hahaha</span>',
  133. utils = require('seed/src/utils')
  134. it('should register the partial as a dom fragment', function () {
  135. Seed.partial(testId, partial)
  136. var converted = utils.partials[testId]
  137. assert.ok(converted instanceof window.DocumentFragment)
  138. assert.strictEqual(converted.querySelector('.partial-test a').innerHTML, '{{hi}}')
  139. assert.strictEqual(converted.querySelector('span').innerHTML, 'hahaha')
  140. })
  141. it('should retrieve the partial if has only one arg', function () {
  142. assert.strictEqual(utils.partials[testId], Seed.partial(testId))
  143. })
  144. it('should work with sd-partial as a directive', function () {
  145. var testId = 'api-partial-direcitve'
  146. Seed.partial(testId, partial)
  147. mock(testId, '<div class="directive" sd-partial="' + testId + '">hello</div>')
  148. var t = new Seed({
  149. el: '#' + testId,
  150. scope: { hi: 'hohoho' }
  151. })
  152. assert.strictEqual(t.$el.querySelector('.directive .partial-test a').textContent, 'hohoho')
  153. assert.strictEqual(t.$el.querySelector('.directive span').innerHTML, 'hahaha')
  154. })
  155. it('should work with sd-partial as an inline interpolation', function () {
  156. var testId = 'api-partial-inline'
  157. Seed.partial(testId, partial)
  158. mock(testId, '<div class="inline">{{>' + testId + '}}</div>')
  159. var t = new Seed({
  160. el: '#' + testId,
  161. scope: { hi: 'hohoho' }
  162. })
  163. assert.strictEqual(t.$el.querySelector('.inline .partial-test a').textContent, 'hohoho')
  164. assert.strictEqual(t.$el.querySelector('.inline span').innerHTML, 'hahaha')
  165. })
  166. })
  167. describe('transition()', function () {
  168. var testId = 'api-trans-test',
  169. transition = {},
  170. utils = require('seed/src/utils')
  171. it('should register a transition object', function () {
  172. Seed.transition(testId, transition)
  173. assert.strictEqual(utils.transitions[testId], transition)
  174. })
  175. it('should retrieve the transition if has only one arg', function () {
  176. assert.strictEqual(Seed.transition(testId), transition)
  177. })
  178. // it('should work with sd-transition', function () {
  179. // assert.ok(false)
  180. // })
  181. })
  182. describe('extend()', function () {
  183. it('should return a subclass of Seed', function () {
  184. var Test = Seed.extend({})
  185. assert.ok(Test.prototype instanceof Seed)
  186. })
  187. it('should allow further extensions', function () {
  188. var Parent = Seed.extend({
  189. scope: {
  190. test: 'hi'
  191. }
  192. })
  193. var Child = Parent.extend({
  194. scope: {
  195. test2: 'ho',
  196. test3: {
  197. hi: 1
  198. }
  199. }
  200. })
  201. assert.strictEqual(Child.super, Parent)
  202. var child = new Child({
  203. scope: {
  204. test3: {
  205. ho: 2
  206. }
  207. }
  208. })
  209. assert.strictEqual(child.test, 'hi')
  210. assert.strictEqual(child.test2, 'ho')
  211. // should overwrite past 1 level deep
  212. assert.strictEqual(child.test3.ho, 2)
  213. assert.notOk(child.test3.hi)
  214. })
  215. describe('Options', function () {
  216. describe('init', function () {
  217. it('should be called on the instance when instantiating', function () {
  218. var called = false,
  219. Test = Seed.extend({ init: function () {
  220. called = true
  221. }})
  222. new Test({ el: document.createElement('div') })
  223. assert.ok(called)
  224. })
  225. })
  226. describe('proto', function () {
  227. it('should be mixed to the exteded VM\'s prototype', function () {
  228. var mixins = {
  229. a: 1,
  230. b: 2,
  231. c: function () {}
  232. }
  233. var Test = Seed.extend({ proto: mixins })
  234. for (var key in mixins) {
  235. assert.strictEqual(Test.prototype[key], mixins[key])
  236. }
  237. })
  238. })
  239. describe('scope', function () {
  240. it('should be copied to each instance', function () {
  241. var testData = { a: 1 },
  242. Test = Seed.extend({
  243. scope: {
  244. test: testData
  245. }
  246. })
  247. var t1 = new Test(),
  248. t2 = new Test()
  249. assert.ok(t1.hasOwnProperty('test'))
  250. assert.strictEqual(t1.test, testData)
  251. assert.ok(t2.hasOwnProperty('test'))
  252. assert.strictEqual(t2.test, testData)
  253. })
  254. })
  255. describe('lazy', function () {
  256. it('should make text input fields only trigger on change', function () {
  257. var Test = Seed.extend({
  258. template: '<input type="text" sd-model="test">',
  259. lazy: true
  260. })
  261. var t = new Test({
  262. scope: {
  263. test: 'hi'
  264. }
  265. })
  266. var input = t.$el.querySelector('input')
  267. input.value = 'hohoho'
  268. input.dispatchEvent(mockKeyEvent('keyup'))
  269. assert.strictEqual(t.test, 'hi')
  270. input.dispatchEvent(mockChangeEvent())
  271. assert.strictEqual(t.test, 'hohoho')
  272. })
  273. })
  274. describe('element options', function () {
  275. it('should not accept el as an extension option', function () {
  276. var el = document.createElement('div'),
  277. Test = Seed.extend({ el: el }),
  278. t = new Test()
  279. assert.notStrictEqual(t.$el, el)
  280. })
  281. it('should create el with options: tagName, id, className and attributes', function () {
  282. var Test = Seed.extend({
  283. tagName: 'p',
  284. id: 'extend-test',
  285. className: 'extend',
  286. attributes: {
  287. 'test': 'hi',
  288. 'sd-text': 'hoho'
  289. },
  290. scope: {
  291. hoho: 'what'
  292. }
  293. })
  294. var t = new Test()
  295. assert.strictEqual(t.$el.nodeName, 'P', 'tagName should match')
  296. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  297. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  298. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  299. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  300. })
  301. it('should ignore tagName when el is passed as an instance option', function () {
  302. var el = document.createElement('div'),
  303. Test = Seed.extend({
  304. tagName: 'p',
  305. id: 'extend-test',
  306. className: 'extend',
  307. attributes: {
  308. 'test': 'hi',
  309. 'sd-text': 'hoho'
  310. },
  311. scope: {
  312. hoho: 'what'
  313. }
  314. }),
  315. t = new Test({
  316. el: el
  317. })
  318. assert.strictEqual(t.$el, el, 'should use instance el')
  319. assert.notStrictEqual(t.$el.nodeName, 'P', 'tagName should NOT match')
  320. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  321. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  322. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  323. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  324. })
  325. })
  326. describe('template', function () {
  327. var raw = '<span>{{hello}}</span><a>haha</a>'
  328. it('should take direct string template and work', function () {
  329. var Test = Seed.extend({
  330. tagName: 'p',
  331. template: raw,
  332. scope: {
  333. hello: 'Ahaha'
  334. }
  335. }),
  336. vm = new Test(),
  337. text1 = vm.$el.querySelector('span').textContent,
  338. text2 = vm.$el.querySelector('a').textContent
  339. assert.strictEqual(vm.$el.nodeName, 'P')
  340. assert.strictEqual(text1, 'Ahaha')
  341. assert.strictEqual(text2, 'haha')
  342. })
  343. it('should take a #id and work', function () {
  344. var testId = 'template-test',
  345. tpl = document.createElement('script')
  346. tpl.id = testId
  347. tpl.type = 'text/template'
  348. tpl.innerHTML = raw
  349. document.getElementById('test').appendChild(tpl)
  350. var Test = Seed.extend({
  351. template: '#' + testId,
  352. scope: { hello: testId }
  353. })
  354. var t = new Test()
  355. assert.strictEqual(t.$el.querySelector('span').textContent, testId)
  356. })
  357. it('should be overwritable', function () {
  358. var Test = Seed.extend({
  359. template: '<div>this should not happen</div>'
  360. })
  361. var t = new Test({
  362. template: raw,
  363. scope: {
  364. hello: 'overwritten!'
  365. }
  366. })
  367. assert.strictEqual(t.$el.querySelector('span').textContent, 'overwritten!')
  368. })
  369. })
  370. describe('directives', function () {
  371. it('should allow the VM to use private directives', function () {
  372. var Test = Seed.extend({
  373. directives: {
  374. test: function (value) {
  375. this.el.innerHTML = value ? 'YES' : 'NO'
  376. }
  377. }
  378. })
  379. var t = new Test({
  380. attributes: {
  381. 'sd-test': 'ok'
  382. },
  383. scope: {
  384. ok: true
  385. }
  386. })
  387. assert.strictEqual(t.$el.innerHTML, 'YES')
  388. t.ok = false
  389. assert.strictEqual(t.$el.innerHTML, 'NO')
  390. })
  391. })
  392. describe('filters', function () {
  393. it('should allow the VM to use private filters', function () {
  394. var Test = Seed.extend({
  395. filters: {
  396. test: function (value) {
  397. return value + '12345'
  398. }
  399. }
  400. })
  401. var t = new Test({
  402. template: '{{hi | test}}',
  403. scope: {
  404. hi: 'hohoho'
  405. }
  406. })
  407. assert.strictEqual(t.$el.textContent, 'hohoho12345')
  408. })
  409. })
  410. describe('vms', function () {
  411. it('should allow the VM to use private child VMs', function () {
  412. var Child = Seed.extend({
  413. scope: {
  414. name: 'child'
  415. }
  416. })
  417. var Parent = Seed.extend({
  418. template: '<p>{{name}}</p><div sd-viewmodel="child">{{name}}</div>',
  419. scope: {
  420. name: 'dad'
  421. },
  422. vms: {
  423. child: Child
  424. }
  425. })
  426. var p = new Parent()
  427. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  428. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  429. })
  430. })
  431. describe('partials', function () {
  432. it('should allow the VM to use private partials', function () {
  433. var Test = Seed.extend({
  434. attributes: {
  435. 'sd-partial': 'test'
  436. },
  437. partials: {
  438. test: '<a>{{a}}</a><p>{{b}}</p>'
  439. },
  440. scope: {
  441. a: 'hi',
  442. b: 'ho'
  443. }
  444. })
  445. var t = new Test()
  446. assert.strictEqual(t.$el.querySelector('a').textContent, 'hi')
  447. assert.strictEqual(t.$el.querySelector('p').textContent, 'ho')
  448. })
  449. })
  450. describe('transitions', function () {
  451. // it('should be tested', function () {
  452. // assert.ok(false)
  453. // })
  454. })
  455. })
  456. })
  457. })