basic.spec.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { expect } from '@playwright/test'
  2. import fs from 'fs/promises'
  3. import path from 'path'
  4. import { test } from './fixtures'
  5. import { randomString, createRandomPage } from './utils'
  6. test('create page and blocks, save to disk', async ({ page, block, graphDir }) => {
  7. const pageTitle = await createRandomPage(page)
  8. // do editing
  9. await page.keyboard.type('first bullet')
  10. await block.enterNext()
  11. await block.waitForBlocks(2)
  12. await page.keyboard.type('second bullet')
  13. await block.clickNext()
  14. await page.keyboard.type('third bullet')
  15. expect(await block.indent()).toBe(true)
  16. await block.enterNext()
  17. await page.keyboard.type('continue editing')
  18. await page.keyboard.press('Shift+Enter')
  19. await page.keyboard.type('second line')
  20. await block.enterNext()
  21. expect(await block.unindent()).toBe(true)
  22. expect(await block.unindent()).toBe(false)
  23. await page.keyboard.type('test ok')
  24. await page.keyboard.press('Escape')
  25. await block.waitForBlocks(5)
  26. // active edit, and create next block
  27. await block.clickNext()
  28. await page.keyboard.type('test')
  29. for (let i = 0; i < 5; i++) {
  30. await page.keyboard.press('Backspace', { delay: 100 })
  31. }
  32. await page.keyboard.press('Escape')
  33. await block.waitForBlocks(5)
  34. await page.waitForTimeout(2000) // wait for saving to disk
  35. const contentOnDisk = await fs.readFile(
  36. path.join(graphDir, `pages/${pageTitle}.md`),
  37. 'utf8'
  38. )
  39. expect(contentOnDisk.trim()).toEqual(`
  40. - first bullet
  41. - second bullet
  42. - third bullet
  43. - continue editing
  44. second line
  45. - test ok`.trim())
  46. })
  47. test('delete and backspace', async ({ page, block }) => {
  48. await createRandomPage(page)
  49. await block.mustFill('test')
  50. // backspace
  51. await page.keyboard.press('Backspace')
  52. await page.keyboard.press('Backspace')
  53. expect(await page.inputValue('textarea >> nth=0')).toBe('te')
  54. // refill
  55. await block.enterNext()
  56. await block.mustType('test')
  57. await page.keyboard.press('ArrowLeft', { delay: 50 })
  58. await page.keyboard.press('ArrowLeft', { delay: 50 })
  59. // delete
  60. await page.keyboard.press('Delete', { delay: 50 })
  61. expect(await page.inputValue('textarea >> nth=0')).toBe('tet')
  62. await page.keyboard.press('Delete', { delay: 50 })
  63. expect(await page.inputValue('textarea >> nth=0')).toBe('te')
  64. await page.keyboard.press('Delete', { delay: 50 })
  65. expect(await page.inputValue('textarea >> nth=0')).toBe('te')
  66. // TODO: test delete & backspace across blocks
  67. })
  68. test('selection', async ({ page, block }) => {
  69. await createRandomPage(page)
  70. // add 5 blocks
  71. await block.mustFill('line 1')
  72. await block.enterNext()
  73. await block.mustFill('line 2')
  74. await block.enterNext()
  75. expect(await block.indent()).toBe(true)
  76. await block.mustFill('line 3')
  77. await block.enterNext()
  78. await block.mustFill('line 4')
  79. expect(await block.indent()).toBe(true)
  80. await block.enterNext()
  81. await block.mustFill('line 5')
  82. // shift+up select 3 blocks
  83. await page.keyboard.down('Shift')
  84. await page.keyboard.press('ArrowUp')
  85. await page.keyboard.press('ArrowUp')
  86. await page.keyboard.press('ArrowUp')
  87. await page.keyboard.up('Shift')
  88. await block.waitForSelectedBlocks(3)
  89. await page.keyboard.press('Backspace')
  90. await block.waitForBlocks(2)
  91. })
  92. test('template', async ({ page, block }) => {
  93. const randomTemplate = randomString(6)
  94. await createRandomPage(page)
  95. await block.mustFill('template test\ntemplate:: ' + randomTemplate)
  96. await page.keyboard.press('Enter')
  97. await block.clickNext()
  98. expect(await block.indent()).toBe(true)
  99. await block.mustFill('line1')
  100. await block.enterNext()
  101. await block.mustFill('line2')
  102. await block.enterNext()
  103. expect(await block.indent()).toBe(true)
  104. await block.mustFill('line3')
  105. await block.enterNext()
  106. expect(await block.unindent()).toBe(true)
  107. expect(await block.unindent()).toBe(true)
  108. expect(await block.unindent()).toBe(false) // already at the first level
  109. await block.waitForBlocks(5)
  110. // NOTE: use delay to type slower, to trigger auto-completion UI.
  111. await block.mustType('/template')
  112. await page.click('[title="Insert a created template here"]')
  113. // type to search template name
  114. await page.keyboard.type(randomTemplate.substring(0, 3), { delay: 100 })
  115. const popupMenuItem = page.locator('.absolute >> text=' + randomTemplate)
  116. await popupMenuItem.waitFor({ timeout: 2000 }) // wait for template search
  117. await popupMenuItem.click()
  118. await block.waitForBlocks(8)
  119. })
  120. test('auto completion square brackets', async ({ page, block }) => {
  121. await createRandomPage(page)
  122. // In this test, `type` is unsed instead of `fill`, to allow for auto-completion.
  123. // [[]]
  124. await block.mustType('This is a [', { toBe: 'This is a []' })
  125. await block.mustType('[', { toBe: 'This is a [[]]' })
  126. // wait for search popup
  127. await page.waitForSelector('text="Search for a page"')
  128. // re-enter edit mode
  129. await page.press('textarea >> nth=0', 'Escape')
  130. await page.click('.ls-block >> nth=-1')
  131. await page.waitForSelector('textarea >> nth=0', { state: 'visible' })
  132. // #3253
  133. await page.press('textarea >> nth=0', 'ArrowLeft')
  134. await page.press('textarea >> nth=0', 'ArrowLeft')
  135. await page.press('textarea >> nth=0', 'Enter')
  136. await page.waitForSelector('text="Search for a page"', { state: 'visible' })
  137. // type more `]`s
  138. await page.type('textarea >> nth=0', ']')
  139. expect(await page.inputValue('textarea >> nth=0')).toBe('This is a [[]]')
  140. await page.type('textarea >> nth=0', ']')
  141. expect(await page.inputValue('textarea >> nth=0')).toBe('This is a [[]]')
  142. await page.type('textarea >> nth=0', ']')
  143. expect(await page.inputValue('textarea >> nth=0')).toBe('This is a [[]]]')
  144. })
  145. test('auto completion and auto pair', async ({ page, block }) => {
  146. await createRandomPage(page)
  147. await block.mustFill('Auto-completion test')
  148. await block.enterNext()
  149. // {{
  150. await block.mustType('type {{', { toBe: 'type {{}}' })
  151. // ((
  152. await block.clickNext()
  153. await block.mustType('type (', { toBe: 'type ()' })
  154. await block.mustType('(', { toBe: 'type (())' })
  155. // [[ #3251
  156. await block.clickNext()
  157. await block.mustType('type [', { toBe: 'type []' })
  158. await block.mustType('[', { toBe: 'type [[]]' })
  159. await block.escapeEditing() // escape any popup from `[[]]`
  160. // ``
  161. await block.clickNext()
  162. await block.mustType('type `', { toBe: 'type ``' })
  163. await block.mustType('code here', { toBe: 'type `code here`' })
  164. })
  165. test('invalid page props #3944', async ({ page, block }) => {
  166. await createRandomPage(page)
  167. await block.mustFill('public:: true\nsize:: 65535')
  168. await page.press('textarea >> nth=0', 'Enter')
  169. // Force rendering property block
  170. await block.clickNext()
  171. })