Skip to content

Commit cfc978d

Browse files
authored
Refator test (#190)
1 parent c56646f commit cfc978d

File tree

1 file changed

+84
-37
lines changed

1 file changed

+84
-37
lines changed

tests/integration/labor.test.ts

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createClient, createLocation, newTestUuid } from "./helpers";
1+
import { createClient } from "./helpers";
22
import { SquareClient } from "../../src";
33

44
function formatDateString(date: Date): string {
@@ -13,51 +13,74 @@ describe("Labor API", () => {
1313
let shiftId: string;
1414

1515
beforeAll(async () => {
16-
// Get first location
17-
locationId = await createLocation(client);
18-
19-
// Create team member for testing
20-
const teamResponse = await client.teamMembers.create({
21-
idempotencyKey: newTestUuid(),
22-
teamMember: {
23-
givenName: "Sherlock",
24-
familyName: "Holmes",
16+
// Get first available location
17+
const locations = await client.locations.list();
18+
if (!locations.locations?.length) {
19+
throw new Error("No locations available for testing");
20+
}
21+
locationId = locations.locations[0].id!;
22+
23+
// Get first available team member at this location
24+
const teamMembers = await client.teamMembers.search({
25+
query: {
26+
filter: {
27+
locationIds: [locationId],
28+
status: "ACTIVE",
29+
},
2530
},
2631
});
27-
memberId = teamResponse.teamMember!.id!;
32+
if (!teamMembers.teamMembers?.length) {
33+
throw new Error(`No team members available at location ${locationId}`);
34+
}
35+
memberId = teamMembers.teamMembers[0].id!;
2836

2937
// Create break type for testing
3038
const breakResponse = await client.labor.breakTypes.create({
31-
idempotencyKey: newTestUuid(),
39+
idempotencyKey: crypto.randomUUID(),
3240
breakType: {
3341
locationId: locationId,
3442
expectedDuration: "PT0H30M0S", // 30 min duration in RFC 3339 format
35-
breakName: `Lunch_${newTestUuid()}`,
43+
breakName: `Lunch_${crypto.randomUUID()}`,
3644
isPaid: true,
3745
},
3846
});
39-
breakId = breakResponse.breakType!.id!;
47+
if (!breakResponse.breakType?.id) {
48+
throw new Error("Failed to create break type");
49+
}
50+
breakId = breakResponse.breakType.id;
4051

4152
// Create shift for testing
4253
const shiftResponse = await client.labor.shifts.create({
43-
idempotencyKey: newTestUuid(),
54+
idempotencyKey: crypto.randomUUID(),
4455
shift: {
4556
startAt: formatDateString(new Date()),
4657
locationId: locationId,
4758
teamMemberId: memberId,
4859
},
4960
});
50-
shiftId = shiftResponse.shift!.id!;
61+
if (!shiftResponse.shift?.id) {
62+
throw new Error("Failed to create shift");
63+
}
64+
shiftId = shiftResponse.shift.id;
5165
});
5266

5367
afterAll(async () => {
5468
// Clean up resources
55-
await client.labor.shifts.delete({
56-
id: shiftId,
57-
});
58-
await client.labor.breakTypes.delete({
59-
id: breakId,
60-
});
69+
try {
70+
await client.labor.shifts.delete({
71+
id: shiftId,
72+
});
73+
} catch (e) {
74+
// Test may have already deleted the shift
75+
}
76+
77+
try {
78+
await client.labor.breakTypes.delete({
79+
id: breakId,
80+
});
81+
} catch (e) {
82+
// Test may have already deleted the break
83+
}
6184
});
6285

6386
it("should list break types", async () => {
@@ -82,7 +105,7 @@ describe("Labor API", () => {
82105
breakType: {
83106
locationId: locationId,
84107
expectedDuration: "PT1H0M0S", // 1 hour duration
85-
breakName: `Lunch_${newTestUuid()}`,
108+
breakName: `Lunch_${crypto.randomUUID()}`,
86109
isPaid: true,
87110
},
88111
});
@@ -134,39 +157,63 @@ describe("Labor API", () => {
134157
});
135158

136159
it("should delete shift", async () => {
137-
// create team member
138-
const teamMemberResponse = await client.teamMembers.create({
139-
idempotencyKey: newTestUuid(),
140-
teamMember: {
141-
givenName: "Sherlock",
142-
familyName: "Holmes",
160+
// First search for existing shifts for this team member
161+
const existingShifts = await client.labor.shifts.search({
162+
query: {
163+
filter: {
164+
teamMemberIds: [memberId],
165+
},
143166
},
167+
limit: 100,
144168
});
145169

146-
// create shift
170+
// Delete any existing shifts
171+
if (existingShifts.shifts) {
172+
for (const shift of existingShifts.shifts) {
173+
if (shift.id) {
174+
await client.labor.shifts.delete({
175+
id: shift.id,
176+
});
177+
}
178+
}
179+
}
180+
181+
// Start the shift 10 seconds from now and end it 20 seconds from now
182+
const startTime = new Date(Date.now() + 10000);
183+
const endTime = new Date(startTime.getTime() + 10000);
184+
185+
// Create shift
147186
const shiftResponse = await client.labor.shifts.create({
148-
idempotencyKey: newTestUuid(),
187+
idempotencyKey: crypto.randomUUID(),
149188
shift: {
150-
startAt: formatDateString(new Date()),
151189
locationId: locationId,
152-
teamMemberId: teamMemberResponse.teamMember?.id!,
190+
startAt: formatDateString(startTime),
191+
teamMemberId: memberId,
192+
endAt: formatDateString(endTime),
153193
},
154194
});
155195

196+
if (!shiftResponse.shift?.id) {
197+
throw new Error("Failed to create shift");
198+
}
199+
200+
// Add a small delay to ensure the shift is fully created
201+
await new Promise(resolve => setTimeout(resolve, 1000));
202+
156203
const response = await client.labor.shifts.delete({
157-
id: shiftResponse.shift?.id!,
204+
id: shiftResponse.shift.id,
158205
});
159206
expect(response).toBeDefined();
160207
});
161208

162209
it("should delete break type", async () => {
163210
// create break type
164211
const breakResponse = await client.labor.breakTypes.create({
165-
idempotencyKey: newTestUuid(),
212+
idempotencyKey: crypto.randomUUID(),
166213
breakType: {
167214
locationId: locationId,
168215
expectedDuration: "PT0H30M0S",
169-
breakName: `Lunch_${newTestUuid()}`,
216+
breakName: `Lunch_${crypto.randomUUID()}`,
170217
isPaid: true,
171218
},
172219
});
@@ -183,4 +230,4 @@ describe("Labor API", () => {
183230
expect(response.data).toBeDefined();
184231
expect(response.data?.length).toBeGreaterThan(0);
185232
});
186-
});
233+
});

0 commit comments

Comments
 (0)