Skip to content

Commit d6af4ad

Browse files
committed
enhance FileSizePipe to support localization
1 parent aadea29 commit d6af4ad

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-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
@@ -8,6 +8,7 @@ import { By } from '@angular/platform-browser';
88
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
99
import { APP_CONFIG } from '@dspace/config/app-config.interface';
1010
import { BitstreamDataService } from '@dspace/core/data/bitstream-data.service';
11+
import { LocaleService } from '@dspace/core/locale/locale.service';
1112
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
1213
import { PaginationService } from '@dspace/core/pagination/pagination.service';
1314
import { Bitstream } from '@dspace/core/shared/bitstream.model';
@@ -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
@@ -10,6 +10,7 @@ import { ActivatedRoute } from '@angular/router';
1010
import { APP_CONFIG } from '@dspace/config/app-config.interface';
1111
import { BitstreamDataService } from '@dspace/core/data/bitstream-data.service';
1212
import { APP_DATA_SERVICES_MAP } from '@dspace/core/data-services-map-type';
13+
import { LocaleService } from '@dspace/core/locale/locale.service';
1314
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
1415
import { Bitstream } from '@dspace/core/shared/bitstream.model';
1516
import { PageInfo } from '@dspace/core/shared/page-info.model';
@@ -38,6 +39,12 @@ import { FileSectionComponent } from './file-section.component';
3839
describe('FileSectionComponent', () => {
3940
let comp: FileSectionComponent;
4041
let fixture: ComponentFixture<FileSectionComponent>;
42+
let localeService: any;
43+
const languageList = ['en;q=1', 'de;q=0.8'];
44+
const mockLocaleService = jasmine.createSpyObj('LocaleService', {
45+
getCurrentLanguageCode: jasmine.createSpy('getCurrentLanguageCode'),
46+
getLanguageCodeList: of(languageList),
47+
});
4148

4249
const bitstreamDataService = jasmine.createSpyObj('bitstreamDataService', {
4350
findAllByItemAndBundleName: createSuccessfulRemoteDataObject$(createPaginatedList([])),
@@ -88,6 +95,7 @@ describe('FileSectionComponent', () => {
8895
{ provide: APP_CONFIG, useValue: environment },
8996
{ provide: ThemeService, useValue: getMockThemeService() },
9097
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub() },
98+
{ provide: LocaleService, useValue: mockLocaleService },
9199
provideMockStore(),
92100
],
93101
schemas: [NO_ERRORS_SCHEMA],
@@ -103,6 +111,8 @@ describe('FileSectionComponent', () => {
103111
}));
104112

105113
beforeEach(waitForAsync(() => {
114+
localeService = TestBed.inject(LocaleService);
115+
localeService.getCurrentLanguageCode.and.returnValue(of('en'));
106116
fixture = TestBed.createComponent(FileSectionComponent);
107117
comp = fixture.componentInstance;
108118
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
@@ -23,6 +23,7 @@ import { DSONameService } from '@dspace/core/breadcrumbs/dso-name.service';
2323
import { BitstreamDataService } from '@dspace/core/data/bitstream-data.service';
2424
import { PaginatedList } from '@dspace/core/data/paginated-list.model';
2525
import { ProcessDataService } from '@dspace/core/data/processes/process-data.service';
26+
import { LocaleService } from '@dspace/core/locale/locale.service';
2627
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
2728
import { Process } from '@dspace/core/processes/process.model';
2829
import { Bitstream } from '@dspace/core/shared/bitstream.model';
@@ -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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import {
22
Pipe,
33
PipeTransform,
44
} from '@angular/core';
5+
import { LocaleService } from '@dspace/core/locale/locale.service';
56
import { filesize } from 'filesize';
7+
import { take } from 'rxjs/operators';
68

79
/*
810
* Convert bytes into largest possible unit.
@@ -18,7 +20,20 @@ import { filesize } from 'filesize';
1820
name: 'dsFileSize',
1921
})
2022
export class FileSizePipe implements PipeTransform {
23+
24+
private currentLocale: string;
25+
26+
constructor(private localeService: LocaleService) {
27+
this.localeService.getCurrentLanguageCode().pipe(take(1)).subscribe(locale => {
28+
this.currentLocale = locale;
29+
});
30+
}
31+
2132
transform(bytes: number = 0, precision: number = 2): string {
22-
return filesize(bytes, { standard: 'jedec', round: precision });
33+
return filesize(bytes, {
34+
standard: 'jedec',
35+
round: precision,
36+
locale: this.currentLocale,
37+
});
2338
}
2439
}

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
@@ -9,9 +9,11 @@ import {
99
TestBed,
1010
waitForAsync,
1111
} from '@angular/core/testing';
12+
import { LocaleService } from '@dspace/core/locale/locale.service';
1213
import { Metadata } from '@dspace/core/shared/metadata.utils';
1314
import { createTestComponent } from '@dspace/core/testing/utils.test';
1415
import { TranslateModule } from '@ngx-translate/core';
16+
import { of } from 'rxjs';
1517

1618
import { FormComponent } from '../../../../../shared/form/form.component';
1719
import { TruncatePipe } from '../../../../../shared/utils/truncate.pipe';
@@ -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)