todomvc.spec.ts 6.7 KB

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