basic.spec.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { expect } from '@playwright/test'
  2. import fs from 'fs/promises'
  3. import path from 'path'
  4. import { test, graphDir } from './fixtures'
  5. import { randomString, createRandomPage, newBlock } from './utils'
  6. test('render app', async ({ page }) => {
  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. const pageTitle = 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. await page.waitForTimeout(500)
  68. const contentOnDisk = await fs.readFile(
  69. path.join(graphDir, `pages/${pageTitle}.md`),
  70. 'utf8'
  71. )
  72. expect(contentOnDisk.trim()).toEqual(`
  73. - this is my first bullet
  74. - this is my second bullet
  75. - this is my third bullet
  76. - continue editing test
  77. continue
  78. - test ok`.trim())
  79. })
  80. test('delete and backspace', async ({ page }) => {
  81. await createRandomPage(page)
  82. await page.fill(':nth-match(textarea, 1)', 'test')
  83. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('test')
  84. // backspace
  85. await page.keyboard.press('Backspace')
  86. await page.keyboard.press('Backspace')
  87. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  88. // refill
  89. await page.fill(':nth-match(textarea, 1)', 'test')
  90. await page.keyboard.press('ArrowLeft')
  91. await page.keyboard.press('ArrowLeft')
  92. // delete
  93. await page.keyboard.press('Delete')
  94. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('tet')
  95. await page.keyboard.press('Delete')
  96. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  97. await page.keyboard.press('Delete')
  98. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  99. // TODO: test delete & backspace across blocks
  100. })
  101. test('selection', async ({ page }) => {
  102. await createRandomPage(page)
  103. await page.fill(':nth-match(textarea, 1)', 'line 1')
  104. await page.press(':nth-match(textarea, 1)', 'Enter')
  105. await page.fill(':nth-match(textarea, 1)', 'line 2')
  106. await page.press(':nth-match(textarea, 1)', 'Enter')
  107. await page.press(':nth-match(textarea, 1)', 'Tab')
  108. await page.fill(':nth-match(textarea, 1)', 'line 3')
  109. await page.press(':nth-match(textarea, 1)', 'Enter')
  110. await page.fill(':nth-match(textarea, 1)', 'line 4')
  111. await page.press(':nth-match(textarea, 1)', 'Tab')
  112. await page.press(':nth-match(textarea, 1)', 'Enter')
  113. await page.fill(':nth-match(textarea, 1)', 'line 5')
  114. await page.keyboard.down('Shift')
  115. await page.keyboard.press('ArrowUp')
  116. await page.keyboard.press('ArrowUp')
  117. await page.keyboard.press('ArrowUp')
  118. await page.keyboard.up('Shift')
  119. await page.waitForTimeout(500)
  120. await page.keyboard.press('Backspace')
  121. expect(await page.$$('.ls-block')).toHaveLength(2)
  122. })
  123. test('template', async ({ page }) => {
  124. const randomTemplate = randomString(10)
  125. await createRandomPage(page)
  126. await page.fill(':nth-match(textarea, 1)', 'template')
  127. await page.press(':nth-match(textarea, 1)', 'Shift+Enter')
  128. await page.type(':nth-match(textarea, 1)', 'template:: ' + randomTemplate)
  129. await page.press(':nth-match(textarea, 1)', 'Enter')
  130. await page.press(':nth-match(textarea, 1)', 'Enter')
  131. await page.press(':nth-match(textarea, 1)', 'Tab')
  132. await page.fill(':nth-match(textarea, 1)', 'line1')
  133. await page.press(':nth-match(textarea, 1)', 'Enter')
  134. await page.fill(':nth-match(textarea, 1)', 'line2')
  135. await page.press(':nth-match(textarea, 1)', 'Enter')
  136. await page.press(':nth-match(textarea, 1)', 'Tab')
  137. await page.fill(':nth-match(textarea, 1)', 'line3')
  138. await page.press(':nth-match(textarea, 1)', 'Enter')
  139. await page.press(':nth-match(textarea, 1)', 'Enter')
  140. await page.press(':nth-match(textarea, 1)', 'Enter')
  141. expect(await page.$$('.ls-block')).toHaveLength(5)
  142. await page.type(':nth-match(textarea, 1)', '/template')
  143. await page.click('[title="Insert a created template here"]')
  144. // type to search template name
  145. await page.keyboard.type(randomTemplate.substring(0, 3))
  146. await page.click('.absolute >> text=' + randomTemplate)
  147. await page.waitForTimeout(500)
  148. expect(await page.$$('.ls-block')).toHaveLength(8)
  149. })
  150. test('auto completion square brackets', async ({ page }) => {
  151. await createRandomPage(page)
  152. await page.fill(':nth-match(textarea, 1)', 'Auto-completion test')
  153. await page.press(':nth-match(textarea, 1)', 'Enter')
  154. // [[]]
  155. await page.type(':nth-match(textarea, 1)', 'This is a [')
  156. await page.inputValue(':nth-match(textarea, 1)').then(text => {
  157. expect(text).toBe('This is a []')
  158. })
  159. await page.type(':nth-match(textarea, 1)', '[')
  160. // wait for search popup
  161. await page.waitForSelector('text="Search for a page"')
  162. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]')
  163. // re-enter edit mode
  164. await page.press(':nth-match(textarea, 1)', 'Escape')
  165. await page.click('.ls-block >> nth=-1')
  166. await page.waitForSelector(':nth-match(textarea, 1)', { state: 'visible' })
  167. // #3253
  168. await page.press(':nth-match(textarea, 1)', 'ArrowLeft')
  169. await page.press(':nth-match(textarea, 1)', 'ArrowLeft')
  170. await page.press(':nth-match(textarea, 1)', 'Enter')
  171. await page.waitForSelector('text="Search for a page"', { state: 'visible' })
  172. // type more `]`s
  173. await page.type(':nth-match(textarea, 1)', ']')
  174. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]')
  175. await page.type(':nth-match(textarea, 1)', ']')
  176. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]')
  177. await page.type(':nth-match(textarea, 1)', ']')
  178. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]]')
  179. })
  180. test('auto completion and auto pair', async ({ page }) => {
  181. await createRandomPage(page)
  182. await page.fill(':nth-match(textarea, 1)', 'Auto-completion test')
  183. await page.press(':nth-match(textarea, 1)', 'Enter')
  184. // {{
  185. await page.type(':nth-match(textarea, 1)', 'type {{')
  186. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type {{}}')
  187. // ((
  188. await newBlock(page)
  189. await page.type(':nth-match(textarea, 1)', 'type (')
  190. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type ()')
  191. await page.type(':nth-match(textarea, 1)', '(')
  192. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type (())')
  193. // 99 #3444
  194. // TODO: Test under different keyboard layout when Playwright supports it
  195. // await newBlock(page)
  196. // await page.type(':nth-match(textarea, 1)', 'type 9')
  197. // expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type 9')
  198. // await page.type(':nth-match(textarea, 1)', '9')
  199. // expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type 99')
  200. // [[ #3251
  201. await newBlock(page)
  202. await page.type(':nth-match(textarea, 1)', 'type [')
  203. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type []')
  204. await page.type(':nth-match(textarea, 1)', '[')
  205. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type [[]]')
  206. // ``
  207. await newBlock(page)
  208. await page.type(':nth-match(textarea, 1)', 'type `')
  209. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type ``')
  210. await page.type(':nth-match(textarea, 1)', 'code here')
  211. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type `code here`')
  212. })
  213. // FIXME: Electron with filechooser is not working
  214. test.skip('open directory', async ({ page }) => {
  215. await page.click('#left-sidebar >> text=Journals')
  216. await page.waitForSelector('h1:has-text("Open a local directory")')
  217. await page.click('h1:has-text("Open a local directory")')
  218. // await page.waitForEvent('filechooser')
  219. await page.keyboard.press('Escape')
  220. await page.click('#left-sidebar >> text=Journals')
  221. })