logseq-api.spec.ts 3.8 KB

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