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