todomvc.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* global __utils__ */
  2. casper.test.begin('todomvc', 69, function (test) {
  3. casper
  4. .start('../../examples/todomvc/index.html')
  5. .then(function () {
  6. test.assertNotVisible('#main', '#main should be hidden')
  7. test.assertNotVisible('#footer', '#footer should be hidden')
  8. test.assertElementCount('#filters .selected', 1, 'should have one filter selected')
  9. test.assertSelectorHasText('#filters .selected', 'All', 'default filter should be "All"')
  10. })
  11. // let's add a new item -----------------------------------------------
  12. .then(function () {
  13. casper.sendKeys('#new-todo', 'test')
  14. })
  15. .then(function () {
  16. // wait before hitting enter
  17. // so v-model unlocks
  18. createNewItem()
  19. })
  20. .then(function () {
  21. test.assertElementCount('.todo', 1, 'new item should be created')
  22. test.assertNotVisible('.todo .edit', 'new item edit box should be hidden')
  23. test.assertSelectorHasText('.todo label', 'test', 'new item should have correct label text')
  24. test.assertSelectorHasText('#todo-count strong', '1', 'remaining count should be 1')
  25. test.assertEvalEquals(function () {
  26. return __utils__.findOne('.todo .toggle').checked
  27. }, false, 'new item toggle should not be checked')
  28. test.assertVisible('#main', '#main should now be visible')
  29. test.assertVisible('#footer', '#footer should now be visible')
  30. test.assertNotVisible('#clear-completed', '#clear-completed should be hidden')
  31. test.assertField({type:'css',path:'#new-todo'}, '', 'new todo input should be reset')
  32. })
  33. // add another item ---------------------------------------------------
  34. .then(function () {
  35. createNewItem('test2')
  36. })
  37. .then(function () {
  38. test.assertElementCount('.todo', 2, 'should have 2 items now')
  39. test.assertSelectorHasText('.todo:nth-child(2) label', 'test2', 'new item should have correct label text')
  40. test.assertSelectorHasText('#todo-count strong', '2', 'remaining count should be 2')
  41. })
  42. // mark one item as completed -----------------------------------------
  43. .thenClick('.todo .toggle', function () {
  44. test.assertElementCount('.todo.completed', 1, 'should have 1 item completed')
  45. test.assertEval(function () {
  46. return __utils__.findOne('.todo').classList.contains('completed')
  47. }, 'it should be the first one')
  48. test.assertSelectorHasText('#todo-count strong', '1', 'remaining count should be 1')
  49. test.assertVisible('#clear-completed', '#clear-completed should now be visible')
  50. test.assertSelectorHasText('#clear-completed', 'Clear completed (1)')
  51. })
  52. // add yet another item -----------------------------------------------
  53. .then(function () {
  54. createNewItem('test3')
  55. })
  56. .then(function () {
  57. test.assertElementCount('.todo', 3, 'should have 3 items now')
  58. test.assertSelectorHasText('.todo:nth-child(3) label', 'test3', 'new item should have correct label text')
  59. test.assertSelectorHasText('#todo-count strong', '2', 'remaining count should be 2')
  60. })
  61. // add moreeee, now we assume they all work properly ------------------
  62. .then(function () {
  63. createNewItem('test4')
  64. createNewItem('test5')
  65. })
  66. .then(function () {
  67. test.assertElementCount('.todo', 5, 'should have 5 items now')
  68. test.assertSelectorHasText('#todo-count strong', '4', 'remaining count should be 4')
  69. })
  70. // check more as completed --------------------------------------------
  71. .then(function () {
  72. this.click('.todo:nth-child(4) .toggle')
  73. this.click('.todo:nth-child(5) .toggle')
  74. })
  75. .then(function () {
  76. test.assertElementCount('.todo.completed', 3, 'should have 3 item completed')
  77. test.assertSelectorHasText('#clear-completed', 'Clear completed (3)')
  78. test.assertSelectorHasText('#todo-count strong', '2', 'remaining count should be 2')
  79. })
  80. // remove a completed item --------------------------------------------
  81. .thenClick('.todo:nth-child(1) .destroy', function () {
  82. test.assertElementCount('.todo', 4, 'should have 4 items now')
  83. test.assertElementCount('.todo.completed', 2, 'should have 2 item completed')
  84. test.assertSelectorHasText('#clear-completed', 'Clear completed (2)')
  85. test.assertSelectorHasText('#todo-count strong', '2', 'remaining count should be 2')
  86. })
  87. // remove a incompleted item ------------------------------------------
  88. .thenClick('.todo:nth-child(2) .destroy', function () {
  89. test.assertElementCount('.todo', 3, 'should have 3 items now')
  90. test.assertElementCount('.todo.completed', 2, 'should have 2 item completed')
  91. test.assertSelectorHasText('#clear-completed', 'Clear completed (2)')
  92. test.assertSelectorHasText('#todo-count strong', '1', 'remaining count should be 1')
  93. })
  94. // remove all completed ------------------------------------------------
  95. .thenClick('#clear-completed', function () {
  96. test.assertElementCount('.todo', 1, 'should have 1 item now')
  97. test.assertSelectorHasText('.todo label', 'test2', 'the remaining one should be the second one')
  98. test.assertElementCount('.todo.completed', 0, 'should have no completed items now')
  99. test.assertSelectorHasText('#todo-count strong', '1', 'remaining count should be 1')
  100. test.assertNotVisible('#clear-completed', '#clear-completed should be hidden')
  101. })
  102. // prepare to test filters ------------------------------------------------
  103. .then(function () {
  104. createNewItem('test')
  105. createNewItem('test')
  106. })
  107. .then(function () {
  108. this.click('.todo:nth-child(2) .toggle')
  109. this.click('.todo:nth-child(3) .toggle')
  110. })
  111. // active filter ----------------------------------------------------------
  112. .thenClick('#filters li:nth-child(2) a', function () {
  113. test.assertElementCount('.todo', 1, 'filter active should have 1 item')
  114. test.assertElementCount('.todo.completed', 0, 'visible items should be incomplete')
  115. })
  116. // add item with filter active --------------------------------------------
  117. // mostly make sure v-repeat works well with v-if
  118. .then(function () {
  119. createNewItem('test')
  120. })
  121. .then(function () {
  122. test.assertElementCount('.todo', 2, 'should be able to create new item when fitler active')
  123. })
  124. // completed filter -------------------------------------------------------
  125. .thenClick('#filters li:nth-child(3) a', function () {
  126. test.assertElementCount('.todo', 2, 'filter completed should have 2 items')
  127. test.assertElementCount('.todo.completed', 2, 'visible items should be completed')
  128. })
  129. // active filter on page load ---------------------------------------------
  130. .thenOpen('../../examples/todomvc/index.html#/active', function () {
  131. test.assertElementCount('.todo', 2, 'filter active should have 2 items')
  132. test.assertElementCount('.todo.completed', 0, 'visible items should be incompleted')
  133. test.assertSelectorHasText('#clear-completed', 'Clear completed (2)')
  134. test.assertSelectorHasText('#todo-count strong', '2', 'remaining count should be 2')
  135. })
  136. // completed filter on page load ------------------------------------------
  137. .thenOpen('../../examples/todomvc/index.html#/completed', function () {
  138. test.assertElementCount('.todo', 2, 'filter completed should have 2 items')
  139. test.assertElementCount('.todo.completed', 2, 'visible items should be completed')
  140. test.assertSelectorHasText('#clear-completed', 'Clear completed (2)')
  141. test.assertSelectorHasText('#todo-count strong', '2', 'remaining count should be 2')
  142. })
  143. // toggling todos when filter is active -----------------------------------
  144. .thenClick('.todo .toggle', function () {
  145. test.assertElementCount('.todo', 1, 'should have only 1 item left')
  146. })
  147. .thenClick('#filters li:nth-child(2) a', function () {
  148. test.assertElementCount('.todo', 3, 'should have only 3 items now')
  149. })
  150. .thenClick('.todo .toggle', function () {
  151. test.assertElementCount('.todo', 2, 'should have only 2 items now')
  152. })
  153. // test editing triggered by blur ------------------------------------------
  154. .thenClick('#filters li:nth-child(1) a')
  155. .then(function () {
  156. doubleClick('.todo:nth-child(1) label')
  157. })
  158. .then(function () {
  159. test.assertElementCount('.todo.editing', 1, 'should have one item being edited')
  160. test.assertEval(function () {
  161. var input = document.querySelector('.todo:nth-child(1) .edit')
  162. return input === document.activeElement
  163. }, 'edit input should be focused')
  164. })
  165. .then(function () {
  166. resetField()
  167. this.sendKeys('.todo:nth-child(1) .edit', 'edited!') // doneEdit triggered by blur
  168. })
  169. .then(function () {
  170. test.assertElementCount('.todo.editing', 0, 'item should no longer be edited')
  171. test.assertSelectorHasText('.todo:nth-child(1) label', 'edited!', 'item should have updated text')
  172. })
  173. // test editing triggered by enter ----------------------------------------
  174. .then(function () {
  175. doubleClick('.todo label')
  176. })
  177. .then(function () {
  178. resetField()
  179. this.sendKeys('.todo:nth-child(1) .edit', 'edited again!', { keepFocus: true })
  180. keyUp(13) // Enter
  181. })
  182. .then(function () {
  183. test.assertElementCount('.todo.editing', 0, 'item should no longer be edited')
  184. test.assertSelectorHasText('.todo:nth-child(1) label', 'edited again!', 'item should have updated text')
  185. })
  186. // test cancel ------------------------------------------------------------
  187. .then(function () {
  188. doubleClick('.todo label')
  189. })
  190. .then(function () {
  191. resetField()
  192. this.sendKeys('.todo:nth-child(1) .edit', 'cancel test', { keepFocus: true })
  193. keyUp(27) // ESC
  194. })
  195. .then(function () {
  196. test.assertElementCount('.todo.editing', 0, 'item should no longer be edited')
  197. test.assertSelectorHasText('.todo label', 'edited again!', 'item should not have updated text')
  198. })
  199. // test empty input remove ------------------------------------------------
  200. .then(function () {
  201. doubleClick('.todo label')
  202. })
  203. .then(function () {
  204. resetField()
  205. this.sendKeys('.todo:nth-child(1) .edit', ' ')
  206. })
  207. .then(function () {
  208. test.assertElementCount('.todo', 3, 'item should have been deleted')
  209. })
  210. //test toggle all
  211. .thenClick('#toggle-all', function () {
  212. test.assertElementCount('.todo.completed', 3, 'should toggle all items to completed')
  213. })
  214. .thenClick('#toggle-all', function () {
  215. test.assertElementCount('.todo:not(.completed)', 3, 'should toggle all items to active')
  216. })
  217. // run
  218. .run(function () {
  219. test.done()
  220. })
  221. // helper ===============
  222. function createNewItem (text) {
  223. if (text) {
  224. casper.sendKeys('#new-todo', text)
  225. }
  226. casper.evaluate(function () {
  227. // casper.mouseEvent can't set keyCode
  228. var field = document.getElementById('new-todo'),
  229. e = document.createEvent('HTMLEvents')
  230. e.initEvent('keyup', true, true)
  231. e.keyCode = 13
  232. field.dispatchEvent(e)
  233. })
  234. }
  235. function doubleClick (selector) {
  236. casper.evaluate(function (selector) {
  237. var el = document.querySelector(selector),
  238. e = document.createEvent('MouseEvents')
  239. e.initMouseEvent('dblclick', true, true, null, 1, 0, 0, 0, 0, false, false, false, false, 0, null)
  240. el.dispatchEvent(e)
  241. }, selector)
  242. }
  243. function keyUp (code) {
  244. casper.evaluate(function (code) {
  245. var input = document.querySelector('.todo:nth-child(1) .edit'),
  246. e = document.createEvent('HTMLEvents')
  247. e.initEvent('keyup', true, true)
  248. e.keyCode = code
  249. input.dispatchEvent(e)
  250. }, code)
  251. }
  252. function resetField () {
  253. // somehow casper.sendKey() option reset:true doesn't work
  254. casper.evaluate(function () {
  255. document.querySelector('.todo:nth-child(1) .edit').value = ''
  256. })
  257. }
  258. })