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