Skip to content

Commit da77ecc

Browse files
[DURACOM-383] update tests
1 parent 84110f3 commit da77ecc

2 files changed

Lines changed: 58 additions & 49 deletions

File tree

src/app/community-page/delete-community-page/delete-community-page.component.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('DeleteCommunityPageComponent', () => {
5050
scriptService = jasmine.createSpyObj('scriptService', {
5151
invoke: createSuccessfulRemoteDataObject$({ processId: '123' }),
5252
});
53-
router = jasmine.createSpyObj('router', ['navigateByUrl']);
53+
router = jasmine.createSpyObj('router', ['navigateByUrl', 'navigate']);
5454
TestBed.configureTestingModule({
5555
imports: [TranslateModule.forRoot(), CommonModule, RouterTestingModule, DeleteCommunityPageComponent],
5656
providers: [
@@ -85,14 +85,23 @@ describe('DeleteCommunityPageComponent', () => {
8585
Object.assign(new ProcessParameter(), { name: '-i', value: mockCommunity.uuid }),
8686
];
8787
(scriptService.invoke as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$({ processId: '123' }));
88-
expect(scriptService.invoke).toHaveBeenCalledWith(DSPACE_OBJECT_DELETION_SCRIPT_NAME, parameterValues, []);
89-
expect(notificationService.success).toHaveBeenCalledWith('community.delete.notification.success');
90-
expect(router.navigateByUrl).toHaveBeenCalledWith(getProcessDetailRoute('123'));
88+
comp.onConfirm(mockCommunity);
89+
setTimeout(() => {
90+
expect(scriptService.invoke).toHaveBeenCalledWith(DSPACE_OBJECT_DELETION_SCRIPT_NAME, parameterValues, []);
91+
expect(notificationService.success).toHaveBeenCalledWith('community.delete.notification.success');
92+
expect(router.navigateByUrl).toHaveBeenCalledWith(getProcessDetailRoute('123'));
93+
done();
94+
}, 0);
9195
});
9296

9397
it('error notification is shown', (done) => {
9498
(scriptService.invoke as jasmine.Spy).and.returnValue(createFailedRemoteDataObject$('Error', 500));
95-
expect(notificationService.error).toHaveBeenCalledWith('community.delete.notification.fail');
99+
comp.onConfirm(mockCommunity);
100+
setTimeout(() => {
101+
expect(notificationService.error).toHaveBeenCalledWith('community.delete.notification.fail');
102+
expect(router.navigate).toHaveBeenCalledWith(['/']);
103+
done();
104+
}, 0);
96105
});
97106
});
98107

src/app/item-page/edit-item-page/item-delete/item-delete.component.spec.ts

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,29 @@ import {
1414
import { RouterTestingModule } from '@angular/router/testing';
1515
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
1616
import { TranslateModule } from '@ngx-translate/core';
17-
import {
18-
EMPTY,
19-
of,
20-
} from 'rxjs';
17+
import { of } from 'rxjs';
2118

2219
import { LinkService } from '../../../core/cache/builders/link.service';
2320
import { EntityTypeDataService } from '../../../core/data/entity-type-data.service';
2421
import { ItemDataService } from '../../../core/data/item-data.service';
2522
import { ObjectUpdatesService } from '../../../core/data/object-updates/object-updates.service';
23+
import {
24+
DSPACE_OBJECT_DELETION_SCRIPT_NAME,
25+
ScriptDataService,
26+
} from '../../../core/data/processes/script-data.service';
2627
import { RelationshipDataService } from '../../../core/data/relationship-data.service';
2728
import { RelationshipTypeDataService } from '../../../core/data/relationship-type-data.service';
2829
import { Item } from '../../../core/shared/item.model';
2930
import { ItemType } from '../../../core/shared/item-relationships/item-type.model';
3031
import { Relationship } from '../../../core/shared/item-relationships/relationship.model';
3132
import { RelationshipType } from '../../../core/shared/item-relationships/relationship-type.model';
33+
import { getProcessDetailRoute } from '../../../process-page/process-page-routing.paths';
34+
import { ProcessParameter } from '../../../process-page/processes/process-parameter.model';
3235
import { getMockThemeService } from '../../../shared/mocks/theme-service.mock';
3336
import { NotificationsService } from '../../../shared/notifications/notifications.service';
3437
import { ListableObjectComponentLoaderComponent } from '../../../shared/object-collection/shared/listable-object/listable-object-component-loader.component';
3538
import {
39+
createFailedRemoteDataObject$,
3640
createSuccessfulRemoteDataObject,
3741
createSuccessfulRemoteDataObject$,
3842
} from '../../../shared/remote-data.utils';
@@ -64,6 +68,7 @@ let linkService;
6468
let entityTypeService;
6569
let notificationsServiceStub;
6670
let typesSelection;
71+
let scriptDataService;
6772

6873
describe('ItemDeleteComponent', () => {
6974
beforeEach(waitForAsync(() => {
@@ -163,6 +168,10 @@ describe('ItemDeleteComponent', () => {
163168

164169
notificationsServiceStub = new NotificationsServiceStub();
165170

171+
scriptDataService = {
172+
invoke: jasmine.createSpy('invoke').and.returnValue(createSuccessfulRemoteDataObject$({ processId: '123' })),
173+
};
174+
166175
TestBed.configureTestingModule({
167176
imports: [CommonModule, FormsModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule, ItemDeleteComponent, VarDirective],
168177
providers: [
@@ -176,6 +185,7 @@ describe('ItemDeleteComponent', () => {
176185
{ provide: RelationshipTypeDataService, useValue: {} },
177186
{ provide: LinkService, useValue: linkService },
178187
{ provide: ThemeService, useValue: getMockThemeService() },
188+
{ provide: ScriptDataService, useValue: scriptDataService },
179189
], schemas: [
180190
CUSTOM_ELEMENTS_SCHEMA,
181191
],
@@ -189,6 +199,10 @@ describe('ItemDeleteComponent', () => {
189199
beforeEach(() => {
190200
fixture = TestBed.createComponent(ItemDeleteComponent);
191201
comp = fixture.componentInstance;
202+
// Verifica che il componente stia usando il mock corretto
203+
expect(comp.scriptDataService).toBe(scriptDataService);
204+
console.log('scriptDataService mock:', scriptDataService);
205+
console.log('comp scriptDataService:', comp.scriptDataService);
192206
fixture.detectChanges();
193207
});
194208

@@ -204,54 +218,40 @@ describe('ItemDeleteComponent', () => {
204218
});
205219

206220
describe('performAction', () => {
207-
describe(`when there are entitytypes`, () => {
208-
it('should call delete function from the ItemDataService', () => {
209-
spyOn(comp, 'notify');
210-
comp.performAction();
211-
expect(mockItemDataService.delete)
212-
.toHaveBeenCalledWith(mockItem.id, types.filter((type) => typesSelection[type]).map((type) => type.id));
213-
expect(comp.notify).toHaveBeenCalled();
214-
});
215-
216-
it('should call delete function from the ItemDataService with empty types', () => {
217-
218-
spyOn(comp, 'notify');
219-
jasmine.getEnv().allowRespy(true);
220-
spyOn(entityTypeService, 'getEntityTypeRelationships').and.returnValue([]);
221-
comp.ngOnInit();
222-
223-
comp.performAction();
224-
225-
expect(mockItemDataService.delete).toHaveBeenCalledWith(mockItem.id, []);
226-
expect(comp.notify).toHaveBeenCalled();
227-
});
221+
it('should invoke the deletion script with correct params, show success notification and redirect on success', (done) => {
222+
const parameterValues: ProcessParameter[] = [
223+
Object.assign(new ProcessParameter(), { name: '-i', value: mockItem.uuid }),
224+
];
225+
scriptDataService.invoke.and.returnValue(createSuccessfulRemoteDataObject$({ processId: '123' }));
226+
console.log('Before performAction - scriptDataService.invoke:', scriptDataService.invoke);
227+
console.log('Before performAction - isSpy:', jasmine.isSpy(scriptDataService.invoke));
228+
comp.performAction();
229+
setTimeout(() => {
230+
console.log('After performAction - scriptDataService.invoke:', scriptDataService.invoke);
231+
console.log('After performAction - calls:', scriptDataService.invoke.calls);
232+
console.log('After performAction - isSpy:', jasmine.isSpy(scriptDataService.invoke));
233+
expect(scriptDataService.invoke).toHaveBeenCalledWith(DSPACE_OBJECT_DELETION_SCRIPT_NAME, parameterValues, []);
234+
expect(comp.notificationsService.success).toHaveBeenCalled();
235+
expect(comp.router.navigateByUrl).toHaveBeenCalledWith(getProcessDetailRoute('123'));
236+
done();
237+
}, 0);
228238
});
229-
230-
describe(`when there are no entity types`, () => {
231-
beforeEach(() => {
232-
(comp as any).entityTypeService = jasmine.createSpyObj('entityTypeService',
233-
{
234-
getEntityTypeByLabel: EMPTY,
235-
},
236-
);
237-
});
238-
239-
it('should call delete function from the ItemDataService', () => {
240-
spyOn(comp, 'notify');
241-
comp.performAction();
242-
expect(mockItemDataService.delete)
243-
.toHaveBeenCalledWith(mockItem.id, types.filter((type) => typesSelection[type]).map((type) => type.id));
244-
expect(comp.notify).toHaveBeenCalled();
245-
});
239+
it('should show error notification and redirect to item edit page on failure', (done) => {
240+
scriptDataService.invoke.and.returnValue(createFailedRemoteDataObject$('Error', 500));
241+
comp.performAction();
242+
setTimeout(() => {
243+
expect(comp.notificationsService.error).toHaveBeenCalled();
244+
expect(comp.router.navigate).toHaveBeenCalledWith([getItemEditRoute(mockItem)]);
245+
done();
246+
}, 0);
246247
});
247248
});
248249
describe('notify', () => {
249250
it('should navigate to the homepage on successful deletion of the item', () => {
250251
comp.notify(true);
251252
expect(routerStub.navigate).toHaveBeenCalledWith(['']);
252253
});
253-
});
254-
describe('notify', () => {
254+
255255
it('should navigate to the item edit page on failed deletion of the item', () => {
256256
comp.notify(false);
257257
expect(routerStub.navigate).toHaveBeenCalledWith([getItemEditRoute(mockItem)]);

0 commit comments

Comments
 (0)