View | Details | Raw Unified | Return to bug 35287
Collapse All | Expand All

(-)a/t/cypress/integration/AdditionalFields_spec.ts (+645 lines)
Line 0 Link Here
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_no_additional_fields_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
        user_roles: [],
21
        vendor_id: 1,
22
        vendor: [cy.get_vendors_to_relate()[0]],
23
        documents: [
24
            {
25
                license_id: 1,
26
                file_description: "file description",
27
                file_name: "file.json",
28
                notes: "file notes",
29
                physical_location: "file physical location",
30
                uri: "file uri",
31
                uploaded_on: "2022-10-27T11:57:02+00:00",
32
            },
33
        ],
34
        extended_attributes: [],
35
        _strings: {
36
            additional_field_values: [],
37
        },
38
    };
39
}
40
function get_license() {
41
    return {
42
        license_id: 1,
43
        name: "license 1",
44
        description: "my first license",
45
        type: "local",
46
        status: "active",
47
        started_on: dates["today_iso"],
48
        ended_on: dates["tomorrow_iso"],
49
        user_roles: [],
50
        vendor_id: 1,
51
        vendor: [cy.get_vendors_to_relate()[0]],
52
        documents: [
53
            {
54
                license_id: 1,
55
                file_description: "file description",
56
                file_name: "file.json",
57
                notes: "file notes",
58
                physical_location: "file physical location",
59
                uri: "file uri",
60
                uploaded_on: "2022-10-27T11:57:02+00:00",
61
            },
62
        ],
63
        extended_attributes: [
64
            {
65
                field_id: 1,
66
                id: "1",
67
                record_id: "1",
68
                value: "REF",
69
            },
70
            {
71
                field_id: 1,
72
                id: "2",
73
                record_id: "1",
74
                value: "NFIC",
75
            },
76
            {
77
                field_id: 2,
78
                id: "3",
79
                record_id: "1",
80
                value: "some text",
81
            },
82
            {
83
                field_id: 3,
84
                id: "4",
85
                record_id: "1",
86
                value: "some repeatable text",
87
            },
88
            {
89
                field_id: 4,
90
                id: "5",
91
                record_id: "1",
92
                value: "AF",
93
            },
94
        ],
95
        _strings: {
96
            additional_field_values: [
97
                {
98
                    field_id: 1,
99
                    field_label: "AV Repeatable",
100
                    type: "av",
101
                    value_str: "Reference, Non-fiction",
102
                },
103
                {
104
                    field_id: 2,
105
                    field_label: "Text non-repeatable",
106
                    type: "text",
107
                    value_str: "some text",
108
                },
109
                {
110
                    field_id: 3,
111
                    field_label: "Text repeatable",
112
                    type: "text",
113
                    value_str: "some repeatable text",
114
                },
115
                {
116
                    field_id: 4,
117
                    field_label: "AV Searchable",
118
                    type: "av",
119
                    value_str: "Afghanistan",
120
                },
121
            ],
122
        },
123
    };
124
}
125
126
function get_licenses_additional_fields() {
127
    return [
128
        {
129
            authorised_value_category: "CCODE",
130
            id: 1,
131
            marcfield: "",
132
            marcfield_mode: "get",
133
            name: "AV Repeatable",
134
            repeatable: true,
135
            searchable: true,
136
            tablename: "erm_licenses",
137
        },
138
        {
139
            authorised_value_category: null,
140
            id: 2,
141
            marcfield: "",
142
            marcfield_mode: "get",
143
            name: "Text non-repeatable",
144
            repeatable: false,
145
            searchable: false,
146
            tablename: "erm_licenses",
147
        },
148
        {
149
            authorised_value_category: null,
150
            id: 3,
151
            marcfield: "",
152
            marcfield_mode: "get",
153
            name: "Text repeatable",
154
            repeatable: true,
155
            searchable: false,
156
            tablename: "erm_licenses",
157
        },
158
        {
159
            authorised_value_category: "COUNTRY",
160
            id: 4,
161
            marcfield: "",
162
            marcfield_mode: "get",
163
            name: "AV Searchable",
164
            repeatable: false,
165
            searchable: true,
166
            tablename: "erm_licenses",
167
        },
168
    ];
169
}
170
171
function get_av_cats() {
172
    return [
173
        {
174
            authorised_values: [
175
                {
176
                    authorised_value_id: 1012,
177
                    category_name: "CCODE",
178
                    description: "Fiction",
179
                    image_url: null,
180
                    opac_description: null,
181
                    value: "FIC",
182
                },
183
                {
184
                    authorised_value_id: 1013,
185
                    category_name: "CCODE",
186
                    description: "Reference",
187
                    image_url: null,
188
                    opac_description: null,
189
                    value: "REF",
190
                },
191
                {
192
                    authorised_value_id: 1014,
193
                    category_name: "CCODE",
194
                    description: "Non-fiction",
195
                    image_url: null,
196
                    opac_description: null,
197
                    value: "NFIC",
198
                },
199
            ],
200
            category_name: "CCODE",
201
            is_system: true,
202
        },
203
        {
204
            authorised_values: [
205
                {
206
                    authorised_value_id: 111,
207
                    category_name: "COUNTRY",
208
                    description: "Andorra",
209
                    image_url: null,
210
                    opac_description: "Andorra",
211
                    value: "AD",
212
                },
213
                {
214
                    authorised_value_id: 112,
215
                    category_name: "COUNTRY",
216
                    description: "United Arab Emirates",
217
                    image_url: null,
218
                    opac_description: "United Arab Emirates",
219
                    value: "AE",
220
                },
221
                {
222
                    authorised_value_id: 113,
223
                    category_name: "COUNTRY",
224
                    description: "Afghanistan",
225
                    image_url: null,
226
                    opac_description: "Afghanistan",
227
                    value: "AF",
228
                },
229
            ],
230
            category_name: "COUNTRY",
231
            is_system: false,
232
        },
233
    ];
234
}
235
236
function get_description_from_av_value(av_cats, av_value): string {
237
    return av_cats
238
        .find(av_cat =>
239
            av_cat.authorised_values.find(av => av.value == av_value.value)
240
        )
241
        ?.authorised_values.find(av => av.value == av_value.value)?.description;
242
}
243
244
describe("Additional Fields operations", () => {
245
    beforeEach(() => {
246
        cy.login();
247
        cy.title().should("eq", "Koha staff interface");
248
        cy.intercept(
249
            "GET",
250
            "/api/v1/erm/config",
251
            '{"settings":{"ERMModule":"1","ERMProviders":["local"]}}'
252
        );
253
    });
254
255
    it("Additional Fields display - Table (licenses)", () => {
256
        let license = get_license();
257
        let licenses = [license];
258
        let license_additional_fields = get_licenses_additional_fields();
259
        let av_cats = get_av_cats();
260
261
        cy.intercept("GET", "/api/v1/additional_fields*", {
262
            body: license_additional_fields,
263
            statusCode: 200,
264
        });
265
266
        cy.intercept("GET", "/api/v1/erm/licenses*", {
267
            statusCode: 200,
268
            body: licenses,
269
            headers: {
270
                "X-Base-Total-Count": "1",
271
                "X-Total-Count": "1",
272
            },
273
        });
274
        cy.intercept("GET", "/api/v1/erm/licenses/*", license);
275
        cy.visit("/cgi-bin/koha/erm/licenses");
276
        cy.get("#licenses_list").contains("Showing 1 to 1 of 1 entries");
277
278
        cy.get("#licenses_list table tbody tr:first").contains(
279
            get_description_from_av_value(
280
                av_cats,
281
                license.extended_attributes[4]
282
            )
283
        );
284
        cy.get("#licenses_list table tbody tr:first").contains(
285
            get_description_from_av_value(
286
                av_cats,
287
                license.extended_attributes[0]
288
            ) +
289
                ", " +
290
                get_description_from_av_value(
291
                    av_cats,
292
                    license.extended_attributes[1]
293
                )
294
        );
295
    });
296
297
    it("Additional Fields display - Show (licenses)", () => {
298
        let empty_license = get_no_additional_fields_license();
299
        let license = get_license();
300
        let licenses = [license];
301
        let vendors = cy.get_vendors_to_relate();
302
        let license_additional_fields = get_licenses_additional_fields();
303
        let av_cats = get_av_cats();
304
305
        // Click the 'Edit' button from the list
306
        cy.intercept("GET", "/api/v1/erm/licenses*", {
307
            statusCode: 200,
308
            body: licenses,
309
            headers: {
310
                "X-Base-Total-Count": "1",
311
                "X-Total-Count": "1",
312
            },
313
        });
314
        cy.intercept("GET", "/api/v1/erm/licenses/*", empty_license).as(
315
            "get-empty-license"
316
        );
317
318
        //Intercept vendors request
319
        cy.intercept("GET", "/api/v1/acquisitions/vendors*", {
320
            statusCode: 200,
321
            body: vendors,
322
        });
323
        cy.intercept("GET", "/api/v1/additional_fields*", {
324
            body: [],
325
            statusCode: 200,
326
        }).as("empty-additional-fields");
327
328
        //Empty additional fields, should not display
329
        cy.visit("/cgi-bin/koha/erm/licenses");
330
        cy.get("#licenses_list table tbody tr:first td:first a").click();
331
        cy.wait("@get-empty-license");
332
        cy.get("#licenses_show #additional_fields").should("not.exist");
333
334
        cy.intercept("GET", "/api/v1/additional_fields*", {
335
            body: license_additional_fields,
336
            statusCode: 200,
337
        }).as("existing-additional-fields");
338
339
        cy.intercept(
340
            {
341
                pathname: "/api/v1/authorised_value_categories",
342
                query: {
343
                    q: '{"me.category_name":["CCODE", "COUNTRY"]}',
344
                },
345
            },
346
            {
347
                body: av_cats,
348
                statusCode: 200,
349
            }
350
        ).as("avcategories");
351
        // cy.wait("@avcategories");
352
353
        cy.intercept("GET", "/api/v1/erm/licenses/*", license).as(
354
            "get-license"
355
        );
356
357
        // There are additional fields, fieldset should exist
358
        cy.visit("/cgi-bin/koha/erm/licenses");
359
        cy.get("#licenses_list table tbody tr:first td:first a").click();
360
        cy.wait("@get-license");
361
        cy.get("#licenses_show #additional_fields").should("exist");
362
363
        // All fields are presented correctly
364
        cy.get(
365
            "#licenses_show #additional_fields label[for='additional_field_1']"
366
        ).contains(license_additional_fields[0].name);
367
        cy.get(
368
            "#licenses_show #additional_fields label[for='additional_field_1']"
369
        )
370
            .parent()
371
            .children("span")
372
            .contains(
373
                get_description_from_av_value(
374
                    av_cats,
375
                    license.extended_attributes[0]
376
                ) +
377
                    ", " +
378
                    get_description_from_av_value(
379
                        av_cats,
380
                        license.extended_attributes[1]
381
                    )
382
            );
383
384
        cy.get(
385
            "#licenses_show #additional_fields label[for='additional_field_2']"
386
        ).contains(license_additional_fields[1].name);
387
        cy.get(
388
            "#licenses_show #additional_fields label[for='additional_field_2']"
389
        )
390
            .parent()
391
            .children("span")
392
            .contains(license.extended_attributes[2].value);
393
394
        cy.get(
395
            "#licenses_show #additional_fields label[for='additional_field_3']"
396
        ).contains(license_additional_fields[2].name);
397
        cy.get(
398
            "#licenses_show #additional_fields label[for='additional_field_3']"
399
        )
400
            .parent()
401
            .children("span")
402
            .contains(license.extended_attributes[3].value);
403
404
        cy.get(
405
            "#licenses_show #additional_fields label[for='additional_field_4']"
406
        ).contains(license_additional_fields[3].name);
407
        cy.get(
408
            "#licenses_show #additional_fields label[for='additional_field_4']"
409
        )
410
            .parent()
411
            .children("span")
412
            .contains(
413
                get_description_from_av_value(
414
                    av_cats,
415
                    license.extended_attributes[4]
416
                )
417
            );
418
    });
419
420
    it("Additional Fields entry - Add (licenses)", () => {
421
        let vendors = cy.get_vendors_to_relate();
422
        let license_additional_fields = get_licenses_additional_fields();
423
        let av_cats = get_av_cats();
424
425
        //Intercept vendors request
426
        cy.intercept("GET", "/api/v1/acquisitions/vendors*", {
427
            statusCode: 200,
428
            body: vendors,
429
        });
430
        cy.intercept("GET", "/api/v1/additional_fields*", {
431
            body: [],
432
            statusCode: 200,
433
        }).as("empty-additional-fields");
434
435
        // No additional fields, fieldset should not exist
436
        cy.visit("/cgi-bin/koha/erm/licenses/add");
437
        cy.get("#licenses_add form #additional_fields").should("not.exist");
438
439
        cy.intercept("GET", "/api/v1/additional_fields*", {
440
            body: license_additional_fields,
441
            statusCode: 200,
442
        }).as("existing-additional-fields");
443
444
        cy.intercept(
445
            {
446
                pathname: "/api/v1/authorised_value_categories",
447
                query: {
448
                    q: '{"me.category_name":["CCODE", "COUNTRY"]}',
449
                },
450
            },
451
            {
452
                body: av_cats,
453
                statusCode: 200,
454
            }
455
        ).as("avcategories");
456
        cy.wait("@avcategories");
457
        // There are additional fields, fieldset should exist
458
        cy.visit("/cgi-bin/koha/erm/licenses/add");
459
        cy.get("#licenses_add form #additional_fields").should("exist");
460
461
        // All additional fields should be listed
462
        cy.get(
463
            "#licenses_add form #additional_fields label[for='additional_field_1']"
464
        ).contains(license_additional_fields[0].name);
465
        cy.get(
466
            "#licenses_add form #additional_fields label[for='additional_field_2']"
467
        ).contains(license_additional_fields[1].name);
468
        cy.get(
469
            "#licenses_add form #additional_fields label[for='additional_field_3']"
470
        ).contains(license_additional_fields[2].name);
471
        cy.get(
472
            "#licenses_add form #additional_fields label[for='additional_field_4']"
473
        ).contains(license_additional_fields[3].name);
474
475
        cy.get("#additional_fields #additional_field_1 .vs__selected").should(
476
            "not.exist"
477
        ); //new license, no pre-selected value
478
479
        // Pick one value
480
        cy.get("#additional_fields #additional_field_1 .vs__search").click();
481
        cy.get(
482
            "#additional_fields #additional_field_1 #vs4__option-0"
483
        ).contains(av_cats[0].authorised_values[0].description);
484
        cy.get("#additional_fields #additional_field_1 #vs4__option-0").click();
485
        cy.get("#additional_fields #additional_field_1 .vs__selected").contains(
486
            av_cats[0].authorised_values[0].description
487
        );
488
        cy.get("#additional_fields #additional_field_1 .vs__selected").should(
489
            "have.length",
490
            1
491
        );
492
493
        // Pick a second value for the same repeatable AV field
494
        cy.get("#additional_fields #additional_field_1 .vs__search").click();
495
        cy.get(
496
            "#additional_fields #additional_field_1 #vs4__option-1"
497
        ).contains(av_cats[0].authorised_values[1].description);
498
        cy.get("#additional_fields #additional_field_1 #vs4__option-1").click();
499
        cy.get("#additional_fields #additional_field_1 .vs__selected").contains(
500
            av_cats[0].authorised_values[1].description
501
        );
502
        cy.get("#additional_fields #additional_field_1 .vs__selected").should(
503
            "have.length",
504
            2
505
        );
506
507
        // Attempt to pick the same value again - should not be possible
508
        cy.get("#additional_fields #additional_field_1 .vs__search").click();
509
        cy.get(
510
            "#additional_fields #additional_field_1 #vs4__option-1"
511
        ).contains(av_cats[0].authorised_values[1].description);
512
        cy.get("#additional_fields #additional_field_1 #vs4__option-1").click();
513
        cy.get("#additional_fields #additional_field_1 .vs__selected").should(
514
            "have.length",
515
            2
516
        );
517
518
        // Remove the second selected value
519
        cy.get(
520
            "#additional_fields #additional_field_1 .vs__selected button[title='Deselect " +
521
                av_cats[0].authorised_values[1].description +
522
                "'"
523
        ).click();
524
        cy.get("#additional_fields #additional_field_1 .vs__selected").should(
525
            "have.length",
526
            1
527
        );
528
        cy.get("#additional_fields #additional_field_1 .vs__selected").contains(
529
            av_cats[0].authorised_values[0].description
530
        );
531
    });
532
533
    it("Additional Fields entry - Edit (licenses)", () => {
534
        let license = get_license();
535
        let licenses = [license];
536
        let vendors = cy.get_vendors_to_relate();
537
        let license_additional_fields = get_licenses_additional_fields();
538
        let av_cats = get_av_cats();
539
540
        // Click the 'Edit' button from the list
541
        cy.intercept("GET", "/api/v1/erm/licenses*", {
542
            statusCode: 200,
543
            body: licenses,
544
            headers: {
545
                "X-Base-Total-Count": "1",
546
                "X-Total-Count": "1",
547
            },
548
        });
549
        cy.intercept("GET", "/api/v1/erm/licenses/*", license).as(
550
            "get-license"
551
        );
552
553
        //Intercept vendors request
554
        cy.intercept("GET", "/api/v1/acquisitions/vendors*", {
555
            statusCode: 200,
556
            body: vendors,
557
        });
558
559
        cy.intercept("GET", "/api/v1/additional_fields*", {
560
            body: license_additional_fields,
561
            statusCode: 200,
562
        }).as("existing-additional-fields");
563
564
        cy.intercept(
565
            {
566
                pathname: "/api/v1/authorised_value_categories",
567
                query: {
568
                    q: '{"me.category_name":["CCODE", "COUNTRY"]}',
569
                },
570
            },
571
            {
572
                body: av_cats,
573
                statusCode: 200,
574
            }
575
        ).as("avcategories");
576
577
        cy.visit("/cgi-bin/koha/erm/licenses");
578
        cy.get("#licenses_list table tbody tr:first").contains("Edit").click();
579
        cy.wait("@get-license");
580
        cy.wait(500); // Cypress is too fast! Vue hasn't populated the form yet!
581
582
        // All additional fields should be pre-populated
583
        cy.get("#additional_fields #additional_field_1 .vs__selected").contains(
584
            get_description_from_av_value(
585
                av_cats,
586
                license.extended_attributes[0]
587
            )
588
        );
589
        cy.get("#additional_fields #additional_field_1 .vs__selected").contains(
590
            get_description_from_av_value(
591
                av_cats,
592
                license.extended_attributes[1]
593
            )
594
        );
595
596
        cy.get(
597
            "#licenses_add form #additional_fields label[for='additional_field_2']"
598
        )
599
            .parent()
600
            .children("input")
601
            .should("have.value", license.extended_attributes[2].value);
602
603
        cy.get(
604
            "#licenses_add form #additional_fields label[for='additional_field_3']"
605
        )
606
            .parent()
607
            .children("input")
608
            .should("have.value", license.extended_attributes[3].value);
609
610
        cy.get("#additional_fields #additional_field_4 .vs__selected").contains(
611
            get_description_from_av_value(
612
                av_cats,
613
                license.extended_attributes[4]
614
            )
615
        );
616
617
        // Clear text field works
618
        cy.get(
619
            "#licenses_add form #additional_fields label[for='additional_field_2']"
620
        )
621
            .parent()
622
            .children(".clear_attribute")
623
            .click();
624
        cy.get(
625
            "#licenses_add form #additional_fields label[for='additional_field_2']"
626
        )
627
            .parent()
628
            .children("input")
629
            .should("have.value", "");
630
631
        // "+New" text field works
632
        cy.get(
633
            "#licenses_add form #additional_fields label[for='additional_field_3']"
634
        ).should("have.length", 1);
635
        cy.get(
636
            "#licenses_add form #additional_fields label[for='additional_field_3']"
637
        )
638
            .parent()
639
            .children(".clone_attribute")
640
            .click();
641
        cy.get(
642
            "#licenses_add form #additional_fields label[for='additional_field_3']"
643
        ).should("have.length", 2);
644
    });
645
});

Return to bug 35287