|
| 1 | +import path from 'node:path' |
| 2 | +import { E2E_TIMEOUT, setupPuppeteer } from './e2eUtils' |
| 3 | + |
| 4 | +describe('e2e: todomvc', () => { |
| 5 | + const { |
| 6 | + page, |
| 7 | + click, |
| 8 | + isVisible, |
| 9 | + count, |
| 10 | + text, |
| 11 | + value, |
| 12 | + isChecked, |
| 13 | + isFocused, |
| 14 | + classList, |
| 15 | + enterValue, |
| 16 | + clearValue, |
| 17 | + } = setupPuppeteer() |
| 18 | + |
| 19 | + async function removeItemAt(n: number) { |
| 20 | + const item = (await page().$('.todo:nth-child(' + n + ')'))! |
| 21 | + const itemBBox = (await item.boundingBox())! |
| 22 | + await page().mouse.move(itemBBox.x + 10, itemBBox.y + 10) |
| 23 | + await click('.todo:nth-child(' + n + ') .destroy') |
| 24 | + } |
| 25 | + |
| 26 | + async function testTodomvc(apiType: 'classic' | 'composition') { |
| 27 | + let baseUrl = `../../examples/${apiType}/todomvc.html` |
| 28 | + baseUrl = `file://${path.resolve(__dirname, baseUrl)}` |
| 29 | + |
| 30 | + await page().goto(baseUrl) |
| 31 | + expect(await isVisible('.main')).toBe(false) |
| 32 | + expect(await isVisible('.footer')).toBe(false) |
| 33 | + expect(await count('.filters .selected')).toBe(1) |
| 34 | + expect(await text('.filters .selected')).toBe('All') |
| 35 | + expect(await count('.todo')).toBe(0) |
| 36 | + |
| 37 | + await enterValue('.new-todo', 'test') |
| 38 | + expect(await count('.todo')).toBe(1) |
| 39 | + expect(await isVisible('.todo .edit')).toBe(false) |
| 40 | + expect(await text('.todo label')).toBe('test') |
| 41 | + expect(await text('.todo-count strong')).toBe('1') |
| 42 | + expect(await isChecked('.todo .toggle')).toBe(false) |
| 43 | + expect(await isVisible('.main')).toBe(true) |
| 44 | + expect(await isVisible('.footer')).toBe(true) |
| 45 | + expect(await isVisible('.clear-completed')).toBe(false) |
| 46 | + expect(await value('.new-todo')).toBe('') |
| 47 | + |
| 48 | + await enterValue('.new-todo', 'test2') |
| 49 | + expect(await count('.todo')).toBe(2) |
| 50 | + expect(await text('.todo:nth-child(2) label')).toBe('test2') |
| 51 | + expect(await text('.todo-count strong')).toBe('2') |
| 52 | + |
| 53 | + // toggle |
| 54 | + await click('.todo .toggle') |
| 55 | + expect(await count('.todo.completed')).toBe(1) |
| 56 | + expect(await classList('.todo:nth-child(1)')).toContain('completed') |
| 57 | + expect(await text('.todo-count strong')).toBe('1') |
| 58 | + expect(await isVisible('.clear-completed')).toBe(true) |
| 59 | + |
| 60 | + await enterValue('.new-todo', 'test3') |
| 61 | + expect(await count('.todo')).toBe(3) |
| 62 | + expect(await text('.todo:nth-child(3) label')).toBe('test3') |
| 63 | + expect(await text('.todo-count strong')).toBe('2') |
| 64 | + |
| 65 | + await enterValue('.new-todo', 'test4') |
| 66 | + await enterValue('.new-todo', 'test5') |
| 67 | + expect(await count('.todo')).toBe(5) |
| 68 | + expect(await text('.todo-count strong')).toBe('4') |
| 69 | + |
| 70 | + // toggle more |
| 71 | + await click('.todo:nth-child(4) .toggle') |
| 72 | + await click('.todo:nth-child(5) .toggle') |
| 73 | + expect(await count('.todo.completed')).toBe(3) |
| 74 | + expect(await text('.todo-count strong')).toBe('2') |
| 75 | + |
| 76 | + // remove |
| 77 | + await removeItemAt(1) |
| 78 | + expect(await count('.todo')).toBe(4) |
| 79 | + expect(await count('.todo.completed')).toBe(2) |
| 80 | + expect(await text('.todo-count strong')).toBe('2') |
| 81 | + await removeItemAt(2) |
| 82 | + expect(await count('.todo')).toBe(3) |
| 83 | + expect(await count('.todo.completed')).toBe(2) |
| 84 | + expect(await text('.todo-count strong')).toBe('1') |
| 85 | + |
| 86 | + // remove all |
| 87 | + await click('.clear-completed') |
| 88 | + expect(await count('.todo')).toBe(1) |
| 89 | + expect(await text('.todo label')).toBe('test2') |
| 90 | + expect(await count('.todo.completed')).toBe(0) |
| 91 | + expect(await text('.todo-count strong')).toBe('1') |
| 92 | + expect(await isVisible('.clear-completed')).toBe(false) |
| 93 | + |
| 94 | + // prepare to test filters |
| 95 | + await enterValue('.new-todo', 'test') |
| 96 | + await enterValue('.new-todo', 'test') |
| 97 | + await click('.todo:nth-child(2) .toggle') |
| 98 | + await click('.todo:nth-child(3) .toggle') |
| 99 | + |
| 100 | + // active filter |
| 101 | + await click('.filters li:nth-child(2) a') |
| 102 | + expect(await count('.todo')).toBe(1) |
| 103 | + expect(await count('.todo.completed')).toBe(0) |
| 104 | + // add item with filter active |
| 105 | + await enterValue('.new-todo', 'test') |
| 106 | + expect(await count('.todo')).toBe(2) |
| 107 | + |
| 108 | + // completed filter |
| 109 | + await click('.filters li:nth-child(3) a') |
| 110 | + expect(await count('.todo')).toBe(2) |
| 111 | + expect(await count('.todo.completed')).toBe(2) |
| 112 | + |
| 113 | + // filter on page load |
| 114 | + await page().goto(`${baseUrl}#active`) |
| 115 | + expect(await count('.todo')).toBe(2) |
| 116 | + expect(await count('.todo.completed')).toBe(0) |
| 117 | + expect(await text('.todo-count strong')).toBe('2') |
| 118 | + |
| 119 | + // completed on page load |
| 120 | + await page().goto(`${baseUrl}#completed`) |
| 121 | + expect(await count('.todo')).toBe(2) |
| 122 | + expect(await count('.todo.completed')).toBe(2) |
| 123 | + expect(await text('.todo-count strong')).toBe('2') |
| 124 | + |
| 125 | + // toggling with filter active |
| 126 | + await click('.todo .toggle') |
| 127 | + expect(await count('.todo')).toBe(1) |
| 128 | + await click('.filters li:nth-child(2) a') |
| 129 | + expect(await count('.todo')).toBe(3) |
| 130 | + await click('.todo .toggle') |
| 131 | + expect(await count('.todo')).toBe(2) |
| 132 | + |
| 133 | + // editing triggered by blur |
| 134 | + await click('.filters li:nth-child(1) a') |
| 135 | + await click('.todo:nth-child(1) label', { clickCount: 2 }) |
| 136 | + expect(await count('.todo.editing')).toBe(1) |
| 137 | + expect(await isFocused('.todo:nth-child(1) .edit')).toBe(true) |
| 138 | + await clearValue('.todo:nth-child(1) .edit') |
| 139 | + await page().type('.todo:nth-child(1) .edit', 'edited!') |
| 140 | + await click('.new-todo') // blur |
| 141 | + expect(await count('.todo.editing')).toBe(0) |
| 142 | + expect(await text('.todo:nth-child(1) label')).toBe('edited!') |
| 143 | + |
| 144 | + // editing triggered by enter |
| 145 | + await click('.todo label', { clickCount: 2 }) |
| 146 | + await enterValue('.todo:nth-child(1) .edit', 'edited again!') |
| 147 | + expect(await count('.todo.editing')).toBe(0) |
| 148 | + expect(await text('.todo:nth-child(1) label')).toBe('edited again!') |
| 149 | + |
| 150 | + // cancel |
| 151 | + await click('.todo label', { clickCount: 2 }) |
| 152 | + await clearValue('.todo:nth-child(1) .edit') |
| 153 | + await page().type('.todo:nth-child(1) .edit', 'edited!') |
| 154 | + await page().keyboard.press('Escape') |
| 155 | + expect(await count('.todo.editing')).toBe(0) |
| 156 | + expect(await text('.todo:nth-child(1) label')).toBe('edited again!') |
| 157 | + |
| 158 | + // empty value should remove |
| 159 | + await click('.todo label', { clickCount: 2 }) |
| 160 | + await enterValue('.todo:nth-child(1) .edit', ' ') |
| 161 | + expect(await count('.todo')).toBe(3) |
| 162 | + |
| 163 | + // toggle all |
| 164 | + await click('.toggle-all+label') |
| 165 | + expect(await count('.todo.completed')).toBe(3) |
| 166 | + await click('.toggle-all+label') |
| 167 | + expect(await count('.todo:not(.completed)')).toBe(3) |
| 168 | + } |
| 169 | + |
| 170 | + test( |
| 171 | + 'composition', |
| 172 | + async () => { |
| 173 | + await testTodomvc('composition') |
| 174 | + }, |
| 175 | + E2E_TIMEOUT, |
| 176 | + ) |
| 177 | +}) |
0 commit comments