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