todomvc.js 11 KB

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