todomvc.spec.ts 6.3 KB

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