todomvc.spec.ts 6.2 KB

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