Bug 32030: Fix Cypress tests - flatpickr date format
[koha.git] / cypress / integration / Licenses_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 const dates = {
6     today_iso: dayjs().format("YYYY-MM-DD"),
7     today_us: dayjs().format("MM/DD/YYYY"),
8     tomorrow_iso: dayjs().add(1, "day").format("YYYY-MM-DD"),
9     tomorrow_us: dayjs().add(1, "day").format("MM/DD/YYYY"),
10 };
11 function get_license() {
12     return {
13         license_id: 1,
14         name: "license 1",
15         description: "my first license",
16         type: "local",
17         status: "active",
18         started_on: dates["today_iso"],
19         ended_on: dates["tomorrow_iso"],
20     };
21 }
22
23 describe("License CRUD operations", () => {
24     beforeEach(() => {
25         cy.login("koha", "koha");
26         cy.title().should("eq", "Koha staff interface");
27     });
28
29     it("List license", () => {
30         // GET license returns 500
31         cy.intercept("GET", "/api/v1/erm/licenses", {
32             statusCode: 500,
33             error: "Something went wrong",
34         });
35         cy.visit("/cgi-bin/koha/erm/erm.pl");
36         cy.get("#navmenulist").contains("Licenses").click();
37         cy.get("main div[class='dialog alert']").contains(
38             /Something went wrong/
39         );
40
41         // GET licenses returns empty list
42         cy.intercept("GET", "/api/v1/erm/licenses*", []);
43         cy.visit("/cgi-bin/koha/erm/licenses");
44         cy.get("#licenses_list").contains("There are no licenses defined.");
45
46         // GET licenses returns something
47         let license = get_license();
48         let licenses = [license];
49
50         cy.intercept("GET", "/api/v1/erm/licenses*", {
51             statusCode: 200,
52             body: licenses,
53             headers: {
54                 "X-Base-Total-Count": "1",
55                 "X-Total-Count": "1",
56             },
57         });
58         cy.intercept("GET", "/api/v1/erm/licenses/*", license);
59         cy.visit("/cgi-bin/koha/erm/licenses");
60         cy.get("#licenses_list").contains("Showing 1 to 1 of 1 entries");
61     });
62
63     it("Add license", () => {
64         // Click the button in the toolbar
65         cy.visit("/cgi-bin/koha/erm/licenses");
66         cy.contains("New license").click();
67         cy.get("#licenses_add h2").contains("New license");
68
69         // Fill in the form for normal attributes
70         let license = get_license();
71
72         cy.get("#licenses_add").contains("Submit").click();
73         cy.get("input:invalid,textarea:invalid,select:invalid").should(
74             "have.length",
75             4
76         );
77         cy.get("#license_name").type(license.name);
78         cy.get("#license_description").type(license.description);
79         cy.get("#licenses_add").contains("Submit").click();
80         cy.get("#license_type .vs__search").type(license.type + '{enter}',{force:true});
81         cy.get("#license_status .vs__search").type(license.status + '{enter}',{force:true});
82
83         cy.get("#started_on+input").click();
84         cy.get(".flatpickr-calendar")
85             .eq(0)
86             .find("span.today")
87             .click({ force: true });
88
89         cy.get("#ended_on+input").click();
90         cy.get(".flatpickr-calendar")
91             .eq(1)
92             .find("span.today")
93             .next("span")
94             .click();
95
96         // Submit the form, get 500
97         cy.intercept("POST", "/api/v1/erm/licenses", {
98             statusCode: 500,
99             error: "Something went wrong",
100         });
101         cy.get("#licenses_add").contains("Submit").click();
102         cy.get("main div[class='dialog alert']").contains(
103             "Something went wrong: Internal Server Error"
104         );
105
106         // Submit the form, success!
107         cy.intercept("POST", "/api/v1/erm/licenses", {
108             statusCode: 201,
109             body: license,
110         });
111         cy.get("#licenses_add").contains("Submit").click();
112         cy.get("main div[class='dialog message']").contains(
113             "License created"
114         );
115     });
116
117     it("Edit license", () => {
118         let license = get_license();
119         let licenses = [license];
120         // Click the 'Edit' button from the list
121         cy.intercept("GET", "/api/v1/erm/licenses*", {
122             statusCode: 200,
123             body: licenses,
124             headers: {
125                 "X-Base-Total-Count": "1",
126                 "X-Total-Count": "1",
127             },
128         });
129         cy.intercept("GET", "/api/v1/erm/licenses/*", license).as(
130             "get-license"
131         );
132         cy.visit("/cgi-bin/koha/erm/licenses");
133         cy.get("#licenses_list table tbody tr:first")
134             .contains("Edit")
135             .click();
136         cy.wait("@get-license");
137         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
138         cy.get("#licenses_add h2").contains("Edit license");
139
140         // Form has been correctly filled in
141         cy.get("#license_name").should("have.value", license.name);
142         cy.get("#license_description").should(
143             "have.value",
144             license.description
145         );
146         cy.get("#license_type .vs__selected").contains("Local");
147         cy.get("#license_status .vs__selected").contains("Active");
148         cy.get("#started_on").invoke("val").should("eq", dates["today_iso"]);
149         cy.get("#ended_on").invoke("val").should("eq", dates["tomorrow_iso"]);
150
151         // Submit the form, get 500
152         cy.intercept("PUT", "/api/v1/erm/licenses/*", {
153             statusCode: 500,
154             error: "Something went wrong",
155         });
156         cy.get("#licenses_add").contains("Submit").click();
157         cy.get("main div[class='dialog alert']").contains(
158             "Something went wrong: Internal Server Error"
159         );
160
161         // Submit the form, success!
162         cy.intercept("PUT", "/api/v1/erm/licenses/*", {
163             statusCode: 200,
164             body: license,
165         });
166         cy.get("#licenses_add").contains("Submit").click();
167         cy.get("main div[class='dialog message']").contains(
168             "License updated"
169         );
170     });
171
172     it("Show license", () => {
173         let license = get_license();
174         let licenses = [license];
175         // Click the "name" link from the list
176         cy.intercept("GET", "/api/v1/erm/licenses*", {
177             statusCode: 200,
178             body: licenses,
179             headers: {
180                 "X-Base-Total-Count": "1",
181                 "X-Total-Count": "1",
182             },
183         });
184         cy.intercept("GET", "/api/v1/erm/licenses/*", license).as(
185             "get-license"
186         );
187         cy.visit("/cgi-bin/koha/erm/licenses");
188         let name_link = cy.get(
189             "#licenses_list table tbody tr:first td:first a"
190         );
191         name_link.should(
192             "have.text",
193             license.name + " (#" + license.license_id + ")"
194         );
195         name_link.click();
196         cy.wait("@get-license");
197         cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
198         cy.get("#licenses_show h2").contains(
199             "License #" + license.license_id
200         );
201     });
202
203     it("Delete license", () => {
204         let license = get_license();
205         let licenses = [license];
206
207         // Click the 'Delete' button from the list
208         cy.intercept("GET", "/api/v1/erm/licenses*", {
209             statusCode: 200,
210             body: licenses,
211             headers: {
212                 "X-Base-Total-Count": "1",
213                 "X-Total-Count": "1",
214             },
215         });
216         cy.intercept("GET", "/api/v1/erm/licenses/*", license);
217         cy.visit("/cgi-bin/koha/erm/licenses");
218
219         cy.get("#licenses_list table tbody tr:first")
220             .contains("Delete")
221             .click();
222         cy.get("#licenses_confirm_delete h2").contains("Delete license");
223         cy.contains("License name: " + license.name);
224
225         // Submit the form, get 500
226         cy.intercept("DELETE", "/api/v1/erm/licenses/*", {
227             statusCode: 500,
228             error: "Something went wrong",
229         });
230         cy.contains("Yes, delete").click();
231         cy.get("main div[class='dialog alert']").contains(
232             "Something went wrong: Internal Server Error"
233         );
234
235         // Submit the form, success!
236         cy.intercept("DELETE", "/api/v1/erm/licenses/*", {
237             statusCode: 204,
238             body: null,
239         });
240         cy.contains("Yes, delete").click();
241         cy.get("main div[class='dialog message']").contains(
242             "License deleted"
243         );
244     });
245 });