]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-associate-material.component.spec.ts
LP 2061136 follow-up: ng lint --fix
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-associate-material.component.spec.ts
1 import { PermService } from '@eg/core/perm.service';
2 import { waitForAsync } from '@angular/core/testing';
3 import { ToastService } from '@eg/share/toast/toast.service';
4 import { CourseService } from '@eg/staff/share/course.service';
5 import { AuthService } from '@eg/core/auth.service';
6 import { NetService } from '@eg/core/net.service';
7 import { PcrudService } from '@eg/core/pcrud.service';
8 import { CourseAssociateMaterialComponent } from './course-associate-material.component';
9 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
10 import { of } from 'rxjs';
11 import { DialogComponent } from '@eg/share/dialog/dialog.component';
12
13
14 describe('CourseAssociateMaterialComponent', () => {
15     let component: CourseAssociateMaterialComponent;
16
17     const mockLibrary = {
18         id: () => 5,
19         shortname: () => 'greatLibrary'
20     };
21
22     const mockLibrary2 = {
23         id: () => 22
24     };
25
26     const mockItem = {
27         a: [],
28         classname: 'acp',
29         _isfieldmapper: true,
30         id: () => {},
31         circ_lib: () => mockLibrary
32     };
33
34     const mockCourse = {
35         a: [],
36         classname: 'acmc',
37         _isfieldmapper: true,
38         owning_lib: () => mockLibrary2
39     };
40
41     const authServiceSpy = jasmine.createSpyObj<AuthService>(['token']);
42     const courseServiceSpy = jasmine.createSpyObj<CourseService>(['associateMaterials']);
43     courseServiceSpy.associateMaterials.and.returnValue({item: mockItem, material: new Promise(() => {})});
44     const netServiceSpy = jasmine.createSpyObj<NetService>(['request']);
45     const pcrudServiceSpy = jasmine.createSpyObj<PcrudService>(['retrieveAll', 'search', 'update']);
46     pcrudServiceSpy.search.and.returnValue(of(mockItem));
47     const toastServiceSpy = jasmine.createSpyObj<ToastService>(['success']);
48     const permServiceSpy = jasmine.createSpyObj<PermService>(['hasWorkPermAt']);
49     permServiceSpy.hasWorkPermAt.and.returnValue(new Promise((resolve) => resolve({UPDATE_COPY: [5, 22]})));
50     const modalSpy = jasmine.createSpyObj<NgbModal>(['open']);
51     const dialogComponentSpy = jasmine.createSpyObj<DialogComponent>(['open']);
52     dialogComponentSpy.open.and.returnValue(of(true));
53     const rejectedDialogComponentSpy = jasmine.createSpyObj<DialogComponent>(['open']);
54     rejectedDialogComponentSpy.open.and.returnValue(of(false));
55
56     beforeEach(() => {
57         component = new CourseAssociateMaterialComponent(authServiceSpy, courseServiceSpy,
58             netServiceSpy, pcrudServiceSpy,
59             toastServiceSpy, permServiceSpy, modalSpy);
60         component.confirmOtherLibraryDialog = dialogComponentSpy;
61         component.currentCourse = mockCourse;
62     });
63
64     describe('#associateItem method', () => {
65         afterEach(() => {
66             courseServiceSpy.associateMaterials.calls.reset();
67         });
68
69         describe('item circ_lib is different from course owning lib', () => {
70             it('attempts to change item circ_lib to the course\'s library', waitForAsync(() => {
71                 const paramsWithCircLib = {
72                     barcode: '123',
73                     relationship: 'required reading',
74                     isModifyingLibrary: true,
75                     tempLibrary: 22, // the Library that owns the course, rather than the item's circ_lib
76                     currentCourse: mockCourse,
77                     isModifyingCallNumber: undefined, isModifyingCircMod: undefined,
78                     isModifyingLocation: undefined, isModifyingStatus: undefined,
79                     tempCircMod: undefined, tempLocation: undefined, tempStatus: undefined
80                 };
81                 component.associateItem('123', 'required reading');
82
83                 setTimeout(() => { // wait for the subscribe() to do its work
84                     expect(courseServiceSpy.associateMaterials).toHaveBeenCalledWith(mockItem, paramsWithCircLib);
85                 }, 500);
86             }));
87
88             it('asks the user to confirm', (waitForAsync(() => {
89                 component.associateItem('123', 'required reading');
90                 setTimeout(() => { // wait for the subscribe() to do its work
91                     expect(dialogComponentSpy.open).toHaveBeenCalled();
92                 }, 500);
93             })));
94
95             it('sets the owning library\'s shortname in the UI', (waitForAsync(() => {
96                 component.associateItem('123', 'required reading');
97                 setTimeout(() => { // wait for the subscribe() to do its work
98                     expect(component.itemCircLib).toBe('greatLibrary');
99                 }, 500);
100             })));
101
102             it('does not proceed if the user says "no" in the different library confirmation dialog', waitForAsync(() => {
103                 component.confirmOtherLibraryDialog = rejectedDialogComponentSpy;
104                 component.associateItem('123', 'required reading');
105
106                 setTimeout(() => { // wait for the subscribe() to do its work
107                     expect(rejectedDialogComponentSpy.open).toHaveBeenCalled();
108                     expect(courseServiceSpy.associateMaterials).not.toHaveBeenCalled();
109                 }, 500);
110             }));
111
112         });
113     });
114 });