logseq-api.spec.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 page.waitForSelector(`span[data-ref="${bPageName}"]`)
  10. let p = await callAPI('get_current_page')
  11. const bp = await callAPI('append_block_in_page', bPageName, 'tests')
  12. expect(p.name).toBe(bPageName)
  13. p = await callAPI('get_page', bPageName)
  14. expect(p.name).toBe(bPageName)
  15. await callAPI('edit_block', bp.uuid)
  16. const b = (await callAPI('get_current_block'))
  17. expect(Object.keys(b)).toContain('uuid')
  18. await page.waitForSelector('.block-editor > textarea')
  19. await page.locator('.block-editor > textarea').fill('')
  20. const content = 'test api'
  21. await page.type('.block-editor > textarea', content)
  22. const editingContent = await callAPI('get_editing_block_content')
  23. expect(editingContent).toBe(content)
  24. // create
  25. let b1 = await callAPI('insert_block', b.uuid, content)
  26. b1 = await callAPI('get_block', b1.uuid)
  27. expect(b1.parent.id).toBe(b.id)
  28. // update
  29. const content1 = content + '+ update!'
  30. await callAPI('update_block', b1.uuid, content1)
  31. await page.waitForTimeout(1000)
  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. })