Skip to content

Commit 96b5964

Browse files
committed
Add unit tests for the badge on bitstreams
1 parent de51261 commit 96b5964

2 files changed

Lines changed: 107 additions & 21 deletions

File tree

src/app/shared/object-collection/shared/badges/access-status-badge/access-status-badge.component.spec.ts

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { TranslateModule } from '@ngx-translate/core';
99
import { environment } from 'src/environments/environment';
1010

1111
import { AccessStatusDataService } from '../../../../../core/data/access-status-data.service';
12+
import { Bitstream } from '../../../../../core/shared/bitstream.model';
1213
import { Item } from '../../../../../core/shared/item.model';
1314
import { createSuccessfulRemoteDataObject$ } from '../../../../remote-data.utils';
1415
import { TruncatePipe } from '../../../../utils/truncate.pipe';
@@ -28,6 +29,7 @@ describe('ItemAccessStatusBadgeComponent', () => {
2829
let accessStatusDataService: AccessStatusDataService;
2930

3031
let item: Item;
32+
let bitstream: Bitstream;
3133

3234
function init() {
3335
unknownStatus = Object.assign(new AccessStatusObject(), {
@@ -44,6 +46,7 @@ describe('ItemAccessStatusBadgeComponent', () => {
4446

4547
embargoStatus = Object.assign(new AccessStatusObject(), {
4648
status: 'embargo',
49+
embargoDate: '2050-01-01',
4750
});
4851

4952
restrictedStatus = Object.assign(new AccessStatusObject(), {
@@ -58,6 +61,11 @@ describe('ItemAccessStatusBadgeComponent', () => {
5861
uuid: 'item-uuid',
5962
type: 'item',
6063
});
64+
65+
bitstream = Object.assign(new Bitstream(), {
66+
uuid: 'bitstream-uuid',
67+
type: 'bitstream',
68+
});
6169
}
6270

6371
function initTestBed() {
@@ -70,7 +78,7 @@ describe('ItemAccessStatusBadgeComponent', () => {
7078
}).compileComponents();
7179
}
7280

73-
function initFixtureAndComponent() {
81+
function initFixtureAndComponentWithItem() {
7482
environment.item.showAccessStatuses = true;
7583
fixture = TestBed.createComponent(AccessStatusBadgeComponent);
7684
component = fixture.componentInstance;
@@ -79,90 +87,163 @@ describe('ItemAccessStatusBadgeComponent', () => {
7987
environment.item.showAccessStatuses = false;
8088
}
8189

82-
function lookForAccessStatusBadge(status: string) {
90+
function initFixtureAndComponentWithBitstream() {
91+
environment.item.bitstream.showAccessStatuses = true;
92+
fixture = TestBed.createComponent(AccessStatusBadgeComponent);
93+
component = fixture.componentInstance;
94+
component.object = bitstream;
95+
fixture.detectChanges();
96+
environment.item.bitstream.showAccessStatuses = false;
97+
}
98+
99+
function lookForAccessStatusBadgeForItem(status: string) {
83100
const badge = fixture.debugElement.query(By.css('span.badge'));
84101
expect(badge.nativeElement.textContent).toEqual(`access-status.${status.toLowerCase()}.listelement.badge`);
85102
}
86103

87-
describe('init', () => {
104+
function lookForAccessStatusBadgeForBitstream() {
105+
const badge = fixture.debugElement.query(By.css('span.badge'));
106+
expect(badge.nativeElement.textContent).toEqual(`embargo.listelement.badge`);
107+
}
108+
109+
function lookForNoAccessStatusBadgeForBitstream() {
110+
const badge = fixture.debugElement.query(By.css('span.badge'));
111+
expect(badge.nativeElement.textContent).toEqual(``);
112+
}
113+
114+
describe('init with Item', () => {
88115
beforeEach(waitForAsync(() => {
89116
init();
90117
initTestBed();
91118
}));
92119
beforeEach(() => {
93-
initFixtureAndComponent();
120+
initFixtureAndComponentWithItem();
94121
});
95122
it('should init the component', () => {
96123
expect(component).toBeTruthy();
97124
});
98125
});
99126

100-
describe('When the findItemAccessStatusFor method returns unknown', () => {
127+
describe('When the findItemAccessStatusFor method returns unknown with Item', () => {
101128
beforeEach(waitForAsync(() => {
102129
init();
103130
initTestBed();
104131
}));
105132
beforeEach(() => {
106-
initFixtureAndComponent();
133+
initFixtureAndComponentWithItem();
107134
});
108135
it('should show the unknown badge', () => {
109-
lookForAccessStatusBadge('unknown');
136+
lookForAccessStatusBadgeForItem('unknown');
110137
});
111138
});
112139

113-
describe('When the findItemAccessStatusFor method returns metadata.only', () => {
140+
describe('When the findItemAccessStatusFor method returns metadata.only with Item', () => {
114141
beforeEach(waitForAsync(() => {
115142
init();
116143
(accessStatusDataService.findItemAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(metadataOnlyStatus));
117144
initTestBed();
118145
}));
119146
beforeEach(() => {
120-
initFixtureAndComponent();
147+
initFixtureAndComponentWithItem();
121148
});
122149
it('should show the metadata only badge', () => {
123-
lookForAccessStatusBadge('metadata.only');
150+
lookForAccessStatusBadgeForItem('metadata.only');
124151
});
125152
});
126153

127-
describe('When the findItemAccessStatusFor method returns open.access', () => {
154+
describe('When the findItemAccessStatusFor method returns open.access with Item', () => {
128155
beforeEach(waitForAsync(() => {
129156
init();
130157
(accessStatusDataService.findItemAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(openAccessStatus));
131158
initTestBed();
132159
}));
133160
beforeEach(() => {
134-
initFixtureAndComponent();
161+
initFixtureAndComponentWithItem();
135162
});
136163
it('should show the open access badge', () => {
137-
lookForAccessStatusBadge('open.access');
164+
lookForAccessStatusBadgeForItem('open.access');
138165
});
139166
});
140167

141-
describe('When the findItemAccessStatusFor method returns embargo', () => {
168+
describe('When the findItemAccessStatusFor method returns embargo with Item', () => {
142169
beforeEach(waitForAsync(() => {
143170
init();
144171
(accessStatusDataService.findItemAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(embargoStatus));
145172
initTestBed();
146173
}));
147174
beforeEach(() => {
148-
initFixtureAndComponent();
175+
initFixtureAndComponentWithItem();
149176
});
150177
it('should show the embargo badge', () => {
151-
lookForAccessStatusBadge('embargo');
178+
lookForAccessStatusBadgeForItem('embargo');
152179
});
153180
});
154181

155-
describe('When the findItemAccessStatusFor method returns restricted', () => {
182+
describe('When the findItemAccessStatusFor method returns restricted with Item', () => {
156183
beforeEach(waitForAsync(() => {
157184
init();
158185
(accessStatusDataService.findItemAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(restrictedStatus));
159186
initTestBed();
160187
}));
161188
beforeEach(() => {
162-
initFixtureAndComponent();
189+
initFixtureAndComponentWithItem();
163190
});
164191
it('should show the restricted badge', () => {
165-
lookForAccessStatusBadge('restricted');
192+
lookForAccessStatusBadgeForItem('restricted');
193+
});
194+
});
195+
196+
describe('init with Bitstream', () => {
197+
beforeEach(waitForAsync(() => {
198+
init();
199+
initTestBed();
200+
}));
201+
beforeEach(() => {
202+
initFixtureAndComponentWithBitstream();
203+
});
204+
it('should init the component', () => {
205+
expect(component).toBeTruthy();
206+
});
207+
});
208+
209+
describe('When the bitstream have no access status', () => {
210+
beforeEach(waitForAsync(() => {
211+
init();
212+
initTestBed();
213+
}));
214+
beforeEach(() => {
215+
initFixtureAndComponentWithBitstream();
216+
});
217+
it('should not show the badge', () => {
218+
lookForNoAccessStatusBadgeForBitstream();
219+
});
220+
});
221+
222+
describe('When the bitstream have an access status with no embargo date', () => {
223+
beforeEach(waitForAsync(() => {
224+
init();
225+
initTestBed();
226+
}));
227+
beforeEach(() => {
228+
bitstream.accessStatus = createSuccessfulRemoteDataObject$(openAccessStatus);
229+
initFixtureAndComponentWithBitstream();
230+
});
231+
it('should not show the badge', () => {
232+
lookForNoAccessStatusBadgeForBitstream();
233+
});
234+
});
235+
236+
describe('When the bitstream have an access status with an embargo date', () => {
237+
beforeEach(waitForAsync(() => {
238+
init();
239+
initTestBed();
240+
}));
241+
beforeEach(() => {
242+
bitstream.accessStatus = createSuccessfulRemoteDataObject$(embargoStatus);
243+
initFixtureAndComponentWithBitstream();
244+
});
245+
it('should show the badge', () => {
246+
lookForAccessStatusBadgeForBitstream();
166247
});
167248
});
168249
});

src/app/shared/object-collection/shared/badges/access-status-badge/access-status-badge.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,19 @@ export class AccessStatusBadgeComponent implements OnDestroy, OnInit {
135135
getFirstSucceededRemoteDataPayload(),
136136
map((accessStatus: AccessStatusObject) => {
137137
if (hasValue(accessStatus.embargoDate)) {
138-
this.accessStatus$ = observableOf('embargo.listelement.badge');
139138
return accessStatus.embargoDate;
140139
} else {
141-
this.accessStatus$ = observableOf(null);
142140
return null;
143141
}
144142
}),
145143
catchError(() => observableOf(null)),
146144
);
145+
this.subs.push(
146+
this.embargoDate$.pipe().subscribe((embargoDate: string) => {
147+
if (hasValue(embargoDate)) {
148+
this.accessStatus$ = observableOf('embargo.listelement.badge');
149+
}
150+
}),
151+
);
147152
}
148153
}

0 commit comments

Comments
 (0)