todomvc.js 10 KB

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