logseq-api.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. await page.waitForTimeout(1000)
  31. b1 = await callAPI('get_block', b1.uuid)
  32. expect(b1.content).toBe(content1)
  33. // remove
  34. await callAPI('remove_block', b1.uuid)
  35. b1 = await callAPI('get_block', b1.uuid)
  36. expect(b1).toBeNull()
  37. // traverse
  38. b1 = await callAPI('insert_block', b.uuid, content1, { sibling: true })
  39. const nb = await callAPI('get_next_sibling_block', b.uuid)
  40. const pb = await callAPI('get_previous_sibling_block', b1.uuid)
  41. expect(nb.uuid).toBe(b1.uuid)
  42. expect(pb.uuid).toBe(b.uuid)
  43. // move
  44. await callAPI('move_block', b.uuid, b1.uuid)
  45. const mb = await callAPI('get_next_sibling_block', b1.uuid)
  46. expect(mb.uuid).toBe(b.uuid)
  47. // properties
  48. await callAPI('upsert_block_property', b1.uuid, 'a', 1)
  49. let prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  50. expect(prop1).toBe(1)
  51. await callAPI('upsert_block_property', b1.uuid, 'a', 2)
  52. prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  53. expect(prop1).toBe(2)
  54. await callAPI('remove_block_property', b1.uuid, 'a')
  55. prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  56. expect(prop1).toBeNull()
  57. await callAPI('upsert_block_property', b1.uuid, 'a', 1)
  58. await callAPI('upsert_block_property', b1.uuid, 'b', 1)
  59. prop1 = await callAPI('get_block_properties', b1.uuid)
  60. expect(prop1).toEqual({ a: 1, b: 1 })
  61. // await page.pause()
  62. })
  63. /**
  64. * @param page
  65. * @param method
  66. * @param args
  67. */
  68. export async function callPageAPI(page, method, ...args) {
  69. return await page.evaluate(([method, args]) => {
  70. // @ts-ignore
  71. return window.logseq.api[method]?.(...args)
  72. }, [method, args])
  73. }