|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { |
| 3 | + ComponentFixture, |
| 4 | + TestBed, |
| 5 | + waitForAsync, |
| 6 | +} from '@angular/core/testing'; |
| 7 | +import { By } from '@angular/platform-browser'; |
| 8 | +import { provideNoopAnimations } from '@angular/platform-browser/animations'; |
| 9 | +import { provideRouter } from '@angular/router'; |
| 10 | +import { DSONameService } from '@dspace/core/breadcrumbs/dso-name.service'; |
| 11 | +import { Item } from '@dspace/core/shared/item.model'; |
| 12 | +import { SearchObjects } from '@dspace/core/shared/search/models/search-objects.model'; |
| 13 | +import { SearchResult } from '@dspace/core/shared/search/models/search-result.model'; |
| 14 | +import { DSONameServiceMock } from '@dspace/core/testing/dso-name.service.mock'; |
| 15 | +import { TranslateLoaderMock } from '@dspace/core/testing/translate-loader.mock'; |
| 16 | +import { URLCombiner } from '@dspace/core/url-combiner/url-combiner'; |
| 17 | +import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote-data.utils'; |
| 18 | +import { |
| 19 | + TranslateLoader, |
| 20 | + TranslateModule, |
| 21 | +} from '@ngx-translate/core'; |
| 22 | +import { throwError } from 'rxjs'; |
| 23 | + |
| 24 | +import { AlertComponent } from '../../../shared/alert/alert.component'; |
| 25 | +import { SearchService } from '../../../shared/search/search.service'; |
| 26 | +import { getItemEditRoute } from '../../item-page-routing-paths'; |
| 27 | +import { CustomUrlConflictErrorComponent } from './custom-url-conflict-error.component'; |
| 28 | + |
| 29 | +@Component({ |
| 30 | + selector: 'ds-alert', |
| 31 | + template: '<ng-content></ng-content>', |
| 32 | +}) |
| 33 | +class AlertComponentStub {} |
| 34 | + |
| 35 | +describe('CustomUrlConflictErrorComponent', () => { |
| 36 | + let component: CustomUrlConflictErrorComponent; |
| 37 | + let fixture: ComponentFixture<CustomUrlConflictErrorComponent>; |
| 38 | + let searchServiceSpy: jasmine.SpyObj<SearchService>; |
| 39 | + |
| 40 | + const customUrl = 'my-conflicting-url'; |
| 41 | + |
| 42 | + const mockItem1 = Object.assign(new Item(), { |
| 43 | + uuid: 'item-uuid-1', |
| 44 | + name: 'Item One', |
| 45 | + metadata: {}, |
| 46 | + _links: { self: { href: 'item-1-selflink' } }, |
| 47 | + }); |
| 48 | + |
| 49 | + const mockItem2 = Object.assign(new Item(), { |
| 50 | + uuid: 'item-uuid-2', |
| 51 | + name: 'Item Two', |
| 52 | + metadata: {}, |
| 53 | + _links: { self: { href: 'item-2-selflink' } }, |
| 54 | + }); |
| 55 | + |
| 56 | + const buildSearchResult = (item: Item): SearchResult<Item> => |
| 57 | + Object.assign(new SearchResult<Item>(), { indexableObject: item }); |
| 58 | + |
| 59 | + const buildSearchObjects = (items: Item[]): SearchObjects<Item> => |
| 60 | + Object.assign(new SearchObjects<Item>(), { page: items.map(buildSearchResult) }); |
| 61 | + |
| 62 | + const expectedEditLink = (item: Item) => |
| 63 | + new URLCombiner(getItemEditRoute(item, true), 'metadata').toString(); |
| 64 | + |
| 65 | + const createComponent = () => { |
| 66 | + fixture = TestBed.createComponent(CustomUrlConflictErrorComponent); |
| 67 | + component = fixture.componentInstance; |
| 68 | + component.customUrl = customUrl; |
| 69 | + fixture.detectChanges(); |
| 70 | + }; |
| 71 | + |
| 72 | + beforeEach(waitForAsync(() => { |
| 73 | + searchServiceSpy = jasmine.createSpyObj('SearchService', ['search']); |
| 74 | + // Default: return empty results — overridden per suite below |
| 75 | + searchServiceSpy.search.and.returnValue( |
| 76 | + createSuccessfulRemoteDataObject$(buildSearchObjects([])), |
| 77 | + ); |
| 78 | + |
| 79 | + TestBed.configureTestingModule({ |
| 80 | + imports: [ |
| 81 | + CustomUrlConflictErrorComponent, |
| 82 | + TranslateModule.forRoot({ |
| 83 | + loader: { provide: TranslateLoader, useClass: TranslateLoaderMock }, |
| 84 | + }), |
| 85 | + ], |
| 86 | + providers: [ |
| 87 | + provideNoopAnimations(), |
| 88 | + provideRouter([]), |
| 89 | + { provide: SearchService, useValue: searchServiceSpy }, |
| 90 | + { provide: DSONameService, useValue: new DSONameServiceMock() }, |
| 91 | + ], |
| 92 | + }) |
| 93 | + .overrideComponent(CustomUrlConflictErrorComponent, { |
| 94 | + remove: { imports: [AlertComponent] }, |
| 95 | + add: { imports: [AlertComponentStub] }, |
| 96 | + }) |
| 97 | + .compileComponents(); |
| 98 | + })); |
| 99 | + |
| 100 | + describe('when search returns matching items', () => { |
| 101 | + beforeEach(() => { |
| 102 | + searchServiceSpy.search.and.returnValue( |
| 103 | + createSuccessfulRemoteDataObject$(buildSearchObjects([mockItem1, mockItem2])), |
| 104 | + ); |
| 105 | + createComponent(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should create', () => { |
| 109 | + expect(component).toBeTruthy(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should call SearchService.search with a query containing the custom URL', () => { |
| 113 | + expect(searchServiceSpy.search).toHaveBeenCalledOnceWith( |
| 114 | + jasmine.objectContaining({ query: `dspace.customurl:${customUrl}` }), |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should emit one entry per conflicting item', (done) => { |
| 119 | + component.conflictingItems$.subscribe((items) => { |
| 120 | + expect(items.length).toBe(2); |
| 121 | + done(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should build the correct metadata edit link for each item', (done) => { |
| 126 | + component.conflictingItems$.subscribe((items) => { |
| 127 | + expect(items[0].editLink).toBe(expectedEditLink(mockItem1)); |
| 128 | + expect(items[1].editLink).toBe(expectedEditLink(mockItem2)); |
| 129 | + done(); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should use DSONameService to resolve item names', (done) => { |
| 134 | + component.conflictingItems$.subscribe((items) => { |
| 135 | + expect(items[0].name).toBe(mockItem1.name); |
| 136 | + expect(items[1].name).toBe(mockItem2.name); |
| 137 | + done(); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should render one edit link per item in the template', () => { |
| 142 | + fixture.detectChanges(); |
| 143 | + const links = fixture.debugElement.queryAll(By.css('ul li a')); |
| 144 | + expect(links.length).toBe(2); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('when search returns no items', () => { |
| 149 | + beforeEach(() => { |
| 150 | + searchServiceSpy.search.and.returnValue( |
| 151 | + createSuccessfulRemoteDataObject$(buildSearchObjects([])), |
| 152 | + ); |
| 153 | + createComponent(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should emit an empty array', (done) => { |
| 157 | + component.conflictingItems$.subscribe((items) => { |
| 158 | + expect(items.length).toBe(0); |
| 159 | + done(); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should show the no-items-found message', () => { |
| 164 | + fixture.detectChanges(); |
| 165 | + const compiled = fixture.nativeElement as HTMLElement; |
| 166 | + expect(compiled.textContent).toContain('error.custom-url-conflict.no-items-found'); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('when search throws an error', () => { |
| 171 | + beforeEach(() => { |
| 172 | + searchServiceSpy.search.and.returnValue(throwError(() => new Error('Network error'))); |
| 173 | + createComponent(); |
| 174 | + }); |
| 175 | + |
| 176 | + it('should emit an empty array on error', (done) => { |
| 177 | + component.conflictingItems$.subscribe((items) => { |
| 178 | + expect(items).toEqual([]); |
| 179 | + done(); |
| 180 | + }); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
| 184 | + |
0 commit comments