Bug 34448: Update cypress tests
[koha.git] / t / cypress / integration / ERM / Titles_spec.ts
1 import { mount } from "@cypress/vue";
2 const dayjs = require("dayjs"); /* Cannot use our calendar JS code, it's in an include file (!)
3                                    Also note that moment.js is deprecated */
4
5 function get_packages_to_relate() {
6     return [
7         {
8             package_id: 1,
9             description: "a package",
10             name: "first package name",
11         },
12         {
13             package_id: 2,
14             description: "a second package",
15             name: "second package name",
16         },
17     ];
18 }
19
20 describe("Title CRUD operations", () => {
21     beforeEach(() => {
22         cy.login();
23         cy.title().should("eq", "Koha staff interface");
24         cy.intercept(
25             "GET",
26             "/cgi-bin/koha/svc/config/systempreferences/?pref=ERMModule",
27             '{"value":"1"}'
28         );
29         cy.intercept(
30             "GET",
31             "/cgi-bin/koha/svc/config/systempreferences/?pref=ERMProviders",
32             '{"value":"local"}'
33         );
34     });
35
36     it("Import titles", () => {
37         let erm_title = cy.get_title();
38         let resource = erm_title.resources[0];
39
40         // Create a list in case none exists
41         cy.visit("/cgi-bin/koha/virtualshelves/shelves.pl");
42         cy.contains("New list").click();
43         cy.get("#shelfname").type("list name");
44         cy.contains("Save").click();
45
46         // First attempt to import list has no packages
47         cy.intercept("GET", "/api/v1/erm/eholdings/local/packages*", {
48             statusCode: 200,
49             body: [],
50         }).as("get-empty-packages");
51         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles");
52         cy.wait(500);
53         cy.get("#toolbar a").contains("Import from list").click();
54         cy.get("h2").contains("Import from a list");
55         cy.get("#package_list .vs__selected").should("not.exist");
56
57         // Make sure packages are returned
58         cy.intercept("GET", "/api/v1/erm/eholdings/local/packages*", {
59             statusCode: 200,
60             body: get_packages_to_relate(),
61         }).as("get-related-packages");
62         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles");
63         cy.get("#toolbar a").contains("Import from list").click();
64         cy.get("h2").contains("Import from a list");
65         cy.wait(500);
66
67         // Prepare background job response to the POST
68         cy.intercept("POST", "/api/v1/erm/eholdings/local/titles/import", {
69             statusCode: 200,
70             body: { job_id: 1 },
71         }).as("get-job-response");
72         cy.get("#list_list tbody tr:first td a").contains("Import").click();
73         cy.get("main div[class='dialog message']").contains(
74             "Import in progress, see job #1"
75         );
76     });
77
78     it("List title", () => {
79         // GET title returns 500
80         cy.intercept("GET", "/api/v1/erm/eholdings/local/titles*", {
81             statusCode: 500,
82             error: "Something went wrong",
83         });
84         cy.visit("/cgi-bin/koha/erm/erm.pl");
85         cy.get("#navmenulist").contains("Titles").click();
86         cy.get("main div[class='dialog alert']").contains(
87             /Something went wrong/
88         );
89
90         // GET titles returns empty list
91         cy.intercept("GET", "/api/v1/erm/eholdings/local/titles*", {
92             statusCode: 200,
93             body: [],
94             headers: {
95                 "X-Base-Total-Count": "0",
96                 "X-Total-Count": "0",
97             },
98         });
99         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles");
100         cy.get("#titles_list").contains("There are no titles defined");
101
102         // GET titles returns something
103         let erm_title = cy.get_title();
104         let titles = [erm_title];
105
106         cy.intercept("GET", "/api/v1/erm/eholdings/local/titles*", {
107             statusCode: 200,
108             body: titles,
109             headers: {
110                 "X-Base-Total-Count": "1",
111                 "X-Total-Count": "1",
112             },
113         });
114         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles");
115         cy.get("#titles_list").contains("Showing 1 to 1 of 1 entries");
116     });
117
118     it("Add title", () => {
119         cy.intercept(
120             {
121                 method: "GET",
122                 url: "/api/v1/erm/eholdings/local/packages*",
123                 times: 1,
124             },
125             {
126                 body: [],
127             }
128         );
129
130         // Click the button in the toolbar
131         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles");
132         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
133         cy.contains("New title").click();
134         cy.get("#titles_add h2").contains("New title");
135
136         // Fill in the form for normal attributes
137         let erm_title = cy.get_title();
138
139         cy.get("#titles_add").contains("Submit").click();
140         cy.get("input:invalid,textarea:invalid,select:invalid").should(
141             "have.length",
142             1
143         );
144         cy.get("#title_publication_title").type(erm_title.publication_title);
145         cy.get("#title_print_identifier").type(erm_title.print_identifier);
146         cy.get("#title_online_identifier").type(erm_title.online_identifier);
147         cy.get("#title_date_first_issue_online").type(
148             erm_title.date_first_issue_online
149         );
150         cy.get("#title_num_first_vol_online").type(
151             erm_title.num_first_vol_online
152         );
153         cy.get("#title_num_first_issue_online").type(
154             erm_title.num_first_issue_online
155         );
156         cy.get("#title_date_last_issue_online").type(
157             erm_title.date_last_issue_online
158         );
159         cy.get("#title_num_last_vol_online").type(
160             erm_title.num_last_vol_online
161         );
162         cy.get("#title_num_last_issue_online").type(
163             erm_title.num_last_issue_online
164         );
165         cy.get("#title_title_url").type(erm_title.title_url);
166         cy.get("#title_first_author").type(erm_title.first_author);
167         cy.get("#title_embargo_info").type(erm_title.embargo_info);
168         cy.get("#title_coverage_depth").type(erm_title.coverage_depth);
169         cy.get("#title_notes").type(erm_title.notes);
170         cy.get("#title_publisher_name").type(erm_title.publisher_name);
171         cy.get("#title_publication_type .vs__search").type(
172             erm_title.publication_type + "{enter}",
173             { force: true }
174         );
175         cy.get("#title_date_monograph_published_print").type(
176             erm_title.date_monograph_published_print
177         );
178         cy.get("#title_date_monograph_published_online").type(
179             erm_title.date_monograph_published_online
180         );
181         cy.get("#title_monograph_volume").type(erm_title.monograph_volume);
182         cy.get("#title_monograph_edition").type(erm_title.monograph_edition);
183         cy.get("#title_first_editor").type(erm_title.first_editor);
184         cy.get("#title_parent_publication_title_id").type(
185             erm_title.parent_publication_title_id
186         );
187         cy.get("#title_preceeding_publication_title_id").type(
188             erm_title.preceeding_publication_title_id
189         );
190         cy.get("#title_access_type").type(erm_title.access_type);
191
192         cy.get("#resources").contains("There are no packages created yet");
193
194         // Submit the form, get 500
195         cy.intercept("POST", "/api/v1/erm/eholdings/local/titles", {
196             statusCode: 500,
197             error: "Something went wrong",
198         });
199         cy.get("#titles_add").contains("Submit").click();
200         cy.get("main div[class='dialog alert']").contains(
201             "Something went wrong: SyntaxError: Unexpected end of JSON input"
202         );
203
204         // Submit the form, success!
205         cy.intercept("POST", "/api/v1/erm/eholdings/local/titles", {
206             statusCode: 201,
207             body: erm_title,
208         });
209         cy.get("#titles_add").contains("Submit").click();
210         cy.get("main div[class='dialog message']").contains("Title created");
211
212         // Add new related package (resource)
213         let related_package = erm_title.resources[0];
214         cy.intercept("GET", "/api/v1/erm/eholdings/local/packages*", {
215             statusCode: 200,
216             body: get_packages_to_relate(),
217         }).as("get-related-packages");
218         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles/add");
219         cy.get("#resources").contains("Add to another package").click();
220         cy.get("#resources").contains("Package 1");
221         cy.get("#resources #resource_package_id_0 .vs__search").type(
222             related_package.package.name
223         );
224         cy.get("#resources #resource_package_id_0 .vs__dropdown-menu li")
225             .eq(0)
226             .click({ force: true }); //click first package suggestion
227     });
228
229     it("Edit title", () => {
230         let erm_title = cy.get_title();
231         let titles = [erm_title];
232         // Click the 'Edit' button from the list
233         cy.intercept("GET", "/api/v1/erm/eholdings/local/titles*", {
234             statusCode: 200,
235             body: titles,
236             headers: {
237                 "X-Base-Total-Count": "1",
238                 "X-Total-Count": "1",
239             },
240         });
241         cy.intercept(
242             "GET",
243             "/api/v1/erm/eholdings/local/titles/*",
244             erm_title
245         ).as("get-title");
246         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles");
247         // Intercept related packages request after entering title edit
248         cy.intercept("GET", "/api/v1/erm/eholdings/local/packages*", {
249             statusCode: 200,
250             body: get_packages_to_relate(),
251         }).as("get-related-packages");
252
253         cy.get("#titles_list table tbody tr:first").contains("Edit").click();
254         cy.wait("@get-title");
255         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
256         cy.get("#titles_add h2").contains("Edit title");
257
258         // Form has been correctly filled in
259         cy.get("#title_publication_title").should(
260             "have.value",
261             erm_title.publication_title
262         );
263         cy.get("#title_print_identifier").should(
264             "have.value",
265             erm_title.print_identifier
266         );
267         cy.get("#title_online_identifier").should(
268             "have.value",
269             erm_title.online_identifier
270         );
271         cy.get("#title_date_first_issue_online").should(
272             "have.value",
273             erm_title.date_first_issue_online
274         );
275         cy.get("#title_num_first_vol_online").should(
276             "have.value",
277             erm_title.num_first_vol_online
278         );
279         cy.get("#title_num_first_issue_online").should(
280             "have.value",
281             erm_title.num_first_issue_online
282         );
283         cy.get("#title_date_last_issue_online").should(
284             "have.value",
285             erm_title.date_last_issue_online
286         );
287         cy.get("#title_num_last_vol_online").should(
288             "have.value",
289             erm_title.num_last_vol_online
290         );
291         cy.get("#title_num_last_issue_online").should(
292             "have.value",
293             erm_title.num_last_issue_online
294         );
295         cy.get("#title_title_url").should("have.value", erm_title.title_url);
296         cy.get("#title_first_author").should(
297             "have.value",
298             erm_title.first_author
299         );
300         cy.get("#title_embargo_info").should(
301             "have.value",
302             erm_title.embargo_info
303         );
304         cy.get("#title_coverage_depth").should(
305             "have.value",
306             erm_title.coverage_depth
307         );
308         cy.get("#title_notes").should("have.value", erm_title.notes);
309         cy.get("#title_publisher_name").should(
310             "have.value",
311             erm_title.publisher_name
312         );
313         cy.get("#title_publication_type .vs__selected").contains("Journal");
314         cy.get("#title_date_monograph_published_print").should(
315             "have.value",
316             erm_title.date_monograph_published_print
317         );
318         cy.get("#title_date_monograph_published_online").should(
319             "have.value",
320             erm_title.date_monograph_published_online
321         );
322         cy.get("#title_monograph_volume").should(
323             "have.value",
324             erm_title.monograph_volume
325         );
326         cy.get("#title_monograph_edition").should(
327             "have.value",
328             erm_title.monograph_edition
329         );
330         cy.get("#title_first_editor").should(
331             "have.value",
332             erm_title.first_editor
333         );
334         cy.get("#title_parent_publication_title_id").should(
335             "have.value",
336             erm_title.parent_publication_title_id
337         );
338         cy.get("#title_preceeding_publication_title_id").should(
339             "have.value",
340             erm_title.preceeding_publication_title_id
341         );
342         cy.get("#title_access_type").should(
343             "have.value",
344             erm_title.access_type
345         );
346
347         //Test related content
348         cy.get("#resources #resource_package_id_0 .vs__selected").contains(
349             "package name"
350         );
351
352         // Submit the form, get 500
353         cy.intercept("PUT", "/api/v1/erm/eholdings/local/titles/*", {
354             statusCode: 500,
355             error: "Something went wrong",
356         });
357         cy.get("#titles_add").contains("Submit").click();
358         cy.get("main div[class='dialog alert']").contains(
359             "Something went wrong: SyntaxError: Unexpected end of JSON input"
360         );
361
362         // Submit the form, success!
363         cy.intercept("PUT", "/api/v1/erm/eholdings/local/titles/*", {
364             statusCode: 200,
365             body: erm_title,
366         });
367         cy.get("#titles_add").contains("Submit").click();
368         cy.get("main div[class='dialog message']").contains("Title updated");
369     });
370
371     it("Show title", () => {
372         let erm_title = cy.get_title();
373         let titles = [erm_title];
374         // Click the "name" link from the list
375         cy.intercept("GET", "/api/v1/erm/eholdings/local/titles*", {
376             statusCode: 200,
377             body: titles,
378             headers: {
379                 "X-Base-Total-Count": "1",
380                 "X-Total-Count": "1",
381             },
382         });
383         // Title with empty resources.
384         cy.intercept(
385             {
386                 method: "GET",
387                 url: "/api/v1/erm/eholdings/local/titles/*",
388                 times: 1,
389             },
390             {
391                 body: {
392                     publication_title: "publication title",
393                     resources: [],
394                     title_id: 1,
395                 },
396             }
397         ).as("get-title");
398         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles");
399         let title_link = cy.get("#titles_list table tbody tr:first td:first a");
400         title_link.should(
401             "have.text",
402             erm_title.publication_title + " (#" + erm_title.title_id + ")"
403         );
404         cy.get("#titles_list table tbody tr:first td:first a").click();
405         cy.wait("@get-title");
406         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
407         cy.get("#eholdings_title_show h2").contains(
408             "Title #" + erm_title.title_id
409         );
410         // There are no packages, the table should not be displayed
411         cy.contains("Packages (0)");
412         cy.get("#table#package_list").should("not.exist");
413
414         // Test now with all values
415         cy.intercept(
416             "GET",
417             "/api/v1/erm/eholdings/local/titles/*",
418             erm_title
419         ).as("get-title");
420
421         // List packages
422         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles/1");
423         cy.contains("Packages (1)");
424         cy.wait(500);
425
426         // Visit resource
427         let related_package = erm_title.resources[0];
428         // cy.get("#package_list tbody tr:first td a").contains("first package name").click();
429         cy.intercept(
430             "GET",
431             "/api/v1/erm/eholdings/local/resources/" +
432                 related_package.resource_id,
433             related_package
434         ).as("get-related-package");
435         cy.get("table#package_list").contains("first package name").click();
436         cy.contains("Resource #" + related_package.resource_id);
437         cy.contains(related_package.package.name);
438     });
439
440     it("Delete title", () => {
441         let erm_title = cy.get_title();
442         let titles = [erm_title];
443
444         // Click the 'Delete' button from the list
445         cy.intercept("GET", "/api/v1/erm/eholdings/local/titles*", {
446             statusCode: 200,
447             body: titles,
448             headers: {
449                 "X-Base-Total-Count": "1",
450                 "X-Total-Count": "1",
451             },
452         });
453         cy.intercept("GET", "/api/v1/erm/eholdings/local/titles/*", erm_title);
454         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles");
455
456         cy.get("#titles_list table tbody tr:first").contains("Delete").click();
457         cy.get(".dialog.alert.confirmation h1").contains("remove this title");
458         cy.contains(erm_title.publication_title);
459
460         // Accept the confirmation dialog, get 500
461         cy.intercept("DELETE", "/api/v1/erm/eholdings/local/titles/*", {
462             statusCode: 500,
463             error: "Something went wrong",
464         });
465         cy.contains("Yes, delete").click();
466         cy.get("main div[class='dialog alert']").contains(
467             "Something went wrong: SyntaxError: Unexpected end of JSON input"
468         );
469
470         // Accept the confirmation dialog, success!
471         cy.intercept("DELETE", "/api/v1/erm/eholdings/local/titles/*", {
472             statusCode: 204,
473             body: null,
474         });
475         cy.get("#titles_list table tbody tr:first").contains("Delete").click();
476         cy.get(".dialog.alert.confirmation h1").contains("remove this title");
477         cy.contains("Yes, delete").click();
478         cy.get("main div[class='dialog message']")
479             .contains("Local title")
480             .contains("deleted");
481
482         // Delete from show
483         // Click the "name" link from the list
484         cy.intercept("GET", "/api/v1/erm/eholdings/local/titles*", {
485             statusCode: 200,
486             body: titles,
487             headers: {
488                 "X-Base-Total-Count": "1",
489                 "X-Total-Count": "1",
490             },
491         });
492         // Title with empty resources.
493         cy.intercept(
494             {
495                 method: "GET",
496                 url: "/api/v1/erm/eholdings/local/titles/*",
497                 times: 1,
498             },
499             {
500                 body: {
501                     publication_title: "publication title",
502                     resources: [],
503                     title_id: 1,
504                 },
505             }
506         ).as("get-title");
507         cy.visit("/cgi-bin/koha/erm/eholdings/local/titles");
508         let title_link = cy.get("#titles_list table tbody tr:first td:first a");
509         title_link.should(
510             "have.text",
511             erm_title.publication_title + " (#" + erm_title.title_id + ")"
512         );
513         cy.get("#titles_list table tbody tr:first td:first a").click();
514         cy.wait("@get-title");
515         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
516         cy.get("#eholdings_title_show h2").contains(
517             "Title #" + erm_title.title_id
518         );
519
520         cy.get("#eholdings_title_show .action_links .fa-trash").click();
521         cy.get(".dialog.alert.confirmation h1").contains("remove this title");
522         cy.contains("Yes, delete").click();
523
524         //Make sure we return to list after deleting from show
525         cy.get("#titles_list table tbody tr:first");
526     });
527 });