Skip to content

Commit 9c5e6bb

Browse files
[DURACOM-426] fix some unit tests
1 parent d262480 commit 9c5e6bb

5 files changed

Lines changed: 191 additions & 140 deletions

File tree

Lines changed: 120 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,132 @@
1-
import { Component } from '@angular/core';
2-
import {
3-
ComponentFixture,
4-
TestBed,
5-
} from '@angular/core/testing';
6-
import { By } from '@angular/platform-browser';
7-
8-
import { EntityIconDirective } from './entity-icon.directive';
9-
10-
describe('EntityIconDirective', () => {
11-
let component: TestComponent;
12-
let fixture: ComponentFixture<TestComponent>;
13-
14-
beforeEach(async () => {
15-
await TestBed.configureTestingModule({
16-
imports: [EntityIconDirective,
17-
TestComponent],
18-
})
19-
.compileComponents();
20-
});
21-
22-
describe('with default value provided', () => {
23-
beforeEach(() => {
24-
fixture = TestBed.createComponent(TestComponent);
25-
component = fixture.componentInstance;
26-
fixture.detectChanges();
27-
});
28-
29-
it('should create', () => {
30-
expect(component).toBeTruthy();
31-
});
32-
33-
it('should display a text-success icon', () => {
34-
const successIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i.text-success'));
35-
expect(successIcon).toBeTruthy();
36-
});
37-
38-
it('should display a text-success icon after span', () => {
39-
const successIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i.text-success'));
40-
const entityElement = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]'));
41-
// position 1 because the icon is after the span
42-
expect(entityElement.nativeElement.children[1]).toBe(successIcon.nativeNode);
43-
});
44-
});
45-
46-
describe('with primary value provided', () => {
47-
beforeEach(() => {
48-
fixture = TestBed.createComponent(TestComponent);
49-
component = fixture.componentInstance;
50-
component.metadata.entityType = 'person';
51-
component.metadata.entityStyle = 'personStaff';
52-
component.iconPosition = 'before';
53-
fixture.detectChanges();
54-
});
55-
56-
it('should display a text-primary icon', () => {
57-
const primaryIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i.text-primary'));
58-
expect(primaryIcon).toBeTruthy();
1+
import { Component } from '@angular/core';
2+
import {
3+
ComponentFixture,
4+
TestBed,
5+
} from '@angular/core/testing';
6+
7+
import { EntityIconDirective } from './entity-icon.directive';
8+
9+
describe('EntityIconDirective', () => {
10+
let component: TestComponent;
11+
let fixture: ComponentFixture<TestComponent>;
12+
13+
beforeEach(async () => {
14+
await TestBed.configureTestingModule({
15+
imports: [
16+
EntityIconDirective,
17+
TestComponent
18+
],
19+
}).compileComponents();
5920
});
6021

61-
it('should display a text-primary icon before span', () => {
62-
const primaryIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i.text-primary'));
63-
const entityElement = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]'));
64-
// position 0 because the icon is before the span
65-
expect(entityElement.nativeElement.children[0]).toBe(primaryIcon.nativeNode);
22+
describe('with default value provided', () => {
23+
beforeEach(() => {
24+
fixture = TestBed.createComponent(TestComponent);
25+
component = fixture.componentInstance;
26+
fixture.detectChanges();
27+
});
28+
29+
it('should create', () => {
30+
expect(component).toBeTruthy();
31+
});
32+
33+
it('should display a text-success icon', () => {
34+
const successIcon = fixture.nativeElement.querySelector('[data-test="entityTestComponent"]').querySelector('i.text-success');
35+
expect(successIcon).toBeTruthy();
36+
});
37+
38+
it('should display a text-success icon after span', () => {
39+
const successIcon = fixture.nativeElement.querySelector('[data-test="entityTestComponent"]').querySelector('i.text-success');
40+
const entityElement = fixture.nativeElement.querySelector('[data-test="entityTestComponent"]');
41+
// position 1 because the icon is after the span
42+
expect(entityElement.children[1]).toBe(successIcon);
43+
});
6644
});
67-
});
6845

69-
describe('when given type doesn\'t exist and fallback on default disabled', () => {
70-
beforeEach(() => {
71-
fixture = TestBed.createComponent(TestComponent);
72-
component = fixture.componentInstance;
73-
component.fallbackOnDefault = false;
74-
component.metadata.entityType = 'TESTFAKE';
75-
component.metadata.entityStyle = 'personFallback';
76-
component.iconPosition = 'before';
77-
fixture.detectChanges();
46+
describe('with primary value provided', () => {
47+
beforeEach(() => {
48+
fixture = TestBed.createComponent(TestComponent);
49+
component = fixture.componentInstance;
50+
component.metadata.entityType = 'person';
51+
component.metadata.entityStyle = 'personStaff';
52+
component.iconPosition = 'before';
53+
fixture.detectChanges();
54+
});
55+
56+
it('should display a text-primary icon', () => {
57+
const primaryIcon = fixture.nativeElement.querySelector('[data-test="entityTestComponent"]').querySelector('i.text-primary');
58+
expect(primaryIcon).toBeTruthy();
59+
});
60+
61+
it('should display a text-primary icon before span', () => {
62+
const primaryIcon = fixture.nativeElement.querySelector('[data-test="entityTestComponent"]').querySelector('i.text-primary');
63+
const entityElement = fixture.nativeElement.querySelector('[data-test="entityTestComponent"]');
64+
// position 0 because the icon is before the span
65+
expect(entityElement.children[0]).toBe(primaryIcon);
66+
});
7867
});
7968

80-
it('should not display a text-primary icon', () => {
81-
const primaryIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i'));
82-
expect(primaryIcon).toBeFalsy();
69+
describe('when given type doesn\'t exist and fallback on default disabled', () => {
70+
beforeEach(() => {
71+
fixture = TestBed.createComponent(TestComponent);
72+
component = fixture.componentInstance;
73+
component.fallbackOnDefault = false;
74+
component.metadata.entityType = 'TESTFAKE';
75+
component.metadata.entityStyle = 'personFallback';
76+
component.iconPosition = 'before';
77+
fixture.detectChanges();
78+
});
79+
80+
it('should not display a text-primary icon', () => {
81+
const primaryIcon = fixture.nativeElement.querySelector('[data-test="entityTestComponent"]').querySelector('i');
82+
expect(primaryIcon).toBeFalsy();
83+
});
8384
});
84-
});
8585

86-
describe('when given style doesn\'t exist and fallback on default disabled', () => {
87-
beforeEach(() => {
88-
fixture = TestBed.createComponent(TestComponent);
89-
component = fixture.componentInstance;
90-
component.fallbackOnDefault = false;
91-
component.metadata.entityType = 'person';
92-
component.metadata.entityStyle = 'personFallback';
93-
component.iconPosition = 'before';
94-
fixture.detectChanges();
86+
describe('when given style doesn\'t exist and fallback on default disabled', () => {
87+
beforeEach(() => {
88+
fixture = TestBed.createComponent(TestComponent);
89+
component = fixture.componentInstance;
90+
component.fallbackOnDefault = false;
91+
component.metadata.entityType = 'person';
92+
component.metadata.entityStyle = 'personFallback';
93+
component.iconPosition = 'before';
94+
fixture.detectChanges();
95+
});
96+
97+
it('should not display a text-primary icon', () => {
98+
const primaryIcon = fixture.nativeElement.querySelector('[data-test="entityTestComponent"]').querySelector('i');
99+
expect(primaryIcon).toBeFalsy();
100+
});
95101
});
96102

97-
it('should not display a text-primary icon', () => {
98-
const primaryIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i'));
99-
expect(primaryIcon).toBeFalsy();
100-
});
101103
});
102104

103-
});
104-
105-
// declare a test component
106-
@Component({
107-
selector: 'ds-test-cmp',
108-
template: `
109-
<div [attr.data-test]="'entityTestComponent'">
110-
<span dsEntityIcon
111-
[iconPosition]="iconPosition"
112-
[entityType]="metadata.entityType"
113-
[entityStyle]="metadata.entityStyle"
114-
[fallbackOnDefault]="fallbackOnDefault">{{ metadata.value }}</span></div>`,
115-
standalone: true,
116-
imports: [
117-
EntityIconDirective,
118-
],
119-
})
120-
class TestComponent {
121-
122-
metadata = {
123-
authority: null,
124-
value: 'Test',
125-
orcidAuthenticated: null,
126-
entityType: 'default',
127-
entityStyle: 'default',
128-
};
129-
iconPosition = 'after';
130-
fallbackOnDefault = true;
131-
132-
}
105+
// declare a test component
106+
@Component({
107+
selector: 'ds-test-cmp',
108+
template: `
109+
<div [attr.data-test]="'entityTestComponent'">
110+
<span dsEntityIcon
111+
[iconPosition]="iconPosition"
112+
[entityType]="metadata.entityType"
113+
[entityStyle]="metadata.entityStyle"
114+
[fallbackOnDefault]="fallbackOnDefault">{{ metadata.value }}</span></div>`,
115+
standalone: true,
116+
imports: [
117+
EntityIconDirective,
118+
],
119+
})
120+
class TestComponent {
121+
122+
metadata = {
123+
authority: null,
124+
value: 'Test',
125+
orcidAuthenticated: null,
126+
entityType: 'default',
127+
entityStyle: 'default',
128+
};
129+
iconPosition = 'after';
130+
fallbackOnDefault = true;
131+
132+
}

src/app/shared/entity-icon/entity-icon.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import {
99
CrisRefConfig,
1010
CrisRefEntityStyleConfig,
11-
} from '../../../config/layout-config.interfaces';
11+
} from '@dspace/config/layout-config.interfaces';
1212
import { environment } from '../../../environments/environment';
1313
import { isEmpty, isNotEmpty } from "@dspace/shared/utils/empty.util";
1414

src/app/shared/metadata-link-view/metadata-link-view-avatar-popover/metadata-link-view-avatar-popover.component.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
TestBed,
66
waitForAsync,
77
} from '@angular/core/testing';
8-
import { TranslateService } from '@ngx-translate/core';
8+
import { TranslateModule, TranslateService } from '@ngx-translate/core';
99
import {
1010
of as observableOf,
1111
of,
@@ -16,7 +16,6 @@ import { AuthorizationDataService } from '@dspace/core/data/feature-authorizatio
1616
import { FileService } from '@dspace/core/shared/file.service';
1717
import { ThemedLoadingComponent } from '../../loading/themed-loading.component';
1818
import { MetadataLinkViewAvatarPopoverComponent } from './metadata-link-view-avatar-popover.component';
19-
import { getMockTranslateService } from "@dspace/core/testing/translate.service.mock";
2019

2120
describe('MetadataLinkViewAvatarPopoverComponent', () => {
2221
let component: MetadataLinkViewAvatarPopoverComponent;
@@ -36,13 +35,22 @@ describe('MetadataLinkViewAvatarPopoverComponent', () => {
3635
fileService = jasmine.createSpyObj('FileService', {
3736
retrieveFileDownloadLink: null,
3837
});
38+
translateServiceStub = {
39+
get: () => of('translated-text'),
40+
onLangChange: new EventEmitter(),
41+
onTranslationChange: new EventEmitter(),
42+
onDefaultLangChange: new EventEmitter(),
43+
};
3944
TestBed.configureTestingModule({
40-
imports: [MetadataLinkViewAvatarPopoverComponent],
45+
imports: [
46+
MetadataLinkViewAvatarPopoverComponent,
47+
TranslateModule.forRoot(),
48+
],
4149
providers: [
4250
{ provide: AuthService, useValue: authService },
4351
{ provide: AuthorizationDataService, useValue: authorizationService },
4452
{ provide: FileService, useValue: fileService },
45-
{ provide: TranslateService, useValue: getMockTranslateService() },
53+
{ provide: TranslateService, useValue: translateServiceStub },
4654
],
4755
})
4856
.overrideComponent(MetadataLinkViewAvatarPopoverComponent, { remove: { imports: [ThemedLoadingComponent] } }).compileComponents();

src/app/shared/object-list/search-result-list-element/item-search-result/item-types/item/item-search-result-list-element.component.spec.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ import { ThemeService } from '../../../../../theme-support/theme.service';
3030
import { TruncatableService } from '../../../../../truncatable/truncatable.service';
3131
import { TruncatePipe } from '../../../../../utils/truncate.pipe';
3232
import { ItemSearchResultListElementComponent } from './item-search-result-list-element.component';
33-
import { provideMockStore } from "@ngrx/store/testing";
33+
import { ThemedThumbnailComponent } from "src/app/thumbnail/themed-thumbnail.component";
34+
import { TruncatableComponent } from "src/app/shared/truncatable/truncatable.component";
35+
import { TruncatablePartComponent } from "src/app/shared/truncatable/truncatable-part/truncatable-part.component";
36+
import { MetadataLinkViewComponent } from "src/app/shared/metadata-link-view/metadata-link-view.component";
37+
import { APP_DATA_SERVICES_MAP } from "@dspace/core/data-services-map-type";
3438

3539
let publicationListElementComponent: ItemSearchResultListElementComponent;
3640
let fixture: ComponentFixture<ItemSearchResultListElementComponent>;
@@ -226,11 +230,18 @@ describe('ItemSearchResultListElementComponent', () => {
226230
'invalidateAuthorizationsRequestCache',
227231
]),
228232
},
229-
provideMockStore()
233+
{ provide: APP_DATA_SERVICES_MAP, useValue: {} },
230234
],
231235
schemas: [NO_ERRORS_SCHEMA],
232236
}).overrideComponent(ItemSearchResultListElementComponent, {
233237
add: { changeDetection: ChangeDetectionStrategy.Default },
238+
}).overrideComponent(ItemSearchResultListElementComponent, {
239+
remove: { imports: [
240+
ThemedThumbnailComponent,
241+
TruncatableComponent,
242+
TruncatablePartComponent,
243+
MetadataLinkViewComponent,
244+
] },
234245
}).compileComponents();
235246
}));
236247

@@ -279,6 +290,32 @@ describe('ItemSearchResultListElementComponent', () => {
279290
});
280291
});
281292

293+
describe('When the item has authors and isCollapsed is true', () => {
294+
beforeEach(() => {
295+
spyOn(publicationListElementComponent, 'isCollapsed').and.returnValue(of(true));
296+
publicationListElementComponent.object = mockItemWithMetadata;
297+
fixture.detectChanges();
298+
});
299+
300+
it('should show limitedMetadata', () => {
301+
const authorElements = fixture.debugElement.queryAll(By.css('span.item-list-authors ds-metadata-link-view'));
302+
expect(authorElements.length).toBe(mockItemWithMetadata.indexableObject.limitedMetadata(publicationListElementComponent.authorMetadata, publicationListElementComponent.additionalMetadataLimit).length);
303+
});
304+
});
305+
306+
describe('When the item has authors and isCollapsed is false', () => {
307+
beforeEach(() => {
308+
spyOn(publicationListElementComponent, 'isCollapsed').and.returnValue(of(false));
309+
publicationListElementComponent.object = mockItemWithMetadata;
310+
fixture.detectChanges();
311+
});
312+
313+
it('should show allMetadata', () => {
314+
const authorElements = fixture.debugElement.queryAll(By.css('span.item-list-authors ds-metadata-link-view'));
315+
expect(authorElements.length).toBe(mockItemWithMetadata.indexableObject.allMetadata(publicationListElementComponent.authorMetadata).length);
316+
});
317+
});
318+
282319
describe('When the item has a publisher', () => {
283320
beforeEach(() => {
284321
publicationListElementComponent.object = mockItemWithMetadata;
@@ -415,6 +452,13 @@ describe('ItemSearchResultListElementComponent', () => {
415452
schemas: [NO_ERRORS_SCHEMA],
416453
}).overrideComponent(ItemSearchResultListElementComponent, {
417454
set: { changeDetection: ChangeDetectionStrategy.Default },
455+
}).overrideComponent(ItemSearchResultListElementComponent, {
456+
remove: { imports: [
457+
ThemedThumbnailComponent,
458+
TruncatableComponent,
459+
TruncatablePartComponent,
460+
MetadataLinkViewComponent,
461+
] },
418462
}).compileComponents();
419463
}));
420464

0 commit comments

Comments
 (0)