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';
14 describe('CourseAssociateMaterialComponent', () => {
15 let component: CourseAssociateMaterialComponent;
19 shortname: () => 'greatLibrary'
22 const mockLibrary2 = {
31 circ_lib: () => mockLibrary
38 owning_lib: () => mockLibrary2
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));
57 component = new CourseAssociateMaterialComponent(authServiceSpy, courseServiceSpy,
58 netServiceSpy, pcrudServiceSpy,
59 toastServiceSpy, permServiceSpy, modalSpy);
60 component.confirmOtherLibraryDialog = dialogComponentSpy;
61 component.currentCourse = mockCourse;
64 describe('#associateItem method', () => {
66 courseServiceSpy.associateMaterials.calls.reset();
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 = {
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
81 component.associateItem('123', 'required reading');
83 setTimeout(() => { // wait for the subscribe() to do its work
84 expect(courseServiceSpy.associateMaterials).toHaveBeenCalledWith(mockItem, paramsWithCircLib);
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();
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');
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');
106 setTimeout(() => { // wait for the subscribe() to do its work
107 expect(rejectedDialogComponentSpy.open).toHaveBeenCalled();
108 expect(courseServiceSpy.associateMaterials).not.toHaveBeenCalled();