Bug 30708: Add tests
[koha.git] / t / cypress / integration / Preservation / Settings.ts
1 import { mount } from "@cypress/vue";
2
3 function get_attributes() {
4     return [
5         {
6             processing_attribute_id: 1,
7             processing_id: 1,
8             name: "Country",
9             type: "authorised_value",
10             option_source: "COUNTRY",
11         },
12         {
13             processing_attribute_id: 1,
14             processing_id: 1,
15             name: "DB",
16             type: "db_column",
17             option_source: "biblio.title",
18         },
19         {
20             processing_attribute_id: 1,
21             processing_id: 1,
22             name: "Height",
23             type: "free_text",
24             option_source: null,
25         },
26     ];
27 }
28 function get_processing() {
29     return {
30         name: "test processing",
31         processing_id: 1,
32         attributes: get_attributes(),
33     };
34 }
35 describe("Processings", () => {
36     beforeEach(() => {
37         cy.login();
38         cy.title().should("eq", "Koha staff interface");
39         cy.intercept(
40             "GET",
41             "/cgi-bin/koha/svc/config/systempreferences/?pref=PreservationModule",
42             '{"value":"1"}'
43         );
44         cy.intercept(
45             "GET",
46             "/cgi-bin/koha/svc/config/systempreferences/?pref=PreservationNotForLoanWaitingListIn",
47             '{"value":"24"}'
48         );
49         cy.intercept(
50             "GET",
51             "/cgi-bin/koha/svc/config/systempreferences/?pref=PreservationNotForLoanDefaultTrainIn",
52             '{"value":"42"}'
53         );
54         cy.intercept(
55             "GET",
56             "/api/v1/authorised_value_categories/NOT_LOAN/authorised_values",
57             [
58                 {
59                     category_name: "NOT_LOAN",
60                     description: "Ordered",
61                     value: "-1",
62                 },
63                 {
64                     category_name: "NOT_LOAN",
65                     description: "Not for loan",
66                     value: "1",
67                 },
68                 {
69                     category_name: "NOT_LOAN",
70                     description: "Staff collection",
71                     value: "2",
72                 },
73                 {
74                     category_name: "NOT_LOAN",
75                     description: "Added to bundle",
76                     value: "3",
77                 },
78                 {
79                     category_name: "NOT_LOAN",
80                     description: "In preservation",
81                     value: "24",
82                 },
83                 {
84                     category_name: "NOT_LOAN",
85                     description: "In preservation external",
86                     value: "42",
87                 },
88             ]
89         );
90     });
91
92     it("Settings", () => {
93         cy.visit("/cgi-bin/koha/preservation/home.pl");
94         cy.get("#navmenulist").contains("Settings").click();
95         cy.get("#not_for_loan_waiting_list_in .vs__selected").contains(
96             "In preservation"
97         );
98         cy.get("#not_for_loan_default_train_in .vs__selected").contains(
99             "In preservation external"
100         );
101     });
102     it("List processing", () => {
103         cy.intercept("GET", "/api/v1/preservation/processings*", []);
104         cy.visit("/cgi-bin/koha/preservation/settings");
105         cy.get("#processing_0").should("not.exist");
106         cy.intercept("GET", "/api/v1/preservation/processings*", [
107             get_processing(),
108         ]);
109         cy.visit("/cgi-bin/koha/preservation/settings");
110         cy.get("#processing_0").should("exist");
111     });
112
113     it("Add processing", () => {
114         cy.intercept("GET", "/api/v1/preservation/processings*", []);
115         cy.visit("/cgi-bin/koha/preservation/settings");
116         let processing = get_processing();
117         cy.contains("Add new processing").click();
118         cy.get("#processing_name").type(processing.name);
119         cy.contains("Add new attribute").click();
120         let attribute = processing.attributes[0];
121         cy.get("#attribute_name_0").type(attribute.name);
122         cy.get("#attribute_type_0 .vs__search").type("Authorized{enter}", {
123             force: true,
124         });
125         cy.get("#attribute_option_0 .vs__search").type(
126             attribute.option_source + "{enter}",
127             { force: true }
128         );
129         cy.contains("Add new attribute").click();
130         attribute = processing.attributes[1];
131         cy.get("#attribute_name_1").type(attribute.name);
132         cy.get("#attribute_type_1 .vs__search").type("Database{enter}", {
133             force: true,
134         });
135         cy.get("#attribute_option_1 .vs__search").type(
136             attribute.option_source + "{enter}",
137             { force: true }
138         );
139         cy.contains("Add new attribute").click();
140         attribute = processing.attributes[2];
141         cy.get("#attribute_name_2").type(attribute.name);
142         cy.get("#attribute_type_2 .vs__search").type("Free{enter}", {
143             force: true,
144         });
145
146         // Submit the form, get 500
147         cy.intercept("POST", "/api/v1/preservation/processings", {
148             statusCode: 500,
149             error: "Something went wrong",
150         });
151         cy.get("#processings_add").contains("Submit").click();
152         cy.get("main div[class='dialog alert']").contains(
153             "Something went wrong: Error: Internal Server Error"
154         );
155
156         // Submit the form, success!
157         cy.intercept("POST", "/api/v1/preservation/processings", {
158             statusCode: 201,
159             body: processing,
160         });
161         cy.intercept("GET", "/api/v1/preservation/processings*", {
162             statusCode: 200,
163             body: [processing],
164         });
165         cy.get("#processings_add").contains("Submit").click();
166         cy.get("main div[class='dialog message']").contains(
167             "Processing created"
168         );
169         cy.get("#processing_0").contains(processing.name);
170     });
171
172     it("Edit processing", () => {
173         let processing = get_processing();
174         cy.intercept("GET", "/api/v1/preservation/processings/*", {
175             statusCode: 200,
176             body: processing,
177         });
178         cy.intercept("GET", "/api/v1/preservation/processings*", {
179             statusCode: 200,
180             body: [processing],
181         });
182         cy.visit("/cgi-bin/koha/preservation/settings");
183         cy.get("#processing_0").contains(processing.name);
184         cy.get("#processing_0").contains("Edit this processing").click();
185         cy.get("#processing_name").should("have.value", processing.name);
186         let attribute = processing.attributes[0];
187         cy.get("#attribute_name_0").should("have.value", attribute.name);
188         cy.get("#attribute_type_0 .vs__selected").contains("Authorized value");
189         cy.get("#attribute_option_0 .vs__selected").contains(
190             attribute.option_source
191         );
192         attribute = processing.attributes[1];
193         cy.get("#attribute_name_1").should("have.value", attribute.name);
194         cy.get("#attribute_type_1 .vs__selected").contains("Database column");
195         cy.get("#attribute_option_1 .vs__selected").contains(
196             attribute.option_source
197         );
198         attribute = processing.attributes[2];
199         cy.get("#attribute_name_2").should("have.value", attribute.name);
200         cy.get("#attribute_type_2 .vs__selected").contains("Free text");
201         cy.get("#attribute_option_2").should("not.exist");
202
203         // Submit the form, get 500
204         cy.intercept("PUT", "/api/v1/preservation/processings/*", {
205             statusCode: 500,
206             error: "Something went wrong",
207         });
208         cy.get("#processings_add").contains("Submit").click();
209         cy.get("main div[class='dialog alert']").contains(
210             "Something went wrong: Error: Internal Server Error"
211         );
212
213         // Submit the form, success!
214         cy.intercept("PUT", "/api/v1/preservation/processings/*", {
215             statusCode: 200,
216             body: processing,
217         });
218         cy.get("#processings_add").contains("Submit").click();
219         cy.get("main div[class='dialog message']").contains(
220             "Processing updated"
221         );
222     });
223
224     it("Delete processing", () => {
225         let processing = get_processing();
226         cy.intercept("GET", "/api/v1/preservation/processings*", {
227             statusCode: 200,
228             body: [processing],
229         });
230
231         // Submit the form, get 500
232         cy.intercept("DELETE", "/api/v1/preservation/processings/*", {
233             statusCode: 500,
234             error: "Something went wrong",
235         });
236         cy.visit("/cgi-bin/koha/preservation/settings");
237         cy.get("#processing_0").contains("Remove this processing").click();
238         cy.contains("Yes, delete").click();
239         cy.get("main div[class='dialog alert']").contains(
240             "Something went wrong: Error: Internal Server Error"
241         );
242
243         // Submit the form, success!
244         cy.intercept("DELETE", "/api/v1/preservation/processings/*", {
245             statusCode: 204,
246             body: null,
247         });
248         cy.get("#processing_0").contains("Remove this processing").click();
249         cy.contains("Yes, delete").click();
250         cy.get("main div[class='dialog message']").contains(
251             `Processing ${processing.name} deleted`
252         );
253     });
254 });