|
Lines 1-873
Link Here
|
| 1 |
import dayjs = require("dayjs"); |
|
|
| 2 |
|
| 3 |
interface BookingTestContext { |
| 4 |
biblio: any; |
| 5 |
patron: any; |
| 6 |
bookingsToCleanup: Array<{ booking_id: number }>; |
| 7 |
} |
| 8 |
|
| 9 |
interface BookingInterceptOverrides { |
| 10 |
bookableItems?: Cypress.RouteHandler; |
| 11 |
loadBookings?: Cypress.RouteHandler; |
| 12 |
loadCheckouts?: Cypress.RouteHandler; |
| 13 |
pickupLocations?: Cypress.RouteHandler; |
| 14 |
searchPatrons?: Cypress.RouteHandler; |
| 15 |
circulationRules?: Cypress.RouteHandler; |
| 16 |
} |
| 17 |
|
| 18 |
const ensureBookableInventory = (biblio: any) => { |
| 19 |
const itemTypeId = biblio.item_type.item_type_id; |
| 20 |
const itemNumbers = biblio.items.map((item: any) => item.item_id); |
| 21 |
|
| 22 |
const updateItemType = cy.task("query", { |
| 23 |
sql: "UPDATE itemtypes SET bookable=1 WHERE itemtype=?", |
| 24 |
values: [itemTypeId], |
| 25 |
}); |
| 26 |
|
| 27 |
if (!itemNumbers.length) { |
| 28 |
return updateItemType; |
| 29 |
} |
| 30 |
|
| 31 |
const placeholders = itemNumbers.map(() => "?").join(","); |
| 32 |
return updateItemType.then(() => |
| 33 |
cy.task("query", { |
| 34 |
sql: `UPDATE items SET bookable=1 WHERE itemnumber IN (${placeholders})`, |
| 35 |
values: itemNumbers, |
| 36 |
}) |
| 37 |
); |
| 38 |
}; |
| 39 |
|
| 40 |
const ensureBookingCapacity = ({ |
| 41 |
libraryId, |
| 42 |
categoryId, |
| 43 |
itemTypeId, |
| 44 |
}: { |
| 45 |
libraryId: string; |
| 46 |
categoryId: string; |
| 47 |
itemTypeId: string; |
| 48 |
}) => { |
| 49 |
const rules = [ |
| 50 |
{ name: "issuelength", value: 7 }, |
| 51 |
{ name: "renewalsallowed", value: 1 }, |
| 52 |
{ name: "renewalperiod", value: 7 }, |
| 53 |
{ name: "bookings_lead_period", value: 0 }, |
| 54 |
{ name: "bookings_trail_period", value: 0 }, |
| 55 |
]; |
| 56 |
const ruleNames = rules.map(rule => rule.name); |
| 57 |
const deletePlaceholders = ruleNames.map(() => "?").join(","); |
| 58 |
const insertPlaceholders = rules.map(() => "(?, ?, ?, ?, ?)").join(","); |
| 59 |
const insertValues: Array<string | number> = []; |
| 60 |
rules.forEach(rule => { |
| 61 |
insertValues.push( |
| 62 |
libraryId, |
| 63 |
categoryId, |
| 64 |
itemTypeId, |
| 65 |
rule.name, |
| 66 |
String(rule.value) |
| 67 |
); |
| 68 |
}); |
| 69 |
|
| 70 |
return cy |
| 71 |
.task("query", { |
| 72 |
sql: `DELETE FROM circulation_rules WHERE branchcode=? AND categorycode=? AND itemtype=? AND rule_name IN (${deletePlaceholders})`, |
| 73 |
values: [libraryId, categoryId, itemTypeId, ...ruleNames], |
| 74 |
}) |
| 75 |
.then(() => |
| 76 |
cy.task("query", { |
| 77 |
sql: `INSERT INTO circulation_rules (branchcode, categorycode, itemtype, rule_name, rule_value) VALUES ${insertPlaceholders}`, |
| 78 |
values: insertValues, |
| 79 |
}) |
| 80 |
); |
| 81 |
}; |
| 82 |
|
| 83 |
const deleteBookings = (bookingIds: number[]) => { |
| 84 |
if (!bookingIds.length) { |
| 85 |
return cy.wrap(null); |
| 86 |
} |
| 87 |
|
| 88 |
const placeholders = bookingIds.map(() => "?").join(","); |
| 89 |
return cy.task("query", { |
| 90 |
sql: `DELETE FROM bookings WHERE booking_id IN (${placeholders})`, |
| 91 |
values: bookingIds, |
| 92 |
}); |
| 93 |
}; |
| 94 |
|
| 95 |
const waitForBookingIslandReady = () => { |
| 96 |
cy.get("booking-modal-island").should("exist"); |
| 97 |
cy.get("booking-modal-island .modal").should("exist"); |
| 98 |
return cy.wait(100); |
| 99 |
}; |
| 100 |
|
| 101 |
const ensurePatronSearchQueryBuilder = () => |
| 102 |
cy.window().then(win => { |
| 103 |
const globalWin = win as Window & { |
| 104 |
buildPatronSearchQuery?: ( |
| 105 |
term: string, |
| 106 |
options?: Record<string, any> |
| 107 |
) => any; |
| 108 |
}; |
| 109 |
if (typeof globalWin.buildPatronSearchQuery === "function") return; |
| 110 |
globalWin.buildPatronSearchQuery = ( |
| 111 |
term: string, |
| 112 |
options: Record<string, any> = {} |
| 113 |
) => { |
| 114 |
if (!term) return []; |
| 115 |
const table_prefix = options.table_prefix || "me"; |
| 116 |
const search_fields = options.search_fields |
| 117 |
? options.search_fields |
| 118 |
.split(",") |
| 119 |
.map((field: string) => field.trim()) |
| 120 |
: ["surname", "firstname", "cardnumber", "userid"]; |
| 121 |
|
| 122 |
const queries: Record<string, unknown>[] = []; |
| 123 |
search_fields.forEach(field => { |
| 124 |
queries.push({ |
| 125 |
[`${table_prefix}.${field}`]: { |
| 126 |
like: `%${term}%`, |
| 127 |
}, |
| 128 |
}); |
| 129 |
}); |
| 130 |
|
| 131 |
return [{ "-or": queries }]; |
| 132 |
}; |
| 133 |
}); |
| 134 |
|
| 135 |
const prepareBookingModalPage = () => |
| 136 |
waitForBookingIslandReady().then(() => ensurePatronSearchQueryBuilder()); |
| 137 |
|
| 138 |
const setDateRange = (startDate: dayjs.Dayjs, endDate: dayjs.Dayjs) => { |
| 139 |
cy.get(".modal.show #booking_period").click({ force: true }); |
| 140 |
cy.get(".flatpickr-calendar", { timeout: 5000 }).should("be.visible"); |
| 141 |
|
| 142 |
const startDateStr = startDate.format("MMMM D, YYYY"); |
| 143 |
cy.get(".flatpickr-calendar") |
| 144 |
.find(`.flatpickr-day[aria-label="${startDateStr}"]`) |
| 145 |
.click({ force: true }); |
| 146 |
|
| 147 |
const endDateStr = endDate.format("MMMM D, YYYY"); |
| 148 |
cy.get(".flatpickr-calendar") |
| 149 |
.find(`.flatpickr-day[aria-label="${endDateStr}"]`) |
| 150 |
.click({ force: true }); |
| 151 |
|
| 152 |
cy.wait(500); |
| 153 |
}; |
| 154 |
|
| 155 |
const interceptBookingModalData = ( |
| 156 |
biblionumber: number | string, |
| 157 |
overrides: BookingInterceptOverrides = {} |
| 158 |
) => { |
| 159 |
const itemsUrl = `/api/v1/biblios/${biblionumber}/items*`; |
| 160 |
if (overrides.bookableItems) { |
| 161 |
cy.intercept("GET", itemsUrl, overrides.bookableItems).as( |
| 162 |
"bookableItems" |
| 163 |
); |
| 164 |
} else { |
| 165 |
cy.intercept("GET", itemsUrl).as("bookableItems"); |
| 166 |
} |
| 167 |
|
| 168 |
const bookingsUrl = `/api/v1/biblios/${biblionumber}/bookings*`; |
| 169 |
if (overrides.loadBookings) { |
| 170 |
cy.intercept("GET", bookingsUrl, overrides.loadBookings).as( |
| 171 |
"loadBookings" |
| 172 |
); |
| 173 |
} else { |
| 174 |
cy.intercept("GET", bookingsUrl).as("loadBookings"); |
| 175 |
} |
| 176 |
|
| 177 |
const checkoutsUrl = `/api/v1/biblios/${biblionumber}/checkouts*`; |
| 178 |
if (overrides.loadCheckouts) { |
| 179 |
cy.intercept("GET", checkoutsUrl, overrides.loadCheckouts).as( |
| 180 |
"loadCheckouts" |
| 181 |
); |
| 182 |
} else { |
| 183 |
cy.intercept("GET", checkoutsUrl).as("loadCheckouts"); |
| 184 |
} |
| 185 |
|
| 186 |
const pickupsUrl = `/api/v1/biblios/${biblionumber}/pickup_locations*`; |
| 187 |
if (overrides.pickupLocations) { |
| 188 |
cy.intercept("GET", pickupsUrl, overrides.pickupLocations).as( |
| 189 |
"pickupLocations" |
| 190 |
); |
| 191 |
} else { |
| 192 |
cy.intercept("GET", pickupsUrl).as("pickupLocations"); |
| 193 |
} |
| 194 |
|
| 195 |
const searchUrl = /\/api\/v1\/patrons\?.*q=.*/; |
| 196 |
if (overrides.searchPatrons) { |
| 197 |
cy.intercept("GET", searchUrl, overrides.searchPatrons).as( |
| 198 |
"searchPatrons" |
| 199 |
); |
| 200 |
} else { |
| 201 |
cy.intercept("GET", searchUrl).as("searchPatrons"); |
| 202 |
} |
| 203 |
|
| 204 |
const rulesUrl = /\/api\/v1\/circulation_rules.*/; |
| 205 |
if (overrides.circulationRules) { |
| 206 |
cy.intercept("GET", rulesUrl, overrides.circulationRules).as( |
| 207 |
"circulationRules" |
| 208 |
); |
| 209 |
} else { |
| 210 |
cy.intercept("GET", rulesUrl).as("circulationRules"); |
| 211 |
} |
| 212 |
}; |
| 213 |
|
| 214 |
const openBookingModalFromList = (biblionumber: number | string) => { |
| 215 |
cy.window().its("openBookingModal").should("be.a", "function"); |
| 216 |
cy.contains("button[data-booking-modal]", /Place booking/i).should( |
| 217 |
"be.visible" |
| 218 |
); |
| 219 |
|
| 220 |
cy.window().then(win => { |
| 221 |
const globalWin = win as Window & { |
| 222 |
openBookingModal?: (props: Record<string, any>) => void; |
| 223 |
}; |
| 224 |
if (typeof globalWin.openBookingModal === "function") { |
| 225 |
globalWin.openBookingModal({ biblionumber: String(biblionumber) }); |
| 226 |
} else { |
| 227 |
throw new Error("window.openBookingModal is not available"); |
| 228 |
} |
| 229 |
}); |
| 230 |
|
| 231 |
cy.wait(["@bookableItems", "@loadBookings"], { timeout: 20000 }); |
| 232 |
cy.get(".modal", { timeout: 15000 }) |
| 233 |
.should("exist") |
| 234 |
.and("have.class", "show") |
| 235 |
.and("be.visible"); |
| 236 |
}; |
| 237 |
|
| 238 |
const selectVsOption = (text: string) => { |
| 239 |
cy.get(".modal.show .vs__dropdown-menu li:not(.vs__no-options)", { |
| 240 |
timeout: 10000, |
| 241 |
}) |
| 242 |
.contains(text) |
| 243 |
.scrollIntoView() |
| 244 |
.click({ force: true }); |
| 245 |
}; |
| 246 |
|
| 247 |
const selectPatronAndInventory = ( |
| 248 |
patron: any, |
| 249 |
pickupLibrary: any, |
| 250 |
item: any, |
| 251 |
options: { skipItemSelection?: boolean } = {} |
| 252 |
) => { |
| 253 |
const searchTerm = patron.cardnumber.slice(0, 4); |
| 254 |
|
| 255 |
cy.get(".modal.show #booking_patron") |
| 256 |
.clear() |
| 257 |
.type(searchTerm, { delay: 0 }); |
| 258 |
cy.wait("@searchPatrons"); |
| 259 |
cy.wait(200); |
| 260 |
selectVsOption(patron.cardnumber); |
| 261 |
|
| 262 |
cy.wait("@pickupLocations"); |
| 263 |
cy.get(".modal.show #pickup_library_id").should( |
| 264 |
"not.have.attr", |
| 265 |
"disabled" |
| 266 |
); |
| 267 |
cy.get(".modal.show #pickup_library_id").click({ force: true }); |
| 268 |
cy.wait(200); |
| 269 |
selectVsOption(pickupLibrary.name); |
| 270 |
|
| 271 |
if (!options.skipItemSelection) { |
| 272 |
cy.get(".modal.show #booking_item_id").click({ force: true }); |
| 273 |
cy.wait(200); |
| 274 |
selectVsOption(item.external_id); |
| 275 |
cy.wait("@circulationRules"); |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
describe("BookingModal integration", () => { |
| 280 |
// Prevent app-level warnings (e.g. from console.warn → error in e2e.js) from failing tests |
| 281 |
Cypress.on("uncaught:exception", () => false); |
| 282 |
|
| 283 |
beforeEach(function (this: BookingTestContext) { |
| 284 |
cy.login(); |
| 285 |
cy.title().should("eq", "Koha staff interface"); |
| 286 |
|
| 287 |
return cy |
| 288 |
.task("insertSampleBiblio", { item_count: 1 }) |
| 289 |
.then(biblio => { |
| 290 |
this.biblio = biblio; |
| 291 |
return ensureBookableInventory(biblio); |
| 292 |
}) |
| 293 |
.then(() => |
| 294 |
cy.task("insertSamplePatron", { |
| 295 |
library: this.biblio.libraries[0], |
| 296 |
}) |
| 297 |
) |
| 298 |
.then(patron => { |
| 299 |
this.patron = patron; |
| 300 |
this.bookingsToCleanup = []; |
| 301 |
return ensureBookingCapacity({ |
| 302 |
libraryId: this.biblio.libraries[0].library_id, |
| 303 |
categoryId: patron.patron.category_id, |
| 304 |
itemTypeId: this.biblio.item_type.item_type_id, |
| 305 |
}); |
| 306 |
}); |
| 307 |
}); |
| 308 |
|
| 309 |
afterEach(function (this: BookingTestContext) { |
| 310 |
const bookingIds = |
| 311 |
this.bookingsToCleanup?.map(booking => booking.booking_id) || []; |
| 312 |
if (bookingIds.length) { |
| 313 |
deleteBookings(bookingIds); |
| 314 |
} |
| 315 |
|
| 316 |
const cleanupTargets = []; |
| 317 |
if (this.patron) cleanupTargets.push(this.patron); |
| 318 |
if (this.biblio) cleanupTargets.push(this.biblio); |
| 319 |
if (cleanupTargets.length) { |
| 320 |
cy.task("deleteSampleObjects", cleanupTargets); |
| 321 |
} |
| 322 |
}); |
| 323 |
|
| 324 |
it("Creates a booking", function (this: BookingTestContext) { |
| 325 |
const ctx = this as BookingTestContext; |
| 326 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 327 |
const patron = ctx.patron.patron; |
| 328 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 329 |
const item = ctx.biblio.items[0]; |
| 330 |
const startDate = dayjs().add(3, "day").startOf("day"); |
| 331 |
const endDate = startDate.add(2, "day"); |
| 332 |
|
| 333 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 334 |
cy.get("#bookings_table").should("exist"); |
| 335 |
prepareBookingModalPage(); |
| 336 |
|
| 337 |
interceptBookingModalData(biblionumber); |
| 338 |
cy.intercept("POST", "/api/v1/bookings").as("createBooking"); |
| 339 |
|
| 340 |
openBookingModalFromList(biblionumber); |
| 341 |
|
| 342 |
cy.get(".modal-title").should("contain", "Place booking"); |
| 343 |
cy.contains(".step-header", "Select Patron").should("exist"); |
| 344 |
cy.get("button[form='form-booking']").should("be.disabled"); |
| 345 |
cy.get("#pickup_library_id").should("have.attr", "disabled"); |
| 346 |
|
| 347 |
selectPatronAndInventory(patron, pickupLibrary, item); |
| 348 |
cy.get("#booking_period").should("not.be.disabled"); |
| 349 |
setDateRange(startDate, endDate); |
| 350 |
cy.wait(1000); |
| 351 |
|
| 352 |
cy.get("button[form='form-booking']", { timeout: 15000 }) |
| 353 |
.should("not.be.disabled") |
| 354 |
.contains("Place booking") |
| 355 |
.click(); |
| 356 |
|
| 357 |
cy.wait("@createBooking").then(({ response }) => { |
| 358 |
expect(response?.statusCode).to.eq(201); |
| 359 |
if (response?.body) { |
| 360 |
ctx.bookingsToCleanup.push(response.body); |
| 361 |
} |
| 362 |
}); |
| 363 |
|
| 364 |
cy.get("body").should("not.have.class", "modal-open"); |
| 365 |
cy.contains("#bookings_table tbody tr", item.external_id).should( |
| 366 |
"exist" |
| 367 |
); |
| 368 |
cy.contains("#bookings_table tbody tr", patron.cardnumber).should( |
| 369 |
"exist" |
| 370 |
); |
| 371 |
}); |
| 372 |
|
| 373 |
it("Updates a booking", function (this: BookingTestContext) { |
| 374 |
const ctx = this as BookingTestContext; |
| 375 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 376 |
const patron = ctx.patron.patron; |
| 377 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 378 |
const item = ctx.biblio.items[0]; |
| 379 |
const initialStart = dayjs().add(5, "day").startOf("day"); |
| 380 |
const initialEnd = initialStart.add(1, "day"); |
| 381 |
const updatedStart = initialStart.add(3, "day"); |
| 382 |
const updatedEnd = updatedStart.add(2, "day"); |
| 383 |
|
| 384 |
cy.task("apiPost", { |
| 385 |
endpoint: "/api/v1/bookings", |
| 386 |
body: { |
| 387 |
biblio_id: biblionumber, |
| 388 |
patron_id: patron.patron_id, |
| 389 |
pickup_library_id: pickupLibrary.library_id, |
| 390 |
item_id: item.item_id, |
| 391 |
start_date: initialStart.toISOString(), |
| 392 |
end_date: initialEnd.toISOString(), |
| 393 |
}, |
| 394 |
}).then(booking => { |
| 395 |
ctx.bookingsToCleanup.push(booking); |
| 396 |
cy.wrap(booking).as("existingBookingRecord"); |
| 397 |
}); |
| 398 |
|
| 399 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 400 |
cy.get("#bookings_table").should("exist"); |
| 401 |
prepareBookingModalPage(); |
| 402 |
|
| 403 |
cy.get("@existingBookingRecord").then((booking: any) => { |
| 404 |
interceptBookingModalData(biblionumber); |
| 405 |
cy.intercept("GET", /\/api\/v1\/patrons\/\d+/).as( |
| 406 |
"prefillPatron" |
| 407 |
); |
| 408 |
cy.intercept("GET", /\/api\/v1\/circulation_rules.*/).as( |
| 409 |
"circulationRules" |
| 410 |
); |
| 411 |
cy.intercept("PUT", `/api/v1/bookings/${booking.booking_id}`).as( |
| 412 |
"updateBooking" |
| 413 |
); |
| 414 |
|
| 415 |
cy.contains("#bookings_table tbody tr", `(${booking.booking_id})`, { |
| 416 |
timeout: 10000, |
| 417 |
}) |
| 418 |
.should("exist") |
| 419 |
.find("button.edit-action") |
| 420 |
.click(); |
| 421 |
}); |
| 422 |
|
| 423 |
cy.wait(["@bookableItems", "@loadBookings"]); |
| 424 |
cy.wait("@prefillPatron").its("response.statusCode").should("eq", 200); |
| 425 |
cy.wait("@pickupLocations", { timeout: 20000 }); |
| 426 |
cy.wait("@circulationRules", { timeout: 20000 }); |
| 427 |
cy.get(".modal.show", { timeout: 10000 }).should("be.visible"); |
| 428 |
|
| 429 |
cy.get(".modal-title").should("contain", "Edit booking"); |
| 430 |
cy.get("button[form='form-booking']").should( |
| 431 |
"contain", |
| 432 |
"Update booking" |
| 433 |
); |
| 434 |
cy.contains(".vs__selected", patron.cardnumber).should("exist"); |
| 435 |
cy.contains(".vs__selected", pickupLibrary.name).should("exist"); |
| 436 |
cy.contains(".vs__selected", item.external_id).should("exist"); |
| 437 |
|
| 438 |
cy.get("#booking_period").then($input => { |
| 439 |
const fp = ($input[0] as any)?._flatpickr; |
| 440 |
expect(fp?.selectedDates?.length).to.eq(2); |
| 441 |
expect(dayjs(fp.selectedDates[0]).isSame(initialStart, "day")).to.be |
| 442 |
.true; |
| 443 |
}); |
| 444 |
|
| 445 |
setDateRange(updatedStart, updatedEnd); |
| 446 |
cy.wait(1000); |
| 447 |
|
| 448 |
cy.get("button[form='form-booking']", { timeout: 30000 }) |
| 449 |
.should("not.be.disabled") |
| 450 |
.and("contain", "Update booking") |
| 451 |
.click(); |
| 452 |
|
| 453 |
cy.wait("@updateBooking").its("response.statusCode").should("eq", 200); |
| 454 |
|
| 455 |
cy.get("@existingBookingRecord").then((booking: any) => { |
| 456 |
cy.task("query", { |
| 457 |
sql: "SELECT start_date, end_date FROM bookings WHERE booking_id=?", |
| 458 |
values: [booking.booking_id], |
| 459 |
}).then(rows => { |
| 460 |
expect(rows).to.have.length(1); |
| 461 |
expect(dayjs(rows[0].start_date).isSame(updatedStart, "day")).to |
| 462 |
.be.true; |
| 463 |
expect(dayjs(rows[0].end_date).isSame(updatedEnd, "day")).to.be |
| 464 |
.true; |
| 465 |
}); |
| 466 |
}); |
| 467 |
}); |
| 468 |
|
| 469 |
it("Resets the modal state after canceling", function (this: BookingTestContext) { |
| 470 |
const ctx = this as BookingTestContext; |
| 471 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 472 |
const patron = ctx.patron.patron; |
| 473 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 474 |
const item = ctx.biblio.items[0]; |
| 475 |
const startDate = dayjs().add(2, "day").startOf("day"); |
| 476 |
const endDate = startDate.add(1, "day"); |
| 477 |
|
| 478 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 479 |
cy.get("#bookings_table").should("exist"); |
| 480 |
prepareBookingModalPage(); |
| 481 |
|
| 482 |
interceptBookingModalData(biblionumber); |
| 483 |
|
| 484 |
openBookingModalFromList(biblionumber); |
| 485 |
cy.get("button[form='form-booking']").should("be.disabled"); |
| 486 |
cy.get("#pickup_library_id").should("have.attr", "disabled"); |
| 487 |
|
| 488 |
selectPatronAndInventory(patron, pickupLibrary, item); |
| 489 |
setDateRange(startDate, endDate); |
| 490 |
cy.wait(1000); |
| 491 |
|
| 492 |
cy.get("button[form='form-booking']", { timeout: 15000 }).should( |
| 493 |
"not.be.disabled" |
| 494 |
); |
| 495 |
|
| 496 |
cy.contains(".modal.show button", /Cancel/i).click(); |
| 497 |
cy.get(".modal.show").should("not.exist"); |
| 498 |
cy.get("body").should("not.have.class", "modal-open"); |
| 499 |
|
| 500 |
openBookingModalFromList(biblionumber); |
| 501 |
cy.get(".modal.show").within(() => { |
| 502 |
cy.get("#booking_patron").should("have.value", ""); |
| 503 |
cy.get("#pickup_library_id").should("have.attr", "disabled"); |
| 504 |
cy.get("#booking_period").should("have.value", ""); |
| 505 |
cy.get("button[form='form-booking']").should("be.disabled"); |
| 506 |
}); |
| 507 |
}); |
| 508 |
|
| 509 |
it("Shows capacity warning for zero-day circulation rules", function (this: BookingTestContext) { |
| 510 |
const ctx = this as BookingTestContext; |
| 511 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 512 |
const patron = ctx.patron.patron; |
| 513 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 514 |
const item = ctx.biblio.items[0]; |
| 515 |
|
| 516 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 517 |
cy.get("#bookings_table").should("exist"); |
| 518 |
prepareBookingModalPage(); |
| 519 |
|
| 520 |
interceptBookingModalData(biblionumber, { |
| 521 |
circulationRules: req => { |
| 522 |
req.reply({ |
| 523 |
statusCode: 200, |
| 524 |
body: [ |
| 525 |
{ |
| 526 |
library_id: pickupLibrary.library_id, |
| 527 |
item_type_id: item.item_type_id, |
| 528 |
patron_category_id: patron.category_id, |
| 529 |
issuelength: 0, |
| 530 |
renewalsallowed: 0, |
| 531 |
renewalperiod: 0, |
| 532 |
bookings_lead_period: 0, |
| 533 |
bookings_trail_period: 0, |
| 534 |
calculated_period_days: 0, |
| 535 |
}, |
| 536 |
], |
| 537 |
}); |
| 538 |
}, |
| 539 |
}); |
| 540 |
|
| 541 |
openBookingModalFromList(biblionumber); |
| 542 |
selectPatronAndInventory(patron, pickupLibrary, item); |
| 543 |
|
| 544 |
cy.get(".modal.show .alert-warning") |
| 545 |
.scrollIntoView() |
| 546 |
.should("be.visible") |
| 547 |
.and("contain", "Bookings are not permitted for this combination"); |
| 548 |
cy.get("#booking_period").should("be.disabled"); |
| 549 |
cy.get("button[form='form-booking']").should("be.disabled"); |
| 550 |
}); |
| 551 |
|
| 552 |
it("Creates a booking without selecting a specific item", function (this: BookingTestContext) { |
| 553 |
const ctx = this as BookingTestContext; |
| 554 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 555 |
const patron = ctx.patron.patron; |
| 556 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 557 |
const startDate = dayjs().add(3, "day").startOf("day"); |
| 558 |
const endDate = startDate.add(2, "day"); |
| 559 |
|
| 560 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 561 |
cy.get("#bookings_table").should("exist"); |
| 562 |
prepareBookingModalPage(); |
| 563 |
|
| 564 |
interceptBookingModalData(biblionumber); |
| 565 |
cy.intercept("POST", "/api/v1/bookings").as("createBooking"); |
| 566 |
|
| 567 |
openBookingModalFromList(biblionumber); |
| 568 |
|
| 569 |
selectPatronAndInventory(patron, pickupLibrary, null, { |
| 570 |
skipItemSelection: true, |
| 571 |
}); |
| 572 |
cy.get("#booking_period").should("not.be.disabled"); |
| 573 |
setDateRange(startDate, endDate); |
| 574 |
cy.wait(1000); |
| 575 |
|
| 576 |
cy.get("button[form='form-booking']", { timeout: 15000 }) |
| 577 |
.should("not.be.disabled") |
| 578 |
.contains("Place booking") |
| 579 |
.click(); |
| 580 |
|
| 581 |
cy.wait("@createBooking").then(({ response }) => { |
| 582 |
expect(response?.statusCode).to.eq(201); |
| 583 |
// With only 1 bookable item the modal auto-assigns item_id client-side. |
| 584 |
// With 2+ items the server performs optimal selection. |
| 585 |
// Either way the response must contain a resolved item_id. |
| 586 |
expect(response?.body.item_id).to.not.be.null; |
| 587 |
if (response?.body) { |
| 588 |
ctx.bookingsToCleanup.push(response.body); |
| 589 |
} |
| 590 |
}); |
| 591 |
|
| 592 |
cy.get("body").should("not.have.class", "modal-open"); |
| 593 |
}); |
| 594 |
|
| 595 |
it("Shows error message when booking creation fails with 400", function (this: BookingTestContext) { |
| 596 |
const ctx = this as BookingTestContext; |
| 597 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 598 |
const patron = ctx.patron.patron; |
| 599 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 600 |
const item = ctx.biblio.items[0]; |
| 601 |
const startDate = dayjs().add(3, "day").startOf("day"); |
| 602 |
const endDate = startDate.add(2, "day"); |
| 603 |
|
| 604 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 605 |
cy.get("#bookings_table").should("exist"); |
| 606 |
prepareBookingModalPage(); |
| 607 |
|
| 608 |
interceptBookingModalData(biblionumber); |
| 609 |
cy.intercept("POST", "/api/v1/bookings", { |
| 610 |
statusCode: 400, |
| 611 |
body: { |
| 612 |
error: "Invalid booking period", |
| 613 |
}, |
| 614 |
}).as("createBooking"); |
| 615 |
|
| 616 |
openBookingModalFromList(biblionumber); |
| 617 |
selectPatronAndInventory(patron, pickupLibrary, item); |
| 618 |
setDateRange(startDate, endDate); |
| 619 |
cy.wait(1000); |
| 620 |
|
| 621 |
cy.get("button[form='form-booking']", { timeout: 15000 }) |
| 622 |
.should("not.be.disabled") |
| 623 |
.click(); |
| 624 |
|
| 625 |
cy.wait("@createBooking"); |
| 626 |
cy.get(".modal.show .alert-danger, .modal.show .alert-warning") |
| 627 |
.scrollIntoView() |
| 628 |
.should("be.visible"); |
| 629 |
cy.get(".modal.show").should("exist"); |
| 630 |
}); |
| 631 |
|
| 632 |
it("Shows error message when booking creation fails with 409 conflict", function (this: BookingTestContext) { |
| 633 |
const ctx = this as BookingTestContext; |
| 634 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 635 |
const patron = ctx.patron.patron; |
| 636 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 637 |
const item = ctx.biblio.items[0]; |
| 638 |
const startDate = dayjs().add(3, "day").startOf("day"); |
| 639 |
const endDate = startDate.add(2, "day"); |
| 640 |
|
| 641 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 642 |
cy.get("#bookings_table").should("exist"); |
| 643 |
prepareBookingModalPage(); |
| 644 |
|
| 645 |
interceptBookingModalData(biblionumber); |
| 646 |
cy.intercept("POST", "/api/v1/bookings", { |
| 647 |
statusCode: 409, |
| 648 |
body: { |
| 649 |
error: "Booking conflict detected", |
| 650 |
}, |
| 651 |
}).as("createBooking"); |
| 652 |
|
| 653 |
openBookingModalFromList(biblionumber); |
| 654 |
selectPatronAndInventory(patron, pickupLibrary, item); |
| 655 |
setDateRange(startDate, endDate); |
| 656 |
cy.wait(1000); |
| 657 |
|
| 658 |
cy.get("button[form='form-booking']", { timeout: 15000 }) |
| 659 |
.should("not.be.disabled") |
| 660 |
.click(); |
| 661 |
|
| 662 |
cy.wait("@createBooking"); |
| 663 |
cy.get(".modal.show .alert-danger, .modal.show .alert-warning") |
| 664 |
.scrollIntoView() |
| 665 |
.should("be.visible"); |
| 666 |
cy.get(".modal.show").should("exist"); |
| 667 |
}); |
| 668 |
|
| 669 |
it("Shows error message when booking creation fails with 500", function (this: BookingTestContext) { |
| 670 |
const ctx = this as BookingTestContext; |
| 671 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 672 |
const patron = ctx.patron.patron; |
| 673 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 674 |
const item = ctx.biblio.items[0]; |
| 675 |
const startDate = dayjs().add(3, "day").startOf("day"); |
| 676 |
const endDate = startDate.add(2, "day"); |
| 677 |
|
| 678 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 679 |
cy.get("#bookings_table").should("exist"); |
| 680 |
prepareBookingModalPage(); |
| 681 |
|
| 682 |
interceptBookingModalData(biblionumber); |
| 683 |
cy.intercept("POST", "/api/v1/bookings", { |
| 684 |
statusCode: 500, |
| 685 |
body: { |
| 686 |
error: "Internal server error", |
| 687 |
}, |
| 688 |
}).as("createBooking"); |
| 689 |
|
| 690 |
openBookingModalFromList(biblionumber); |
| 691 |
selectPatronAndInventory(patron, pickupLibrary, item); |
| 692 |
setDateRange(startDate, endDate); |
| 693 |
cy.wait(1000); |
| 694 |
|
| 695 |
cy.get("button[form='form-booking']", { timeout: 15000 }) |
| 696 |
.should("not.be.disabled") |
| 697 |
.click(); |
| 698 |
|
| 699 |
cy.wait("@createBooking"); |
| 700 |
cy.get(".modal.show .alert-danger, .modal.show .alert-warning") |
| 701 |
.scrollIntoView() |
| 702 |
.should("be.visible"); |
| 703 |
cy.get(".modal.show").should("exist"); |
| 704 |
}); |
| 705 |
|
| 706 |
it("Shows message when patron search returns no results", function (this: BookingTestContext) { |
| 707 |
const ctx = this as BookingTestContext; |
| 708 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 709 |
|
| 710 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 711 |
cy.get("#bookings_table").should("exist"); |
| 712 |
prepareBookingModalPage(); |
| 713 |
|
| 714 |
interceptBookingModalData(biblionumber, { |
| 715 |
searchPatrons: { |
| 716 |
statusCode: 200, |
| 717 |
body: [], |
| 718 |
headers: { |
| 719 |
"X-Base-Total-Count": "0", |
| 720 |
"X-Total-Count": "0", |
| 721 |
}, |
| 722 |
}, |
| 723 |
}); |
| 724 |
|
| 725 |
openBookingModalFromList(biblionumber); |
| 726 |
|
| 727 |
cy.get(".modal.show #booking_patron") |
| 728 |
.clear() |
| 729 |
.type("NONEXISTENT", { delay: 0 }); |
| 730 |
cy.wait("@searchPatrons"); |
| 731 |
cy.wait(500); |
| 732 |
|
| 733 |
cy.get(".modal.show .vs__no-options").should("be.visible"); |
| 734 |
}); |
| 735 |
|
| 736 |
it("Shows no options when no pickup locations are available", function (this: BookingTestContext) { |
| 737 |
const ctx = this as BookingTestContext; |
| 738 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 739 |
const patron = ctx.patron.patron; |
| 740 |
|
| 741 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 742 |
cy.get("#bookings_table").should("exist"); |
| 743 |
prepareBookingModalPage(); |
| 744 |
|
| 745 |
interceptBookingModalData(biblionumber, { |
| 746 |
pickupLocations: { |
| 747 |
statusCode: 200, |
| 748 |
body: [], |
| 749 |
headers: { |
| 750 |
"X-Base-Total-Count": "0", |
| 751 |
"X-Total-Count": "0", |
| 752 |
}, |
| 753 |
}, |
| 754 |
}); |
| 755 |
|
| 756 |
openBookingModalFromList(biblionumber); |
| 757 |
|
| 758 |
const searchTerm = patron.cardnumber.slice(0, 4); |
| 759 |
cy.get(".modal.show #booking_patron") |
| 760 |
.clear() |
| 761 |
.type(searchTerm, { delay: 0 }); |
| 762 |
cy.wait("@searchPatrons"); |
| 763 |
cy.wait(200); |
| 764 |
selectVsOption(patron.cardnumber); |
| 765 |
|
| 766 |
cy.wait("@pickupLocations"); |
| 767 |
cy.wait(500); |
| 768 |
|
| 769 |
cy.get(".modal.show #pickup_library_id").click({ force: true }); |
| 770 |
cy.wait(200); |
| 771 |
cy.get(".modal.show .vs__no-options").should("be.visible"); |
| 772 |
}); |
| 773 |
|
| 774 |
it("Shows conflicting dates when an item is already booked", function (this: BookingTestContext) { |
| 775 |
const ctx = this as BookingTestContext; |
| 776 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 777 |
const patron = ctx.patron.patron; |
| 778 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 779 |
const item = ctx.biblio.items[0]; |
| 780 |
const conflictStart = dayjs().add(5, "day").startOf("day"); |
| 781 |
const conflictEnd = conflictStart.add(2, "day"); |
| 782 |
|
| 783 |
cy.task("apiPost", { |
| 784 |
endpoint: "/api/v1/bookings", |
| 785 |
body: { |
| 786 |
biblio_id: biblionumber, |
| 787 |
patron_id: patron.patron_id, |
| 788 |
pickup_library_id: pickupLibrary.library_id, |
| 789 |
item_id: item.item_id, |
| 790 |
start_date: conflictStart.toISOString(), |
| 791 |
end_date: conflictEnd.toISOString(), |
| 792 |
}, |
| 793 |
}).then(booking => { |
| 794 |
ctx.bookingsToCleanup.push(booking); |
| 795 |
}); |
| 796 |
|
| 797 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 798 |
cy.get("#bookings_table").should("exist"); |
| 799 |
prepareBookingModalPage(); |
| 800 |
|
| 801 |
interceptBookingModalData(biblionumber); |
| 802 |
|
| 803 |
openBookingModalFromList(biblionumber); |
| 804 |
selectPatronAndInventory(patron, pickupLibrary, item); |
| 805 |
|
| 806 |
cy.get(".modal.show #booking_period").click({ force: true }); |
| 807 |
cy.get(".flatpickr-calendar", { timeout: 5000 }).should("be.visible"); |
| 808 |
|
| 809 |
const conflictDateStr = conflictStart.format("MMMM D, YYYY"); |
| 810 |
cy.get(".flatpickr-calendar") |
| 811 |
.find(`.flatpickr-day[aria-label="${conflictDateStr}"]`) |
| 812 |
.should("have.class", "flatpickr-disabled"); |
| 813 |
}); |
| 814 |
|
| 815 |
it("Creates a booking when item type is auto-selected", function (this: BookingTestContext) { |
| 816 |
const ctx = this as BookingTestContext; |
| 817 |
const biblionumber = ctx.biblio.biblio.biblio_id; |
| 818 |
const patron = ctx.patron.patron; |
| 819 |
const pickupLibrary = ctx.biblio.libraries[0]; |
| 820 |
const startDate = dayjs().add(3, "day").startOf("day"); |
| 821 |
const endDate = startDate.add(2, "day"); |
| 822 |
|
| 823 |
cy.visit(`/cgi-bin/koha/bookings/list.pl?biblionumber=${biblionumber}`); |
| 824 |
cy.get("#bookings_table").should("exist"); |
| 825 |
prepareBookingModalPage(); |
| 826 |
|
| 827 |
interceptBookingModalData(biblionumber); |
| 828 |
cy.intercept("POST", "/api/v1/bookings").as("createBooking"); |
| 829 |
|
| 830 |
openBookingModalFromList(biblionumber); |
| 831 |
|
| 832 |
const searchTerm = patron.cardnumber.slice(0, 4); |
| 833 |
cy.get(".modal.show #booking_patron") |
| 834 |
.clear() |
| 835 |
.type(searchTerm, { delay: 0 }); |
| 836 |
cy.wait("@searchPatrons"); |
| 837 |
cy.wait(200); |
| 838 |
selectVsOption(patron.cardnumber); |
| 839 |
|
| 840 |
cy.wait("@pickupLocations"); |
| 841 |
cy.get(".modal.show #pickup_library_id").should( |
| 842 |
"not.have.attr", |
| 843 |
"disabled" |
| 844 |
); |
| 845 |
cy.get(".modal.show #pickup_library_id").click({ force: true }); |
| 846 |
cy.wait(200); |
| 847 |
selectVsOption(pickupLibrary.name); |
| 848 |
|
| 849 |
cy.wait("@circulationRules"); |
| 850 |
cy.wait(500); |
| 851 |
|
| 852 |
cy.get("#booking_period").should("not.be.disabled"); |
| 853 |
setDateRange(startDate, endDate); |
| 854 |
cy.wait(1000); |
| 855 |
|
| 856 |
cy.get("button[form='form-booking']", { timeout: 15000 }) |
| 857 |
.should("not.be.disabled") |
| 858 |
.contains("Place booking") |
| 859 |
.click(); |
| 860 |
|
| 861 |
cy.wait("@createBooking").then(({ response }) => { |
| 862 |
expect(response?.statusCode).to.eq(201); |
| 863 |
// With auto-selected item type and 1 item, the modal auto-assigns. |
| 864 |
// The response must contain a resolved item_id. |
| 865 |
expect(response?.body.item_id).to.not.be.null; |
| 866 |
if (response?.body) { |
| 867 |
ctx.bookingsToCleanup.push(response.body); |
| 868 |
} |
| 869 |
}); |
| 870 |
|
| 871 |
cy.get("body").should("not.have.class", "modal-open"); |
| 872 |
}); |
| 873 |
}); |