directives.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 be empty with other stuff', function () {
  29. dir.update(null)
  30. assert.strictEqual(dir.el.textContent, '')
  31. dir.update(false)
  32. assert.strictEqual(dir.el.textContent, '')
  33. dir.update(true)
  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 be empty with other stuff', function () {
  56. dir.update(null)
  57. assert.strictEqual(dir.el.innerHTML, '')
  58. dir.update(false)
  59. assert.strictEqual(dir.el.innerHTML, '')
  60. dir.update(true)
  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('focus', function () {
  140. var dir = mockDirective('focus', 'input')
  141. it('should focus on the element', function (done) {
  142. var focused = false
  143. // the el needs to be in the dom and visible to actually
  144. // trigger the focus event
  145. document.body.appendChild(dir.el)
  146. dir.el.addEventListener('focus', function () {
  147. focused = true
  148. })
  149. dir.update(true)
  150. // the focus event has a 0ms timeout to make it async
  151. setTimeout(function () {
  152. assert.ok(focused)
  153. document.body.removeChild(dir.el)
  154. done()
  155. }, 0)
  156. })
  157. })
  158. describe('class', function () {
  159. it('should set class to the value if it has no arg', function () {
  160. var dir = mockDirective('class')
  161. dir.update('test')
  162. assert.ok(dir.el.classList.contains('test'))
  163. dir.update('hoho')
  164. assert.ok(!dir.el.classList.contains('test'))
  165. assert.ok(dir.el.classList.contains('hoho'))
  166. })
  167. it('should add/remove class based on truthy/falsy if it has an arg', function () {
  168. var dir = mockDirective('class')
  169. dir.arg = 'test'
  170. dir.update(true)
  171. assert.ok(dir.el.classList.contains('test'))
  172. dir.update(false)
  173. assert.ok(!dir.el.classList.contains('test'))
  174. })
  175. })
  176. describe('model', function () {
  177. describe('input[checkbox]', function () {
  178. var dir = mockDirective('model', 'input', 'checkbox')
  179. dir.bind()
  180. before(function () {
  181. document.body.appendChild(dir.el)
  182. })
  183. it('should set checked on update()', function () {
  184. dir.update(true)
  185. assert.ok(dir.el.checked)
  186. dir.update(false)
  187. assert.ok(!dir.el.checked)
  188. })
  189. it('should trigger vm.$set when clicked', function () {
  190. var triggered = false
  191. dir.key = 'foo'
  192. dir.vm = { $set: function (key, val) {
  193. assert.strictEqual(key, 'foo')
  194. assert.strictEqual(val, true)
  195. triggered = true
  196. }}
  197. dir.el.dispatchEvent(mockMouseEvent('click'))
  198. assert.ok(triggered)
  199. })
  200. it('should remove event listener with unbind()', function () {
  201. var removed = true
  202. dir.vm.$set = function () {
  203. removed = false
  204. }
  205. dir.unbind()
  206. dir.el.dispatchEvent(mockMouseEvent('click'))
  207. assert.ok(removed)
  208. })
  209. after(function () {
  210. document.body.removeChild(dir.el)
  211. })
  212. })
  213. describe('input[radio]', function () {
  214. var dir1 = mockDirective('model', 'input', 'radio'),
  215. dir2 = mockDirective('model', 'input', 'radio')
  216. dir1.el.name = 'input-radio'
  217. dir2.el.name = 'input-radio'
  218. dir1.el.value = '12345'
  219. dir2.el.value = '54321'
  220. dir1.bind()
  221. dir2.bind()
  222. before(function () {
  223. document.body.appendChild(dir1.el)
  224. document.body.appendChild(dir2.el)
  225. })
  226. it('should set el.checked on update()', function () {
  227. assert.notOk(dir1.el.checked)
  228. dir1.update(12345)
  229. assert.ok(dir1.el.checked)
  230. })
  231. it('should trigger vm.$set when clicked', function () {
  232. var triggered = false
  233. dir2.key = 'radio'
  234. dir2.vm = { $set: function (key, val) {
  235. triggered = true
  236. assert.strictEqual(key, 'radio')
  237. assert.strictEqual(val, dir2.el.value)
  238. }}
  239. dir2.el.dispatchEvent(mockMouseEvent('click'))
  240. assert.ok(triggered)
  241. assert.ok(dir2.el.checked)
  242. assert.notOk(dir1.el.checked)
  243. })
  244. it('should remove listeners on unbind()', function () {
  245. var removed = true
  246. dir1.vm = { $set: function () {
  247. removed = false
  248. }}
  249. dir1.unbind()
  250. dir1.el.dispatchEvent(mockMouseEvent('click'))
  251. assert.ok(removed)
  252. })
  253. after(function () {
  254. document.body.removeChild(dir1.el)
  255. document.body.removeChild(dir2.el)
  256. })
  257. })
  258. describe('select', function () {
  259. var dir = mockDirective('model', 'select')
  260. dir.el.innerHTML = '<option>0</option><option>1</option>'
  261. dir.bind()
  262. before(function () {
  263. document.body.appendChild(dir.el)
  264. })
  265. it('should set value on update()', function () {
  266. dir.update(0)
  267. assert.strictEqual(dir.el.value, '0')
  268. })
  269. it('should trigger vm.$set when value is changed', function () {
  270. var triggered = false
  271. dir.key = 'select'
  272. dir.vm = { $set: function (key, val) {
  273. triggered = true
  274. assert.strictEqual(key, 'select')
  275. assert.equal(val, 1)
  276. }}
  277. dir.el.options.selectedIndex = 1
  278. dir.el.dispatchEvent(mockChangeEvent())
  279. assert.ok(triggered)
  280. })
  281. it('should remove listener on unbind()', function () {
  282. var removed = true
  283. dir.vm = { $set: function () {
  284. removed = false
  285. }}
  286. dir.unbind()
  287. dir.el.dispatchEvent(mockChangeEvent())
  288. assert.ok(removed)
  289. })
  290. after(function () {
  291. document.body.removeChild(dir.el)
  292. })
  293. })
  294. describe('input[text] and others', function () {
  295. var dir = mockDirective('model', 'input', 'email')
  296. dir.bind()
  297. before(function () {
  298. document.body.appendChild(dir.el)
  299. })
  300. it('should set the value on update()', function () {
  301. dir.update('foobar')
  302. assert.strictEqual(dir.el.value, 'foobar')
  303. })
  304. // `lazy` option is tested in the API suite
  305. it('should trigger vm.$set when value is changed via keyup', function () {
  306. var triggered = false
  307. dir.key = 'foo'
  308. dir.vm = { $set: function (key, val) {
  309. assert.strictEqual(key, 'foo')
  310. assert.strictEqual(val, 'bar')
  311. triggered = true
  312. }}
  313. dir.el.value = 'bar'
  314. dir.el.dispatchEvent(mockKeyEvent('keyup'))
  315. assert.ok(triggered)
  316. })
  317. it('should remove event listener with unbind()', function () {
  318. var removed = true
  319. dir.vm.$set = function () {
  320. removed = false
  321. }
  322. dir.unbind()
  323. dir.el.dispatchEvent(mockKeyEvent('keyup'))
  324. assert.ok(removed)
  325. })
  326. after(function () {
  327. document.body.removeChild(dir.el)
  328. })
  329. })
  330. })
  331. describe('if', function () {
  332. it('should remain detached if it was detached during bind() and never attached', function () {
  333. var dir = mockDirective('if')
  334. dir.bind()
  335. dir.update(true)
  336. assert.notOk(dir.el.parentNode)
  337. dir.update(false)
  338. assert.notOk(dir.el.parentNode)
  339. })
  340. it('should remove el and insert ref when value is falsy', function () {
  341. var dir = mockDirective('if'),
  342. parent = document.createElement('div')
  343. parent.appendChild(dir.el)
  344. dir.bind()
  345. dir.update(false)
  346. assert.notOk(dir.el.parentNode)
  347. assert.notOk(parent.contains(dir.el))
  348. // phantomJS weird bug:
  349. // Node.contains() returns false when argument is a comment node.
  350. assert.strictEqual(dir.ref.parentNode, parent)
  351. })
  352. it('should append el and remove ref when value is truthy', function () {
  353. var dir = mockDirective('if'),
  354. parent = document.createElement('div')
  355. parent.appendChild(dir.el)
  356. dir.bind()
  357. dir.update(false)
  358. dir.update(true)
  359. assert.strictEqual(dir.el.parentNode, parent)
  360. assert.ok(parent.contains(dir.el))
  361. assert.notOk(parent.contains(dir.ref))
  362. })
  363. it('should work even if it was detached during bind()', function () {
  364. var dir = mockDirective('if')
  365. dir.bind()
  366. var parent = document.createElement('div')
  367. parent.appendChild(dir.el)
  368. dir.update(false)
  369. assert.strictEqual(dir.parent, parent)
  370. assert.notOk(dir.el.parentNode)
  371. assert.notOk(parent.contains(dir.el))
  372. assert.strictEqual(dir.ref.parentNode, parent)
  373. dir.update(true)
  374. assert.strictEqual(dir.el.parentNode, parent)
  375. assert.ok(parent.contains(dir.el))
  376. assert.notOk(parent.contains(dir.ref))
  377. })
  378. })
  379. describe('on (non-delegated only)', function () {
  380. var dir = mockDirective('on')
  381. dir.arg = 'click'
  382. it('should set the handler to be triggered by arg through update()', function () {
  383. var triggered = false
  384. dir.update(function () {
  385. triggered = true
  386. })
  387. dir.el.dispatchEvent(mockMouseEvent('click'))
  388. assert.ok(triggered)
  389. })
  390. it('should wrap the handler to supply expected args', function () {
  391. var vm = dir.binding.compiler.vm, // owner VM
  392. e = mockMouseEvent('click'), // original event
  393. triggered = false
  394. dir.update(function (ev) {
  395. assert.strictEqual(this, vm, 'handler should be called on owner VM')
  396. assert.strictEqual(ev, e, 'event should be passed in')
  397. assert.strictEqual(ev.vm, dir.vm)
  398. triggered = true
  399. })
  400. dir.el.dispatchEvent(e)
  401. assert.ok(triggered)
  402. })
  403. it('should remove previous handler when update() a new handler', function () {
  404. var triggered1 = false,
  405. triggered2 = false
  406. dir.update(function () {
  407. triggered1 = true
  408. })
  409. dir.update(function () {
  410. triggered2 = true
  411. })
  412. dir.el.dispatchEvent(mockMouseEvent('click'))
  413. assert.notOk(triggered1)
  414. assert.ok(triggered2)
  415. })
  416. it('should remove the handler in unbind()', function () {
  417. var triggered = false
  418. dir.update(function () {
  419. triggered = true
  420. })
  421. dir.unbind()
  422. dir.el.dispatchEvent(mockMouseEvent('click'))
  423. assert.notOk(triggered)
  424. assert.strictEqual(dir.handler, null, 'should remove reference to handler')
  425. assert.strictEqual(dir.el.sd_viewmodel, null, 'should remove reference to VM on the element')
  426. })
  427. it('should not use delegation if the event is blur or focus', function () {
  428. var dir = mockDirective('on', 'input'),
  429. triggerCount = 0,
  430. handler = function () {
  431. triggerCount++
  432. }
  433. document.body.appendChild(dir.el)
  434. dir.arg = 'focus'
  435. dir.update(handler)
  436. dir.el.focus()
  437. assert.strictEqual(triggerCount, 1)
  438. dir.arg = 'blur'
  439. dir.update(handler)
  440. dir.el.blur()
  441. assert.strictEqual(triggerCount, 2)
  442. document.body.removeChild(dir.el)
  443. })
  444. })
  445. })
  446. function mockDirective (dirName, tag, type) {
  447. var dir = Seed.directive(dirName),
  448. ret = {
  449. binding: { compiler: { vm: {} } },
  450. compiler: { vm: {}, options: {} },
  451. el: document.createElement(tag || 'div')
  452. }
  453. if (typeof dir === 'function') {
  454. ret.update = dir
  455. } else {
  456. for (var key in dir) {
  457. ret[key] = dir[key]
  458. }
  459. }
  460. if (tag === 'input') ret.el.type = type || 'text'
  461. return ret
  462. }