directives.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Only tests directives in `src/directives/index.js`
  3. * and the non-delegated case for `sd-on`
  4. *
  5. * The combination of `sd-repeat` and `sd-on` are covered in
  6. * the E2E test case for repeated items.
  7. */
  8. describe('UNIT: Directives', function () {
  9. describe('attr', function () {
  10. var dir = mockDirective('attr')
  11. dir.arg = 'href'
  12. it('should set an attribute', function () {
  13. var url = 'http://a.b.com'
  14. dir.update(url)
  15. assert.strictEqual(dir.el.getAttribute('href'), url)
  16. })
  17. })
  18. describe('text', function () {
  19. var dir = mockDirective('text')
  20. it('should work with a string', function () {
  21. dir.update('hallo')
  22. assert.strictEqual(dir.el.textContent, 'hallo')
  23. })
  24. it('should work with a number', function () {
  25. dir.update(12345)
  26. assert.strictEqual(dir.el.textContent, '12345')
  27. })
  28. it('should work with booleans', function () {
  29. dir.update(true)
  30. assert.strictEqual(dir.el.textContent, 'true')
  31. })
  32. it('should be empty with other stuff', function () {
  33. dir.update(null)
  34. assert.strictEqual(dir.el.textContent, '')
  35. dir.update(undefined)
  36. assert.strictEqual(dir.el.textContent, '')
  37. dir.update({a:123})
  38. assert.strictEqual(dir.el.textContent, '')
  39. dir.update(function () {})
  40. assert.strictEqual(dir.el.textContent, '')
  41. })
  42. })
  43. describe('html', function () {
  44. var dir = mockDirective('html')
  45. it('should work with a string', function () {
  46. dir.update('hi!!!')
  47. assert.strictEqual(dir.el.innerHTML, 'hi!!!')
  48. dir.update('<span>haha</span><a>lol</a>')
  49. assert.strictEqual(dir.el.querySelector('span').textContent, 'haha')
  50. })
  51. it('should work with a number', function () {
  52. dir.update(12345)
  53. assert.strictEqual(dir.el.innerHTML, '12345')
  54. })
  55. it('should work with booleans', function () {
  56. dir.update(true)
  57. assert.strictEqual(dir.el.textContent, 'true')
  58. })
  59. it('should be empty with other stuff', function () {
  60. dir.update(null)
  61. assert.strictEqual(dir.el.innerHTML, '')
  62. dir.update(undefined)
  63. assert.strictEqual(dir.el.innerHTML, '')
  64. dir.update({a:123})
  65. assert.strictEqual(dir.el.innerHTML, '')
  66. dir.update(function () {})
  67. assert.strictEqual(dir.el.innerHTML, '')
  68. })
  69. })
  70. describe('style', function () {
  71. var dir = mockDirective('style')
  72. it('should convert the arg from dash style to camel case', function () {
  73. dir.arg = 'font-family'
  74. dir.bind()
  75. assert.strictEqual(dir.arg, 'fontFamily')
  76. dir.arg = '-webkit-transform'
  77. dir.bind()
  78. assert.strictEqual(dir.arg, 'webkitTransform')
  79. })
  80. it('should update the element style', function () {
  81. dir.update('rotate(20deg)')
  82. assert.strictEqual(dir.el.style.webkitTransform, 'rotate(20deg)')
  83. })
  84. })
  85. describe('show', function () {
  86. var dir = mockDirective('show')
  87. it('should be default value when value is truthy', function () {
  88. dir.update(1)
  89. assert.strictEqual(dir.el.style.display, '')
  90. dir.update('hi!')
  91. assert.strictEqual(dir.el.style.display, '')
  92. dir.update(true)
  93. assert.strictEqual(dir.el.style.display, '')
  94. dir.update({})
  95. assert.strictEqual(dir.el.style.display, '')
  96. dir.update(function () {})
  97. assert.strictEqual(dir.el.style.display, '')
  98. })
  99. it('should be none when value is falsy', function () {
  100. dir.update(0)
  101. assert.strictEqual(dir.el.style.display, 'none')
  102. dir.update('')
  103. assert.strictEqual(dir.el.style.display, 'none')
  104. dir.update(false)
  105. assert.strictEqual(dir.el.style.display, 'none')
  106. dir.update(null)
  107. assert.strictEqual(dir.el.style.display, 'none')
  108. dir.update(undefined)
  109. assert.strictEqual(dir.el.style.display, 'none')
  110. })
  111. })
  112. describe('visible', function () {
  113. var dir = mockDirective('visible')
  114. it('should be default value when value is truthy', function () {
  115. dir.update(1)
  116. assert.strictEqual(dir.el.style.visibility, '')
  117. dir.update('hi!')
  118. assert.strictEqual(dir.el.style.visibility, '')
  119. dir.update(true)
  120. assert.strictEqual(dir.el.style.visibility, '')
  121. dir.update({})
  122. assert.strictEqual(dir.el.style.visibility, '')
  123. dir.update(function () {})
  124. assert.strictEqual(dir.el.style.visibility, '')
  125. })
  126. it('should be hidden when value is falsy', function () {
  127. dir.update(0)
  128. assert.strictEqual(dir.el.style.visibility, 'hidden')
  129. dir.update('')
  130. assert.strictEqual(dir.el.style.visibility, 'hidden')
  131. dir.update(false)
  132. assert.strictEqual(dir.el.style.visibility, 'hidden')
  133. dir.update(null)
  134. assert.strictEqual(dir.el.style.visibility, 'hidden')
  135. dir.update(undefined)
  136. assert.strictEqual(dir.el.style.visibility, 'hidden')
  137. })
  138. })
  139. describe('class', function () {
  140. it('should set class to the value if it has no arg', function () {
  141. var dir = mockDirective('class')
  142. dir.update('test')
  143. assert.ok(dir.el.classList.contains('test'))
  144. dir.update('hoho')
  145. assert.ok(!dir.el.classList.contains('test'))
  146. assert.ok(dir.el.classList.contains('hoho'))
  147. })
  148. it('should add/remove class based on truthy/falsy if it has an arg', function () {
  149. var dir = mockDirective('class')
  150. dir.arg = 'test'
  151. dir.update(true)
  152. assert.ok(dir.el.classList.contains('test'))
  153. dir.update(false)
  154. assert.ok(!dir.el.classList.contains('test'))
  155. })
  156. })
  157. describe('model', function () {
  158. describe('input[checkbox]', function () {
  159. var dir = mockDirective('model', 'input', 'checkbox')
  160. dir.bind()
  161. before(function () {
  162. document.body.appendChild(dir.el)
  163. })
  164. it('should set checked on update()', function () {
  165. dir.update(true)
  166. assert.ok(dir.el.checked)
  167. dir.update(false)
  168. assert.ok(!dir.el.checked)
  169. })
  170. it('should trigger vm.$set when clicked', function () {
  171. var triggered = false
  172. dir.key = 'foo'
  173. dir.vm = { $set: function (key, val) {
  174. assert.strictEqual(key, 'foo')
  175. assert.strictEqual(val, true)
  176. triggered = true
  177. }}
  178. dir.el.dispatchEvent(mockMouseEvent('click'))
  179. assert.ok(triggered)
  180. })
  181. it('should remove event listener with unbind()', function () {
  182. var removed = true
  183. dir.vm.$set = function () {
  184. removed = false
  185. }
  186. dir.unbind()
  187. dir.el.dispatchEvent(mockMouseEvent('click'))
  188. assert.ok(removed)
  189. })
  190. after(function () {
  191. document.body.removeChild(dir.el)
  192. })
  193. })
  194. describe('input[radio]', function () {
  195. var dir1 = mockDirective('model', 'input', 'radio'),
  196. dir2 = mockDirective('model', 'input', 'radio')
  197. dir1.el.name = 'input-radio'
  198. dir2.el.name = 'input-radio'
  199. dir1.el.value = '12345'
  200. dir2.el.value = '54321'
  201. dir1.bind()
  202. dir2.bind()
  203. before(function () {
  204. document.body.appendChild(dir1.el)
  205. document.body.appendChild(dir2.el)
  206. })
  207. it('should set el.checked on update()', function () {
  208. assert.notOk(dir1.el.checked)
  209. dir1.update(12345)
  210. assert.ok(dir1.el.checked)
  211. })
  212. it('should trigger vm.$set when clicked', function () {
  213. var triggered = false
  214. dir2.key = 'radio'
  215. dir2.vm = { $set: function (key, val) {
  216. triggered = true
  217. assert.strictEqual(key, 'radio')
  218. assert.strictEqual(val, dir2.el.value)
  219. }}
  220. dir2.el.dispatchEvent(mockMouseEvent('click'))
  221. assert.ok(triggered)
  222. assert.ok(dir2.el.checked)
  223. assert.notOk(dir1.el.checked)
  224. })
  225. it('should remove listeners on unbind()', function () {
  226. var removed = true
  227. dir1.vm = { $set: function () {
  228. removed = false
  229. }}
  230. dir1.unbind()
  231. dir1.el.dispatchEvent(mockMouseEvent('click'))
  232. assert.ok(removed)
  233. })
  234. after(function () {
  235. document.body.removeChild(dir1.el)
  236. document.body.removeChild(dir2.el)
  237. })
  238. })
  239. describe('select', function () {
  240. var dir = mockDirective('model', 'select'),
  241. o1 = document.createElement('option'),
  242. o2 = document.createElement('option')
  243. o1.value = 0
  244. o2.value = 1
  245. dir.el.appendChild(o1)
  246. dir.el.appendChild(o2)
  247. dir.bind()
  248. before(function () {
  249. document.body.appendChild(dir.el)
  250. })
  251. it('should set value on update()', function () {
  252. dir.update(0)
  253. assert.strictEqual(dir.el.value, '0')
  254. })
  255. it('should trigger vm.$set when value is changed', function () {
  256. var triggered = false
  257. dir.key = 'select'
  258. dir.vm = { $set: function (key, val) {
  259. triggered = true
  260. assert.strictEqual(key, 'select')
  261. assert.equal(val, 1)
  262. }}
  263. dir.el.options.selectedIndex = 1
  264. dir.el.dispatchEvent(mockHTMLEvent('change'))
  265. assert.ok(triggered)
  266. })
  267. it('should remove listener on unbind()', function () {
  268. var removed = true
  269. dir.vm = { $set: function () {
  270. removed = false
  271. }}
  272. dir.unbind()
  273. dir.el.dispatchEvent(mockHTMLEvent('change'))
  274. assert.ok(removed)
  275. })
  276. after(function () {
  277. document.body.removeChild(dir.el)
  278. })
  279. })
  280. describe('input[text] and others', function () {
  281. var dir = mockDirective('model', 'input', 'text')
  282. dir.bind()
  283. before(function () {
  284. document.body.appendChild(dir.el)
  285. })
  286. it('should set the value on update()', function () {
  287. dir.update('foobar')
  288. assert.strictEqual(dir.el.value, 'foobar')
  289. })
  290. // `lazy` option is tested in the API suite
  291. it('should trigger vm.$set when value is changed via input', function () {
  292. var triggered = false
  293. dir.key = 'foo'
  294. dir.vm = { $set: function (key, val) {
  295. assert.strictEqual(key, 'foo')
  296. assert.strictEqual(val, 'bar')
  297. triggered = true
  298. }}
  299. dir.el.value = 'bar'
  300. dir.el.dispatchEvent(mockHTMLEvent('input'))
  301. assert.ok(triggered)
  302. })
  303. it('should remove event listener with unbind()', function () {
  304. var removed = true
  305. dir.vm.$set = function () {
  306. removed = false
  307. }
  308. dir.unbind()
  309. dir.el.dispatchEvent(mockHTMLEvent('input'))
  310. assert.ok(removed)
  311. })
  312. after(function () {
  313. document.body.removeChild(dir.el)
  314. })
  315. })
  316. })
  317. describe('if', function () {
  318. it('should remain detached if it was detached during bind() and never attached', function () {
  319. var dir = mockDirective('if')
  320. dir.bind()
  321. dir.update(true)
  322. assert.notOk(dir.el.parentNode)
  323. dir.update(false)
  324. assert.notOk(dir.el.parentNode)
  325. })
  326. it('should remove el and insert ref when value is falsy', function () {
  327. var dir = mockDirective('if'),
  328. parent = document.createElement('div')
  329. parent.appendChild(dir.el)
  330. dir.bind()
  331. dir.update(false)
  332. assert.notOk(dir.el.parentNode)
  333. assert.notOk(parent.contains(dir.el))
  334. // phantomJS weird bug:
  335. // Node.contains() returns false when argument is a comment node.
  336. assert.strictEqual(dir.ref.parentNode, parent)
  337. })
  338. it('should append el and remove ref when value is truthy', function () {
  339. var dir = mockDirective('if'),
  340. parent = document.createElement('div')
  341. parent.appendChild(dir.el)
  342. dir.bind()
  343. dir.update(false)
  344. dir.update(true)
  345. assert.strictEqual(dir.el.parentNode, parent)
  346. assert.ok(parent.contains(dir.el))
  347. assert.notOk(parent.contains(dir.ref))
  348. })
  349. it('should work even if it was detached during bind()', function () {
  350. var dir = mockDirective('if')
  351. dir.bind()
  352. var parent = document.createElement('div')
  353. parent.appendChild(dir.el)
  354. dir.update(false)
  355. assert.strictEqual(dir.parent, parent)
  356. assert.notOk(dir.el.parentNode)
  357. assert.notOk(parent.contains(dir.el))
  358. assert.strictEqual(dir.ref.parentNode, parent)
  359. dir.update(true)
  360. assert.strictEqual(dir.el.parentNode, parent)
  361. assert.ok(parent.contains(dir.el))
  362. assert.notOk(parent.contains(dir.ref))
  363. })
  364. })
  365. describe('on (non-delegated only)', function () {
  366. var dir = mockDirective('on')
  367. dir.arg = 'click'
  368. before(function () {
  369. document.body.appendChild(dir.el)
  370. })
  371. it('should set the handler to be triggered by arg through update()', function () {
  372. var triggered = false
  373. dir.update(function () {
  374. triggered = true
  375. })
  376. dir.el.dispatchEvent(mockMouseEvent('click'))
  377. assert.ok(triggered)
  378. })
  379. it('should wrap the handler to supply expected args', function () {
  380. var vm = dir.binding.compiler.vm, // owner VM
  381. e = mockMouseEvent('click'), // original event
  382. triggered = false
  383. dir.update(function (ev) {
  384. assert.strictEqual(this, vm, 'handler should be called on owner VM')
  385. assert.strictEqual(ev, e, 'event should be passed in')
  386. assert.strictEqual(ev.vm, dir.vm)
  387. triggered = true
  388. })
  389. dir.el.dispatchEvent(e)
  390. assert.ok(triggered)
  391. })
  392. it('should remove previous handler when update() a new handler', function () {
  393. var triggered1 = false,
  394. triggered2 = false
  395. dir.update(function () {
  396. triggered1 = true
  397. })
  398. dir.update(function () {
  399. triggered2 = true
  400. })
  401. dir.el.dispatchEvent(mockMouseEvent('click'))
  402. assert.notOk(triggered1)
  403. assert.ok(triggered2)
  404. })
  405. it('should remove the handler in unbind()', function () {
  406. var triggered = false
  407. dir.update(function () {
  408. triggered = true
  409. })
  410. dir.unbind()
  411. dir.el.dispatchEvent(mockMouseEvent('click'))
  412. assert.notOk(triggered)
  413. assert.strictEqual(dir.handler, null, 'should remove reference to handler')
  414. assert.strictEqual(dir.el.sd_viewmodel, null, 'should remove reference to VM on the element')
  415. })
  416. it('should not use delegation if the event is blur or focus', function () {
  417. var dir = mockDirective('on', 'input'),
  418. triggerCount = 0,
  419. handler = function () {
  420. triggerCount++
  421. }
  422. document.body.appendChild(dir.el)
  423. dir.arg = 'focus'
  424. dir.update(handler)
  425. dir.el.dispatchEvent(mockHTMLEvent('focus'))
  426. assert.strictEqual(triggerCount, 1)
  427. dir.arg = 'blur'
  428. dir.update(handler)
  429. dir.el.dispatchEvent(mockHTMLEvent('blur'))
  430. assert.strictEqual(triggerCount, 2)
  431. document.body.removeChild(dir.el)
  432. })
  433. after(function () {
  434. document.body.removeChild(dir.el)
  435. })
  436. })
  437. describe('pre', function () {
  438. it('should skip compilation', function () {
  439. var testId = 'pre-test'
  440. mock(testId, '<span sd-pre><strong>{{lol}}</strong><a sd-text="hi"></a></span>')
  441. var t = new Seed({
  442. el: '#' + testId,
  443. scope: {
  444. lol: 'heyhey',
  445. hi: 'hohoho'
  446. }
  447. })
  448. assert.strictEqual(t.$el.querySelector('strong').textContent, '{{lol}}')
  449. assert.strictEqual(t.$el.querySelector('a').textContent, '')
  450. assert.ok(t.$el.querySelector('a').hasAttribute('sd-text'))
  451. })
  452. })
  453. describe('id', function () {
  454. it('should register a VM isntance on its parent\'s $', function () {
  455. var called = false
  456. var Child = Seed.extend({
  457. proto: {
  458. test: function () {
  459. called = true
  460. }
  461. }
  462. })
  463. var t = new Seed({
  464. template: '<div sd-viewmodel="child" sd-id="hihi"></div>',
  465. viewmodels: {
  466. child: Child
  467. }
  468. })
  469. assert.ok(t.$.hihi instanceof Child)
  470. t.$.hihi.test()
  471. assert.ok(called)
  472. t.$.hihi.$destroy()
  473. assert.notOk('hihi' in t.$)
  474. })
  475. })
  476. })
  477. function mockDirective (dirName, tag, type) {
  478. var dir = Seed.directive(dirName),
  479. ret = {
  480. binding: { compiler: { vm: {} } },
  481. compiler: { vm: {}, options: {} },
  482. el: document.createElement(tag || 'div')
  483. }
  484. if (typeof dir === 'function') {
  485. ret.update = dir
  486. } else {
  487. for (var key in dir) {
  488. ret[key] = dir[key]
  489. }
  490. }
  491. if (tag === 'input') ret.el.type = type || 'text'
  492. return ret
  493. }