todomvc.js 11 KB

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