basic.spec.ts 10 KB

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