|
| 1 | +import { PlaneClient } from "../../src/client/plane-client"; |
| 2 | +import { Milestone, UpdateMilestoneRequest, MilestoneWorkItem } from "../../src/models"; |
| 3 | +import { config } from "./constants"; |
| 4 | +import { createTestClient } from "../helpers/test-utils"; |
| 5 | +import { describeIf as describe } from "../helpers/conditional-tests"; |
| 6 | + |
| 7 | +describe(!!(config.workspaceSlug && config.projectId && config.workItemId), "Milestone API Tests", () => { |
| 8 | + let client: PlaneClient; |
| 9 | + let workspaceSlug: string; |
| 10 | + let projectId: string; |
| 11 | + let workItemId: string; |
| 12 | + let milestone: Milestone; |
| 13 | + |
| 14 | + beforeAll(async () => { |
| 15 | + client = createTestClient(); |
| 16 | + workspaceSlug = config.workspaceSlug; |
| 17 | + projectId = config.projectId; |
| 18 | + workItemId = config.workItemId; |
| 19 | + }); |
| 20 | + |
| 21 | + afterAll(async () => { |
| 22 | + // Clean up created resources |
| 23 | + if (milestone?.id) { |
| 24 | + try { |
| 25 | + await client.milestones.delete(workspaceSlug, projectId, milestone.id); |
| 26 | + } catch (error) { |
| 27 | + console.warn("Failed to delete milestone:", error); |
| 28 | + } |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + it("should create a milestone", async () => { |
| 33 | + milestone = await client.milestones.create(workspaceSlug, projectId, { |
| 34 | + title: "Test Milestone", |
| 35 | + }); |
| 36 | + |
| 37 | + expect(milestone).toBeDefined(); |
| 38 | + expect(milestone.id).toBeDefined(); |
| 39 | + expect(milestone.title).toBe("Test Milestone"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should retrieve a milestone", async () => { |
| 43 | + const retrievedMilestone = await client.milestones.retrieve(workspaceSlug, projectId, milestone.id); |
| 44 | + |
| 45 | + expect(retrievedMilestone).toBeDefined(); |
| 46 | + expect(retrievedMilestone.id).toBe(milestone.id); |
| 47 | + expect(retrievedMilestone.title).toBe(milestone.title); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should update a milestone", async () => { |
| 51 | + const updateData: UpdateMilestoneRequest = { |
| 52 | + title: "Updated Test Milestone", |
| 53 | + target_date: "2026-12-31", |
| 54 | + }; |
| 55 | + |
| 56 | + const updatedMilestone = await client.milestones.update( |
| 57 | + workspaceSlug, |
| 58 | + projectId, |
| 59 | + milestone.id, |
| 60 | + updateData |
| 61 | + ); |
| 62 | + |
| 63 | + expect(updatedMilestone).toBeDefined(); |
| 64 | + expect(updatedMilestone.id).toBe(milestone.id); |
| 65 | + expect(updatedMilestone.title).toBe("Updated Test Milestone"); |
| 66 | + expect(updatedMilestone.target_date).toBe("2026-12-31"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should list milestones", async () => { |
| 70 | + const milestones = await client.milestones.list(workspaceSlug, projectId); |
| 71 | + |
| 72 | + expect(milestones).toBeDefined(); |
| 73 | + expect(Array.isArray(milestones.results)).toBe(true); |
| 74 | + expect(milestones.results.length).toBeGreaterThan(0); |
| 75 | + |
| 76 | + const foundMilestone = milestones.results.find((m) => m.id === milestone.id); |
| 77 | + expect(foundMilestone).toBeDefined(); |
| 78 | + expect(foundMilestone?.title).toBe("Updated Test Milestone"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should add work items to milestone", async () => { |
| 82 | + await client.milestones.addWorkItems(workspaceSlug, projectId, milestone.id, [workItemId]); |
| 83 | + |
| 84 | + const workItems = await client.milestones.listWorkItems(workspaceSlug, projectId, milestone.id); |
| 85 | + |
| 86 | + expect(workItems).toBeDefined(); |
| 87 | + expect(Array.isArray(workItems.results)).toBe(true); |
| 88 | + expect(workItems.results.length).toBeGreaterThan(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should list work items in milestone", async () => { |
| 92 | + const workItems = await client.milestones.listWorkItems(workspaceSlug, projectId, milestone.id); |
| 93 | + |
| 94 | + expect(workItems).toBeDefined(); |
| 95 | + expect(Array.isArray(workItems.results)).toBe(true); |
| 96 | + expect(workItems.results.length).toBeGreaterThan(0); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should remove work items from milestone", async () => { |
| 100 | + await client.milestones.removeWorkItems(workspaceSlug, projectId, milestone.id, [workItemId]); |
| 101 | + |
| 102 | + const workItems = await client.milestones.listWorkItems(workspaceSlug, projectId, milestone.id); |
| 103 | + |
| 104 | + expect(workItems).toBeDefined(); |
| 105 | + expect(Array.isArray(workItems.results)).toBe(true); |
| 106 | + |
| 107 | + const foundWorkItem = workItems.results.find((item: MilestoneWorkItem) => item.issue === workItemId); |
| 108 | + expect(foundWorkItem).toBeUndefined(); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should delete a milestone", async () => { |
| 112 | + await client.milestones.delete(workspaceSlug, projectId, milestone.id); |
| 113 | + |
| 114 | + // Verify it's deleted by trying to retrieve it |
| 115 | + try { |
| 116 | + await client.milestones.retrieve(workspaceSlug, projectId, milestone.id); |
| 117 | + fail("Expected an error when retrieving a deleted milestone"); |
| 118 | + } catch (error) { |
| 119 | + expect(error).toBeDefined(); |
| 120 | + } |
| 121 | + |
| 122 | + // Prevent afterAll from trying to delete again |
| 123 | + milestone = undefined as any; |
| 124 | + }); |
| 125 | +}); |
0 commit comments