Skip to content

Commit 13435e6

Browse files
authored
Merge pull request #5110 from saschaszott/saschaszott-patch-16
[Port dspace-9_x] Enhance FileSizePipe to support localization
2 parents 2ebd700 + e75bb3d commit 13435e6

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

src/app/item-page/full/field-components/file-section/full-file-section.component.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { APP_CONFIG } from 'src/config/app-config.interface';
1616
import { environment } from 'src/environments/environment';
1717

1818
import { BitstreamDataService } from '../../../../core/data/bitstream-data.service';
19+
import { LocaleService } from '../../../../core/locale/locale.service';
1920
import { PaginationService } from '../../../../core/pagination/pagination.service';
2021
import { Bitstream } from '../../../../core/shared/bitstream.model';
2122
import { SearchConfigurationService } from '../../../../core/shared/search/search-configuration.service';
@@ -37,6 +38,12 @@ import { FullFileSectionComponent } from './full-file-section.component';
3738
describe('FullFileSectionComponent', () => {
3839
let comp: FullFileSectionComponent;
3940
let fixture: ComponentFixture<FullFileSectionComponent>;
41+
let localeService: any;
42+
const languageList = ['en;q=1', 'de;q=0.8'];
43+
const mockLocaleService = jasmine.createSpyObj('LocaleService', {
44+
getCurrentLanguageCode: jasmine.createSpy('getCurrentLanguageCode'),
45+
getLanguageCodeList: of(languageList),
46+
});
4047

4148
const mockBitstream: Bitstream = Object.assign(new Bitstream(),
4249
{
@@ -93,6 +100,7 @@ describe('FullFileSectionComponent', () => {
93100
{ provide: NotificationsService, useValue: new NotificationsServiceStub() },
94101
{ provide: SearchConfigurationService, useValue: jasmine.createSpyObj(['getCurrentConfiguration']) },
95102
{ provide: PaginationService, useValue: paginationService },
103+
{ provide: LocaleService, useValue: mockLocaleService },
96104
{ provide: APP_CONFIG, useValue: environment },
97105
],
98106
schemas: [NO_ERRORS_SCHEMA],
@@ -104,6 +112,8 @@ describe('FullFileSectionComponent', () => {
104112
}));
105113

106114
beforeEach(waitForAsync(() => {
115+
localeService = TestBed.inject(LocaleService);
116+
localeService.getCurrentLanguageCode.and.returnValue(of('en'));
107117
fixture = TestBed.createComponent(FullFileSectionComponent);
108118
comp = fixture.componentInstance;
109119
fixture.detectChanges();

src/app/item-page/simple/field-components/file-section/file-section.component.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
APP_DATA_SERVICES_MAP,
2121
} from '../../../../../config/app-config.interface';
2222
import { BitstreamDataService } from '../../../../core/data/bitstream-data.service';
23+
import { LocaleService } from '../../../../core/locale/locale.service';
2324
import { Bitstream } from '../../../../core/shared/bitstream.model';
2425
import { PageInfo } from '../../../../core/shared/page-info.model';
2526
import { XSRFService } from '../../../../core/xsrf/xsrf.service';
@@ -40,6 +41,12 @@ import { FileSectionComponent } from './file-section.component';
4041
describe('FileSectionComponent', () => {
4142
let comp: FileSectionComponent;
4243
let fixture: ComponentFixture<FileSectionComponent>;
44+
let localeService: any;
45+
const languageList = ['en;q=1', 'de;q=0.8'];
46+
const mockLocaleService = jasmine.createSpyObj('LocaleService', {
47+
getCurrentLanguageCode: jasmine.createSpy('getCurrentLanguageCode'),
48+
getLanguageCodeList: of(languageList),
49+
});
4350

4451
const bitstreamDataService = jasmine.createSpyObj('bitstreamDataService', {
4552
findAllByItemAndBundleName: createSuccessfulRemoteDataObject$(createPaginatedList([])),
@@ -90,6 +97,7 @@ describe('FileSectionComponent', () => {
9097
{ provide: APP_CONFIG, useValue: environment },
9198
{ provide: ThemeService, useValue: getMockThemeService() },
9299
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub() },
100+
{ provide: LocaleService, useValue: mockLocaleService },
93101
provideMockStore(),
94102
],
95103
schemas: [NO_ERRORS_SCHEMA],
@@ -105,6 +113,8 @@ describe('FileSectionComponent', () => {
105113
}));
106114

107115
beforeEach(waitForAsync(() => {
116+
localeService = TestBed.inject(LocaleService);
117+
localeService.getCurrentLanguageCode.and.returnValue(of('en'));
108118
fixture = TestBed.createComponent(FileSectionComponent);
109119
comp = fixture.componentInstance;
110120
fixture.detectChanges();

src/app/process-page/detail/process-detail.component.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
2727
import { BitstreamDataService } from '../../core/data/bitstream-data.service';
2828
import { PaginatedList } from '../../core/data/paginated-list.model';
2929
import { ProcessDataService } from '../../core/data/processes/process-data.service';
30+
import { LocaleService } from '../../core/locale/locale.service';
3031
import { Bitstream } from '../../core/shared/bitstream.model';
3132
import { ThemedFileDownloadLinkComponent } from '../../shared/file-download-link/themed-file-download-link.component';
3233
import { ThemedLoadingComponent } from '../../shared/loading/themed-loading.component';
@@ -60,6 +61,12 @@ describe('ProcessDetailComponent', () => {
6061
let router: RouterStub;
6162
let modalService;
6263
let notificationsService: NotificationsServiceStub;
64+
let localeService: any;
65+
const languageList = ['en;q=1', 'de;q=0.8'];
66+
const mockLocaleService = jasmine.createSpyObj('LocaleService', {
67+
getCurrentLanguageCode: jasmine.createSpy('getCurrentLanguageCode'),
68+
getLanguageCodeList: of(languageList),
69+
});
6370

6471
let process: Process;
6572
let fileName: string;
@@ -169,6 +176,7 @@ describe('ProcessDetailComponent', () => {
169176
{ provide: NgbModal, useValue: modalService },
170177
{ provide: NotificationsService, useValue: notificationsService },
171178
{ provide: Router, useValue: router },
179+
{ provide: LocaleService, useValue: mockLocaleService },
172180
],
173181
schemas: [CUSTOM_ELEMENTS_SCHEMA],
174182
})
@@ -186,7 +194,10 @@ describe('ProcessDetailComponent', () => {
186194
beforeEach(() => {
187195
fixture = TestBed.createComponent(ProcessDetailComponent);
188196
component = fixture.componentInstance;
197+
localeService = TestBed.inject(LocaleService);
198+
localeService.getCurrentLanguageCode.and.returnValue(of('en'));
189199
});
200+
190201
afterEach(fakeAsync(() => {
191202
TestBed.resetTestingModule();
192203
fixture.destroy();

src/app/shared/utils/file-size-pipe.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {
33
PipeTransform,
44
} from '@angular/core';
55
import { filesize } from 'filesize';
6+
import { take } from 'rxjs/operators';
7+
8+
import { LocaleService } from '../../core/locale/locale.service';
69

710
/*
811
* Convert bytes into largest possible unit.
@@ -18,7 +21,20 @@ import { filesize } from 'filesize';
1821
name: 'dsFileSize',
1922
})
2023
export class FileSizePipe implements PipeTransform {
24+
25+
private currentLocale: string;
26+
27+
constructor(private localeService: LocaleService) {
28+
this.localeService.getCurrentLanguageCode().pipe(take(1)).subscribe(locale => {
29+
this.currentLocale = locale;
30+
});
31+
}
32+
2133
transform(bytes: number = 0, precision: number = 2): string {
22-
return filesize(bytes, { standard: 'jedec', round: precision });
34+
return filesize(bytes, {
35+
standard: 'jedec',
36+
round: precision,
37+
locale: this.currentLocale,
38+
});
2339
}
2440
}

src/app/submission/sections/upload/file/view/section-upload-file-view.component.spec.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
waitForAsync,
1111
} from '@angular/core/testing';
1212
import { TranslateModule } from '@ngx-translate/core';
13+
import { of } from 'rxjs';
1314

15+
import { LocaleService } from '../../../../../core/locale/locale.service';
1416
import { Metadata } from '../../../../../core/shared/metadata.utils';
1517
import { FormComponent } from '../../../../../shared/form/form.component';
1618
import { mockUploadFiles } from '../../../../../shared/mocks/submission.mock';
@@ -24,6 +26,12 @@ describe('SubmissionSectionUploadFileViewComponent test suite', () => {
2426
let comp: SubmissionSectionUploadFileViewComponent;
2527
let compAsAny: any;
2628
let fixture: ComponentFixture<SubmissionSectionUploadFileViewComponent>;
29+
let localeService: any;
30+
const languageList = ['en;q=1', 'de;q=0.8'];
31+
const mockLocaleService = jasmine.createSpyObj('LocaleService', {
32+
getCurrentLanguageCode: jasmine.createSpy('getCurrentLanguageCode'),
33+
getLanguageCodeList: of(languageList),
34+
});
2735

2836
const fileData: any = mockUploadFiles[0];
2937

@@ -38,6 +46,7 @@ describe('SubmissionSectionUploadFileViewComponent test suite', () => {
3846
],
3947
providers: [
4048
SubmissionSectionUploadFileViewComponent,
49+
{ provide: LocaleService, useValue: mockLocaleService },
4150
],
4251
schemas: [NO_ERRORS_SCHEMA],
4352
})
@@ -56,14 +65,15 @@ describe('SubmissionSectionUploadFileViewComponent test suite', () => {
5665
let testComp: TestComponent;
5766
let testFixture: ComponentFixture<TestComponent>;
5867

59-
// synchronous beforeEach
60-
beforeEach(() => {
68+
beforeEach(waitForAsync(async () => {
6169
const html = `
6270
<ds-submission-section-upload-file-view [fileData]="fileData"></ds-submission-section-upload-file-view>`;
6371

6472
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
73+
localeService = TestBed.inject(LocaleService);
74+
localeService.getCurrentLanguageCode.and.returnValue(of('en'));
6575
testComp = testFixture.componentInstance;
66-
});
76+
}));
6777

6878
afterEach(() => {
6979
testFixture.destroy();
@@ -77,11 +87,13 @@ describe('SubmissionSectionUploadFileViewComponent test suite', () => {
7787
});
7888

7989
describe('', () => {
80-
beforeEach(() => {
90+
beforeEach(waitForAsync(async () => {
91+
localeService = TestBed.inject(LocaleService);
92+
localeService.getCurrentLanguageCode.and.returnValue(of('en'));
8193
fixture = TestBed.createComponent(SubmissionSectionUploadFileViewComponent);
8294
comp = fixture.componentInstance;
8395
compAsAny = comp;
84-
});
96+
}));
8597

8698
afterEach(() => {
8799
fixture.destroy();

0 commit comments

Comments
 (0)