store-selftest.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import assert from "node:assert/strict"
  2. import { createPinia, setActivePinia } from "pinia"
  3. const SRC = "../../../../../src/modules/cockpit/"
  4. setActivePinia(createPinia())
  5. const { useEventStore } = await import(SRC + "stores/eventStore.js")
  6. const config = { dataSource: "real", api: { baseUrl: "http://test.invalid" } }
  7. let urls = []
  8. /** 回显请求里的 page,让分页视图与请求一致(否则会被"越界夹回"逻辑拉回第 1 页)。 */
  9. const fetchImpl = async (url) => {
  10. urls.push(String(url))
  11. const page = Number((String(url).match(/[?&]page=(\d+)/) || [])[1] || 1)
  12. return { ok: true, status: 200, json: async () => ({ code: 200, msg: "ok", data: { pagination: { page, pageSize: 10, total: 30, totalPages: 3 } } }) }
  13. }
  14. const store = useEventStore()
  15. store.configure({ config, fetchImpl })
  16. await store.loadPanels({ mode: "WARNING", industry: "all", scope: "all" })
  17. assert.equal(store.currentPage, 1)
  18. await store.setCurrentPage(2)
  19. console.log("after setCurrentPage(2), currentPage =", store.currentPage)
  20. assert.equal(store.currentPage, 2)
  21. urls = []
  22. await store.loadPanels({ scope: "south" })
  23. console.log("after scope change, pages =", store.currentPage, store.historyPage, store.workOrderPage)
  24. assert.equal(store.currentPage, 1, "换片区必须回第 1 页")
  25. assert.equal(store.historyPage, 1)
  26. assert.equal(store.workOrderPage, 1)
  27. const currentReq = urls.find((u) => u.includes("/events/current"))
  28. const analysisReq = urls.find((u) => u.includes("/multidimensional-analysis"))
  29. console.log("current url:", currentReq)
  30. console.log("analysis url:", analysisReq)
  31. assert.ok(currentReq && /[?&]page=1(&|$)/.test(currentReq), "当前事件应回第 1 页")
  32. assert.ok(currentReq && !currentReq.includes("district="), "当前事件不该带 district")
  33. assert.ok(analysisReq && analysisReq.includes("district=0"), "预警分析应带 district=0")
  34. await store.setCurrentPage(2)
  35. assert.equal(store.currentPage, 2)
  36. urls = []
  37. await store.loadPanels({})
  38. console.log("after same-condition reload, currentPage =", store.currentPage)
  39. assert.equal(store.currentPage, 2, "同条件重取不复位页码")
  40. console.log("STORE TEST PASS")