basic.spec.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { expect } from '@playwright/test'
  2. import { test } from './fixtures'
  3. import { randomString, createRandomPage, newBlock } from './utils'
  4. test('render app', async ({ page }) => {
  5. // Direct Electron console to Node terminal.
  6. // page.on('console', console.log)
  7. // NOTE: part of app startup tests is moved to `fixtures.ts`.
  8. expect(await page.title()).toMatch(/^Logseq.*?/)
  9. })
  10. test('toggle sidebar', async ({ page }) => {
  11. let sidebar = page.locator('#left-sidebar')
  12. // Left sidebar is toggled by `is-open` class
  13. if (/is-open/.test(await sidebar.getAttribute('class'))) {
  14. await page.click('#left-menu.button')
  15. expect(await sidebar.getAttribute('class')).not.toMatch(/is-open/)
  16. } else {
  17. await page.click('#left-menu.button')
  18. expect(await sidebar.getAttribute('class')).toMatch(/is-open/)
  19. await page.click('#left-menu.button')
  20. expect(await sidebar.getAttribute('class')).not.toMatch(/is-open/)
  21. }
  22. await page.click('#left-menu.button')
  23. expect(await sidebar.getAttribute('class')).toMatch(/is-open/)
  24. await page.waitForSelector('#left-sidebar .left-sidebar-inner', { state: 'visible' })
  25. await page.waitForSelector('#left-sidebar a:has-text("New page")', { state: 'visible' })
  26. })
  27. test('search', async ({ page }) => {
  28. await page.click('#search-button')
  29. await page.waitForSelector('[placeholder="Search or create page"]')
  30. await page.fill('[placeholder="Search or create page"]', 'welcome')
  31. await page.waitForTimeout(500)
  32. const results = await page.$$('#ui__ac-inner .block')
  33. expect(results.length).toBeGreaterThanOrEqual(1)
  34. })
  35. test('create page and blocks', async ({ page }) => {
  36. await createRandomPage(page)
  37. // do editing
  38. await page.fill(':nth-match(textarea, 1)', 'this is my first bullet')
  39. await page.press(':nth-match(textarea, 1)', 'Enter')
  40. // first block
  41. expect(await page.$$('.block-content')).toHaveLength(1)
  42. await page.fill(':nth-match(textarea, 1)', 'this is my second bullet')
  43. await page.press(':nth-match(textarea, 1)', 'Enter')
  44. await page.fill(':nth-match(textarea, 1)', 'this is my third bullet')
  45. await page.press(':nth-match(textarea, 1)', 'Tab')
  46. await page.press(':nth-match(textarea, 1)', 'Enter')
  47. await page.keyboard.type('continue editing test')
  48. await page.keyboard.press('Shift+Enter')
  49. await page.keyboard.type('continue')
  50. await page.keyboard.press('Enter')
  51. await page.keyboard.press('Shift+Tab')
  52. await page.keyboard.press('Shift+Tab')
  53. await page.keyboard.type('test ok')
  54. await page.keyboard.press('Escape')
  55. const blocks = await page.$$('.ls-block')
  56. expect(blocks).toHaveLength(5)
  57. // active edit
  58. await page.click('.ls-block >> nth=-1')
  59. await page.press('textarea >> nth=0', 'Enter')
  60. await page.fill('textarea >> nth=0', 'test')
  61. for (let i = 0; i < 5; i++) {
  62. await page.keyboard.press('Backspace')
  63. }
  64. await page.keyboard.press('Escape')
  65. await page.waitForTimeout(500)
  66. expect(await page.$$('.ls-block')).toHaveLength(5)
  67. })
  68. test('delete and backspace', async ({ page }) => {
  69. await createRandomPage(page)
  70. await page.fill(':nth-match(textarea, 1)', 'test')
  71. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('test')
  72. // backspace
  73. await page.keyboard.press('Backspace')
  74. await page.keyboard.press('Backspace')
  75. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  76. // refill
  77. await page.fill(':nth-match(textarea, 1)', 'test')
  78. await page.keyboard.press('ArrowLeft')
  79. await page.keyboard.press('ArrowLeft')
  80. // delete
  81. await page.keyboard.press('Delete')
  82. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('tet')
  83. await page.keyboard.press('Delete')
  84. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  85. await page.keyboard.press('Delete')
  86. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  87. // TODO: test delete & backspace across blocks
  88. })
  89. test('selection', async ({ page }) => {
  90. await createRandomPage(page)
  91. await page.fill(':nth-match(textarea, 1)', 'line 1')
  92. await page.press(':nth-match(textarea, 1)', 'Enter')
  93. await page.fill(':nth-match(textarea, 1)', 'line 2')
  94. await page.press(':nth-match(textarea, 1)', 'Enter')
  95. await page.press(':nth-match(textarea, 1)', 'Tab')
  96. await page.fill(':nth-match(textarea, 1)', 'line 3')
  97. await page.press(':nth-match(textarea, 1)', 'Enter')
  98. await page.fill(':nth-match(textarea, 1)', 'line 4')
  99. await page.press(':nth-match(textarea, 1)', 'Tab')
  100. await page.press(':nth-match(textarea, 1)', 'Enter')
  101. await page.fill(':nth-match(textarea, 1)', 'line 5')
  102. await page.keyboard.down('Shift')
  103. await page.keyboard.press('ArrowUp')
  104. await page.keyboard.press('ArrowUp')
  105. await page.keyboard.press('ArrowUp')
  106. await page.keyboard.up('Shift')
  107. await page.waitForTimeout(500)
  108. await page.keyboard.press('Backspace')
  109. expect(await page.$$('.ls-block')).toHaveLength(2)
  110. })
  111. test('template', async ({ page }) => {
  112. const randomTemplate = randomString(10)
  113. await createRandomPage(page)
  114. await page.fill(':nth-match(textarea, 1)', 'template')
  115. await page.press(':nth-match(textarea, 1)', 'Shift+Enter')
  116. await page.type(':nth-match(textarea, 1)', 'template:: ' + randomTemplate)
  117. await page.press(':nth-match(textarea, 1)', 'Enter')
  118. await page.press(':nth-match(textarea, 1)', 'Enter')
  119. await page.press(':nth-match(textarea, 1)', 'Tab')
  120. await page.fill(':nth-match(textarea, 1)', 'line1')
  121. await page.press(':nth-match(textarea, 1)', 'Enter')
  122. await page.fill(':nth-match(textarea, 1)', 'line2')
  123. await page.press(':nth-match(textarea, 1)', 'Enter')
  124. await page.press(':nth-match(textarea, 1)', 'Tab')
  125. await page.fill(':nth-match(textarea, 1)', 'line3')
  126. await page.press(':nth-match(textarea, 1)', 'Enter')
  127. await page.press(':nth-match(textarea, 1)', 'Enter')
  128. await page.press(':nth-match(textarea, 1)', 'Enter')
  129. expect(await page.$$('.ls-block')).toHaveLength(5)
  130. await page.type(':nth-match(textarea, 1)', '/template')
  131. await page.click('[title="Insert a created template here"]')
  132. // type to search template name
  133. await page.keyboard.type(randomTemplate.substring(0, 3))
  134. await page.click('.absolute >> text=' + randomTemplate)
  135. await page.waitForTimeout(500)
  136. expect(await page.$$('.ls-block')).toHaveLength(8)
  137. })
  138. test('auto completion square brackets', async ({ page }) => {
  139. await createRandomPage(page)
  140. await page.fill(':nth-match(textarea, 1)', 'Auto-completion test')
  141. await page.press(':nth-match(textarea, 1)', 'Enter')
  142. // [[]]
  143. await page.type(':nth-match(textarea, 1)', 'This is a [')
  144. await page.inputValue(':nth-match(textarea, 1)').then(text => {
  145. expect(text).toBe('This is a []')
  146. })
  147. await page.type(':nth-match(textarea, 1)', '[')
  148. // wait for search popup
  149. await page.waitForSelector('text="Search for a page"')
  150. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]')
  151. // re-enter edit mode
  152. await page.press(':nth-match(textarea, 1)', 'Escape')
  153. await page.click('.ls-block >> nth=-1')
  154. await page.waitForSelector(':nth-match(textarea, 1)', { state: 'visible' })
  155. // #3253
  156. await page.press(':nth-match(textarea, 1)', 'ArrowLeft')
  157. await page.press(':nth-match(textarea, 1)', 'ArrowLeft')
  158. await page.press(':nth-match(textarea, 1)', 'Enter')
  159. await page.waitForSelector('text="Search for a page"', { state: 'visible' })
  160. // type more `]`s
  161. await page.type(':nth-match(textarea, 1)', ']')
  162. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]')
  163. await page.type(':nth-match(textarea, 1)', ']')
  164. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]')
  165. await page.type(':nth-match(textarea, 1)', ']')
  166. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]]')
  167. })
  168. test('auto completion and auto pair', async ({ page }) => {
  169. await createRandomPage(page)
  170. await page.fill(':nth-match(textarea, 1)', 'Auto-completion test')
  171. await page.press(':nth-match(textarea, 1)', 'Enter')
  172. // {{
  173. await page.type(':nth-match(textarea, 1)', 'type {{')
  174. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type {{}}')
  175. // ((
  176. await newBlock(page)
  177. await page.type(':nth-match(textarea, 1)', 'type (')
  178. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type ()')
  179. await page.type(':nth-match(textarea, 1)', '(')
  180. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type (())')
  181. // 99 #3444
  182. // TODO: Test under different keyboard layout when Playwright supports it
  183. // await newBlock(page)
  184. // await page.type(':nth-match(textarea, 1)', 'type 9')
  185. // expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type 9')
  186. // await page.type(':nth-match(textarea, 1)', '9')
  187. // expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type 99')
  188. // [[ #3251
  189. await newBlock(page)
  190. await page.type(':nth-match(textarea, 1)', 'type [')
  191. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type []')
  192. await page.type(':nth-match(textarea, 1)', '[')
  193. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type [[]]')
  194. // ``
  195. await newBlock(page)
  196. await page.type(':nth-match(textarea, 1)', 'type `')
  197. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type ``')
  198. await page.type(':nth-match(textarea, 1)', 'code here')
  199. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type `code here`')
  200. })
  201. // FIXME: Electron with filechooser is not working
  202. test.skip('open directory', async ({ page }) => {
  203. await page.click('#left-sidebar >> text=Journals')
  204. await page.waitForSelector('h1:has-text("Open a local directory")')
  205. await page.click('h1:has-text("Open a local directory")')
  206. // await page.waitForEvent('filechooser')
  207. await page.keyboard.press('Escape')
  208. await page.click('#left-sidebar >> text=Journals')
  209. })