FamilyWorkbench.spec.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { shallowMount } from "@vue/test-utils"
  2. import FamilyWorkbench from "@/views/FamilyWorkbench.vue"
  3. import { getFamilyWorkbench } from "@/api/familyWorkbench"
  4. import { getFamilyTasks, getFamilyOverview } from "@/api/admin"
  5. jest.mock("@/api/familyWorkbench")
  6. jest.mock("@/api/admin", () => ({
  7. getFamilyTasks: jest.fn().mockResolvedValue({ data: { records: [], total: 0 } }),
  8. getFamilyOverview: jest.fn().mockResolvedValue({ data: { reports: [], plans: [] } })
  9. }))
  10. jest.mock("echarts", () => ({
  11. init: jest.fn(() => ({ setOption: jest.fn(), dispose: jest.fn(), resize: jest.fn() }))
  12. }))
  13. const mockCards = [
  14. { key: "reports", label: "报告", total: 7, series: [] },
  15. { key: "plans", label: "方案", total: 3, series: [] },
  16. { key: "tasks", label: "任务", total: 9, series: [] },
  17. { key: "messages", label: "消息", total: 2, series: [] },
  18. { key: "cf", label: "CF值", total: 100, series: [] },
  19. { key: "coupons", label: "优惠券", total: 4, series: [] }
  20. ]
  21. describe("FamilyWorkbench 单家庭工作台", () => {
  22. function mountPage() {
  23. return shallowMount(FamilyWorkbench, {
  24. mocks: {
  25. $route: { params: { id: "5" } },
  26. $router: { push: jest.fn() },
  27. $message: { error: jest.fn(), success: jest.fn() }
  28. }
  29. })
  30. }
  31. beforeEach(() => {
  32. getFamilyWorkbench.mockReset()
  33. getFamilyWorkbench.mockResolvedValue({
  34. data: { family: { id: 5, name: "测试家庭", inviteCode: "T5" }, cards: mockCards }
  35. })
  36. })
  37. it("渲染 6 张指标卡并显示总额", async () => {
  38. const wrapper = mountPage()
  39. await wrapper.vm.$nextTick()
  40. await wrapper.vm.$nextTick()
  41. const cards = wrapper.findAll(".wb-card")
  42. expect(cards.length).toBe(6)
  43. expect(wrapper.text()).toContain("9")
  44. expect(wrapper.text()).toContain("100")
  45. wrapper.destroy()
  46. })
  47. it("点击任务卡 -> 打开任务列表抽屉并加载该家庭任务", async () => {
  48. const wrapper = mountPage()
  49. await wrapper.vm.$nextTick()
  50. await wrapper.vm.$nextTick()
  51. wrapper.findAll(".wb-card").at(2).trigger("click")
  52. await wrapper.vm.$nextTick()
  53. expect(wrapper.vm.activeModule).toBe("tasks")
  54. expect(wrapper.vm.taskDrawerVisible).toBe(true)
  55. expect(getFamilyTasks).toHaveBeenCalledWith(5, expect.anything())
  56. wrapper.destroy()
  57. })
  58. it("优惠券卡 -> 深链 family-coupon 页", async () => {
  59. const wrapper = mountPage()
  60. await wrapper.vm.$nextTick()
  61. await wrapper.vm.$nextTick()
  62. wrapper.findAll(".wb-card").at(5).trigger("click")
  63. expect(wrapper.vm.$router.push).toHaveBeenCalledWith(
  64. expect.objectContaining({ path: "/family-coupon", query: { familyId: 5 } })
  65. )
  66. wrapper.destroy()
  67. })
  68. })