api.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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('component()', function () {
  89. var testId = 'api-vm-test',
  90. testId2 = testId + '2',
  91. opts = {
  92. className: 'hihi',
  93. scope: { hi: 'ok' }
  94. },
  95. Test = Seed.extend(opts),
  96. utils = require('seed/src/utils')
  97. it('should register a Component constructor', function () {
  98. Seed.component(testId, Test)
  99. assert.strictEqual(utils.components[testId], Test)
  100. })
  101. it('should also work with option objects', function () {
  102. Seed.component(testId2, opts)
  103. assert.ok(utils.components[testId2].prototype instanceof Seed)
  104. })
  105. it('should retrieve the VM if has only one arg', function () {
  106. assert.strictEqual(Seed.component(testId), Test)
  107. })
  108. it('should work with sd-component', function () {
  109. mock(testId, '<div sd-component="' + testId + '">{{hi}}</div>')
  110. var t = new Seed({ el: '#' + testId }),
  111. child = t.$el.querySelector('div')
  112. assert.strictEqual(child.className, 'hihi')
  113. assert.strictEqual(child.textContent, 'ok')
  114. mock(testId2, '<div sd-component="' + testId2 + '">{{hi}}</div>')
  115. var t2 = new Seed({ el: '#' + testId2 }),
  116. child2 = t2.$el.querySelector('div')
  117. assert.strictEqual(child2.className, 'hihi')
  118. assert.strictEqual(child2.textContent, 'ok')
  119. })
  120. })
  121. describe('partial()', function () {
  122. var testId = 'api-partial-test',
  123. partial = '<div class="partial-test"><a>{{hi}}</a></div><span>hahaha</span>',
  124. utils = require('seed/src/utils')
  125. it('should register the partial as a dom fragment', function () {
  126. Seed.partial(testId, partial)
  127. var converted = utils.partials[testId]
  128. assert.ok(converted instanceof window.DocumentFragment)
  129. assert.strictEqual(converted.querySelector('.partial-test a').innerHTML, '{{hi}}')
  130. assert.strictEqual(converted.querySelector('span').innerHTML, 'hahaha')
  131. })
  132. it('should retrieve the partial if has only one arg', function () {
  133. assert.strictEqual(utils.partials[testId], Seed.partial(testId))
  134. })
  135. it('should work with sd-partial as a directive', function () {
  136. var testId = 'api-partial-direcitve'
  137. Seed.partial(testId, partial)
  138. mock(testId, '<div class="directive" sd-partial="' + testId + '">hello</div>')
  139. var t = new Seed({
  140. el: '#' + testId,
  141. scope: { hi: 'hohoho' }
  142. })
  143. assert.strictEqual(t.$el.querySelector('.directive .partial-test a').textContent, 'hohoho')
  144. assert.strictEqual(t.$el.querySelector('.directive span').innerHTML, 'hahaha')
  145. })
  146. it('should work with sd-partial as an inline interpolation', function () {
  147. var testId = 'api-partial-inline'
  148. Seed.partial(testId, partial)
  149. mock(testId, '<div class="inline">{{>' + testId + '}}</div>')
  150. var t = new Seed({
  151. el: '#' + testId,
  152. scope: { hi: 'hohoho' }
  153. })
  154. assert.strictEqual(t.$el.querySelector('.inline .partial-test a').textContent, 'hohoho')
  155. assert.strictEqual(t.$el.querySelector('.inline span').innerHTML, 'hahaha')
  156. })
  157. })
  158. describe('transition()', function () {
  159. var testId = 'api-trans-test',
  160. transition = {},
  161. utils = require('seed/src/utils')
  162. it('should register a transition object', function () {
  163. Seed.transition(testId, transition)
  164. assert.strictEqual(utils.transitions[testId], transition)
  165. })
  166. it('should retrieve the transition if has only one arg', function () {
  167. assert.strictEqual(Seed.transition(testId), transition)
  168. })
  169. it('should work with sd-transition', function () {
  170. var enterCalled = false,
  171. leaveCalled = false
  172. Seed.transition('transition-api-test', {
  173. enter: function (el, done) {
  174. enterCalled = true
  175. done()
  176. },
  177. leave: function (el, done) {
  178. leaveCalled = true
  179. done()
  180. }
  181. })
  182. var t = new Seed({
  183. attributes: {
  184. 'sd-show': 'show',
  185. 'sd-transition': 'transition-api-test'
  186. },
  187. scope: {
  188. show: false
  189. }
  190. })
  191. document.body.appendChild(t.$el)
  192. t.show = true
  193. assert.ok(enterCalled)
  194. assert.strictEqual(t.$el.style.display, '')
  195. t.show = false
  196. assert.ok(leaveCalled)
  197. assert.strictEqual(t.$el.style.display, 'none')
  198. t.$destroy()
  199. })
  200. })
  201. describe('extend()', function () {
  202. it('should return a subclass of Seed', function () {
  203. var Test = Seed.extend({})
  204. assert.ok(Test.prototype instanceof Seed)
  205. })
  206. it('should allow further extensions', function () {
  207. var Parent = Seed.extend({
  208. scope: {
  209. test: 'hi'
  210. }
  211. })
  212. var Child = Parent.extend({
  213. scope: {
  214. test2: 'ho',
  215. test3: {
  216. hi: 1
  217. }
  218. }
  219. })
  220. assert.strictEqual(Child.super, Parent)
  221. var child = new Child({
  222. scope: {
  223. test3: {
  224. ho: 2
  225. }
  226. }
  227. })
  228. assert.strictEqual(child.test, 'hi')
  229. assert.strictEqual(child.test2, 'ho')
  230. // should overwrite past 1 level deep
  231. assert.strictEqual(child.test3.ho, 2)
  232. assert.notOk(child.test3.hi)
  233. })
  234. describe('Options', function () {
  235. describe('init', function () {
  236. it('should be called on the instance when instantiating', function () {
  237. var called = false,
  238. Test = Seed.extend({ init: function () {
  239. called = true
  240. }})
  241. new Test({ el: document.createElement('div') })
  242. assert.ok(called)
  243. })
  244. })
  245. describe('proto', function () {
  246. it('should be mixed to the exteded VM\'s prototype', function () {
  247. var mixins = {
  248. a: 1,
  249. b: 2,
  250. c: function () {}
  251. }
  252. var Test = Seed.extend({ proto: mixins })
  253. for (var key in mixins) {
  254. assert.strictEqual(Test.prototype[key], mixins[key])
  255. }
  256. })
  257. })
  258. describe('scope', function () {
  259. it('should be copied to each instance', function () {
  260. var testData = { a: 1 },
  261. Test = Seed.extend({
  262. scope: {
  263. test: testData
  264. }
  265. })
  266. var t1 = new Test(),
  267. t2 = new Test()
  268. assert.ok(t1.hasOwnProperty('test'))
  269. assert.strictEqual(t1.test, testData)
  270. assert.ok(t2.hasOwnProperty('test'))
  271. assert.strictEqual(t2.test, testData)
  272. })
  273. })
  274. describe('lazy', function () {
  275. it('should make text input fields only trigger on change', function () {
  276. var Test = Seed.extend({
  277. template: '<input type="text" sd-model="test">',
  278. lazy: true
  279. })
  280. var t = new Test({
  281. scope: {
  282. test: 'hi'
  283. }
  284. })
  285. var input = t.$el.querySelector('input')
  286. input.value = 'hohoho'
  287. input.dispatchEvent(mockHTMLEvent('input'))
  288. assert.strictEqual(t.test, 'hi')
  289. input.dispatchEvent(mockHTMLEvent('change'))
  290. assert.strictEqual(t.test, 'hohoho')
  291. })
  292. })
  293. describe('element options', function () {
  294. it('should not accept el as an extension option', function () {
  295. var el = document.createElement('div'),
  296. Test = Seed.extend({ el: el }),
  297. t = new Test()
  298. assert.notStrictEqual(t.$el, el)
  299. })
  300. it('should create el with options: tagName, id, className and attributes', function () {
  301. var Test = Seed.extend({
  302. tagName: 'p',
  303. id: 'extend-test',
  304. className: 'extend',
  305. attributes: {
  306. 'test': 'hi',
  307. 'sd-text': 'hoho'
  308. },
  309. scope: {
  310. hoho: 'what'
  311. }
  312. })
  313. var t = new Test()
  314. assert.strictEqual(t.$el.nodeName, 'P', 'tagName should match')
  315. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  316. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  317. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  318. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  319. })
  320. it('should ignore tagName when el is passed as an instance option', function () {
  321. var el = document.createElement('div'),
  322. Test = Seed.extend({
  323. tagName: 'p',
  324. id: 'extend-test',
  325. className: 'extend',
  326. attributes: {
  327. 'test': 'hi',
  328. 'sd-text': 'hoho'
  329. },
  330. scope: {
  331. hoho: 'what'
  332. }
  333. }),
  334. t = new Test({
  335. el: el
  336. })
  337. assert.strictEqual(t.$el, el, 'should use instance el')
  338. assert.notStrictEqual(t.$el.nodeName, 'P', 'tagName should NOT match')
  339. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  340. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  341. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  342. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  343. })
  344. })
  345. describe('template', function () {
  346. var raw = '<span>{{hello}}</span><a>haha</a>'
  347. it('should take direct string template and work', function () {
  348. var Test = Seed.extend({
  349. tagName: 'p',
  350. template: raw,
  351. scope: {
  352. hello: 'Ahaha'
  353. }
  354. }),
  355. vm = new Test(),
  356. text1 = vm.$el.querySelector('span').textContent,
  357. text2 = vm.$el.querySelector('a').textContent
  358. assert.strictEqual(vm.$el.nodeName, 'P')
  359. assert.strictEqual(text1, 'Ahaha')
  360. assert.strictEqual(text2, 'haha')
  361. })
  362. it('should take a #id and work', function () {
  363. var testId = 'template-test',
  364. tpl = document.createElement('script')
  365. tpl.id = testId
  366. tpl.type = 'text/template'
  367. tpl.innerHTML = raw
  368. document.getElementById('test').appendChild(tpl)
  369. var Test = Seed.extend({
  370. template: '#' + testId,
  371. scope: { hello: testId }
  372. })
  373. var t = new Test()
  374. assert.strictEqual(t.$el.querySelector('span').textContent, testId)
  375. })
  376. it('should be overwritable', function () {
  377. var Test = Seed.extend({
  378. template: '<div>this should not happen</div>'
  379. })
  380. var t = new Test({
  381. template: raw,
  382. scope: {
  383. hello: 'overwritten!'
  384. }
  385. })
  386. assert.strictEqual(t.$el.querySelector('span').textContent, 'overwritten!')
  387. })
  388. })
  389. describe('directives', function () {
  390. it('should allow the VM to use private directives', function () {
  391. var Test = Seed.extend({
  392. directives: {
  393. test: function (value) {
  394. this.el.innerHTML = value ? 'YES' : 'NO'
  395. }
  396. }
  397. })
  398. var t = new Test({
  399. attributes: {
  400. 'sd-test': 'ok'
  401. },
  402. scope: {
  403. ok: true
  404. }
  405. })
  406. assert.strictEqual(t.$el.innerHTML, 'YES')
  407. t.ok = false
  408. assert.strictEqual(t.$el.innerHTML, 'NO')
  409. })
  410. })
  411. describe('filters', function () {
  412. it('should allow the VM to use private filters', function () {
  413. var Test = Seed.extend({
  414. filters: {
  415. test: function (value) {
  416. return value + '12345'
  417. }
  418. }
  419. })
  420. var t = new Test({
  421. template: '{{hi | test}}',
  422. scope: {
  423. hi: 'hohoho'
  424. }
  425. })
  426. assert.strictEqual(t.$el.textContent, 'hohoho12345')
  427. })
  428. })
  429. describe('components', function () {
  430. it('should allow the VM to use private child VMs', function () {
  431. var Child = Seed.extend({
  432. scope: {
  433. name: 'child'
  434. }
  435. })
  436. var Parent = Seed.extend({
  437. template: '<p>{{name}}</p><div sd-component="child">{{name}}</div>',
  438. scope: {
  439. name: 'dad'
  440. },
  441. components: {
  442. child: Child
  443. }
  444. })
  445. var p = new Parent()
  446. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  447. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  448. })
  449. it('should work with plain option object', function () {
  450. var Parent = Seed.extend({
  451. template: '<p>{{name}}</p><div sd-component="child">{{name}}</div>',
  452. scope: {
  453. name: 'dad'
  454. },
  455. components: {
  456. child: {
  457. scope: {
  458. name: 'child'
  459. }
  460. }
  461. }
  462. })
  463. var p = new Parent()
  464. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  465. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  466. })
  467. })
  468. describe('partials', function () {
  469. it('should allow the VM to use private partials', function () {
  470. var Test = Seed.extend({
  471. attributes: {
  472. 'sd-partial': 'test'
  473. },
  474. partials: {
  475. test: '<a>{{a}}</a><p>{{b}}</p>'
  476. },
  477. scope: {
  478. a: 'hi',
  479. b: 'ho'
  480. }
  481. })
  482. var t = new Test()
  483. assert.strictEqual(t.$el.querySelector('a').textContent, 'hi')
  484. assert.strictEqual(t.$el.querySelector('p').textContent, 'ho')
  485. })
  486. })
  487. describe('teardown', function () {
  488. it('should be called when a vm is destroyed', function () {
  489. var called = false
  490. var Test = Seed.extend({
  491. teardown: function () {
  492. called = true
  493. }
  494. })
  495. var test = new Test()
  496. test.$destroy()
  497. assert.ok(called)
  498. })
  499. })
  500. describe('transitions', function () {
  501. it('should get called during transitions', function () {
  502. var enterCalled = false,
  503. leaveCalled = false
  504. var t = new Seed({
  505. attributes: {
  506. 'sd-show': 'show',
  507. 'sd-transition': 'test'
  508. },
  509. transitions: {
  510. test: {
  511. enter: function (el, done) {
  512. enterCalled = true
  513. done()
  514. },
  515. leave: function (el, done) {
  516. leaveCalled = true
  517. done()
  518. }
  519. }
  520. },
  521. scope: {
  522. show: false
  523. }
  524. })
  525. document.body.appendChild(t.$el)
  526. t.show = true
  527. assert.ok(enterCalled)
  528. assert.strictEqual(t.$el.style.display, '')
  529. t.show = false
  530. assert.ok(leaveCalled)
  531. assert.strictEqual(t.$el.style.display, 'none')
  532. t.$destroy()
  533. })
  534. })
  535. })
  536. })
  537. })