logseq-api.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { test } from './fixtures'
  2. import { expect } from '@playwright/test'
  3. import { callPageAPI } from './utils'
  4. test('block related apis',
  5. async ({ page }) => {
  6. const callAPI = callPageAPI.bind(null, page)
  7. const bPageName = 'block-test-page'
  8. await callAPI('create_page', bPageName, null, { createFirstBlock: false })
  9. await callAPI('create_page', bPageName, null, { createFirstBlock: false })
  10. await page.waitForSelector(`body[data-page="${bPageName}"]`)
  11. let p = await callAPI('get_current_page')
  12. const bp = await callAPI('append_block_in_page', bPageName, 'tests')
  13. expect(p.name).toBe(bPageName)
  14. p = await callAPI('get_page', bPageName)
  15. expect(p.name).toBe(bPageName)
  16. await callAPI('edit_block', bp.uuid)
  17. const b = (await callAPI('get_current_block'))
  18. expect(Object.keys(b)).toContain('uuid')
  19. await page.waitForSelector('.block-editor > textarea')
  20. await page.locator('.block-editor > textarea').fill('')
  21. const content = 'test api'
  22. await page.type('.block-editor > textarea', content)
  23. const editingContent = await callAPI('get_editing_block_content')
  24. expect(editingContent).toBe(content)
  25. // create
  26. let b1 = await callAPI('insert_block', b.uuid, content)
  27. b1 = await callAPI('get_block', b1.uuid)
  28. expect(b1.parent.id).toBe(b.id)
  29. // update
  30. const content1 = content + '+ update!'
  31. await callAPI('update_block', b1.uuid, content1)
  32. b1 = await callAPI('get_block', b1.uuid)
  33. expect(b1.content).toBe(content1)
  34. // remove
  35. await callAPI('remove_block', b1.uuid)
  36. b1 = await callAPI('get_block', b1.uuid)
  37. expect(b1).toBeNull()
  38. // traverse
  39. b1 = await callAPI('insert_block', b.uuid, content1, { sibling: true })
  40. const nb = await callAPI('get_next_sibling_block', b.uuid)
  41. const pb = await callAPI('get_previous_sibling_block', b1.uuid)
  42. expect(nb.uuid).toBe(b1.uuid)
  43. expect(pb.uuid).toBe(b.uuid)
  44. // move
  45. await callAPI('move_block', b.uuid, b1.uuid)
  46. const mb = await callAPI('get_next_sibling_block', b1.uuid)
  47. expect(mb.uuid).toBe(b.uuid)
  48. // properties
  49. await callAPI('upsert_block_property', b1.uuid, 'a', 1)
  50. let prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  51. expect(prop1).toBe(1)
  52. await callAPI('upsert_block_property', b1.uuid, 'a', 2)
  53. prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  54. expect(prop1).toBe(2)
  55. await callAPI('remove_block_property', b1.uuid, 'a')
  56. prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  57. expect(prop1).toBeNull()
  58. await callAPI('upsert_block_property', b1.uuid, 'a', 1)
  59. await callAPI('upsert_block_property', b1.uuid, 'b', 1)
  60. prop1 = await callAPI('get_block_properties', b1.uuid)
  61. expect(prop1).toEqual({ a: 1, b: 1 })
  62. // await page.pause()
  63. })
  64. /**
  65. * load local tests plugin
  66. */
  67. export async function loadLocalE2eTestsPlugin(page) {
  68. const pid = 'a-logseq-plugin-for-e2e-tests'
  69. const hasLoaded = await page.evaluate(([pid]) => {
  70. // @ts-ignore
  71. const p = window.LSPluginCore.registeredPlugins.get(pid)
  72. return p != null
  73. }, [pid])
  74. if (hasLoaded) return true
  75. await callPageAPI(page, 'set_state_from_store',
  76. 'ui/developer-mode?', true)
  77. await page.keyboard.press('t+p')
  78. await page.locator('text=Load unpacked plugin')
  79. await callPageAPI(page, 'set_state_from_store',
  80. 'plugin/selected-unpacked-pkg', `${__dirname}/plugin`)
  81. await page.keyboard.press('Escape')
  82. await page.keyboard.press('Escape')
  83. }