Skip to content

Commit

Permalink
Merged in dspace-cris-2023_02_x_DSC-758 (pull request DSpace#1508)
Browse files Browse the repository at this point in the history
Dspace cris 2023 02 x DSC-758

Approved-by: Giuseppe Digilio
  • Loading branch information
vins01-4science authored and atarix83 committed Mar 29, 2024
2 parents 5d5f439 + 04df967 commit 234b490
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 104 deletions.
242 changes: 139 additions & 103 deletions src/app/core/locale/locale.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AuthService } from '../auth/auth.service';
import { NativeWindowRef } from '../services/window.service';
import { RouteService } from '../services/route.service';
import { routeServiceStub } from '../../shared/testing/route-service.stub';
import { of as observableOf } from 'rxjs';

describe('LocaleService test suite', () => {
let service: LocaleService;
Expand All @@ -22,129 +23,164 @@ describe('LocaleService test suite', () => {
let authService;
let routeService;
let document;
let spyOnGetLanguage;


const translateServiceStub: any = {
getLangs: () => {
return langList;
},
getBrowserLang: () => {
return langList;
},
// eslint-disable-next-line @typescript-eslint/no-empty-function
use: (param: string) => {
}
};

authService = jasmine.createSpyObj('AuthService', {
isAuthenticated: jasmine.createSpy('isAuthenticated'),
isAuthenticationLoaded: jasmine.createSpy('isAuthenticationLoaded')
});

const langList = ['en', 'xx', 'de'];

beforeEach(waitForAsync(() => {
return TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslateLoaderMock
}
}),
],
providers: [
{ provide: CookieService, useValue: new CookieServiceMock() },
{ provide: AuthService, userValue: authService },
{ provide: RouteService, useValue: routeServiceStub },
{ provide: Document, useValue: document },
]
});
}));

beforeEach(() => {
cookieService = TestBed.inject(CookieService);
translateService = TestBed.inject(TranslateService);
routeService = TestBed.inject(RouteService);
window = new NativeWindowRef();
document = { documentElement: { lang: 'en' } };
service = new LocaleService(window, cookieService, translateService, authService, routeService, document);
serviceAsAny = service;
spyOnGet = spyOn(cookieService, 'get');
spyOnSet = spyOn(cookieService, 'set');
});
describe('with valid language', () => {

beforeEach(waitForAsync(() => {
return TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslateLoaderMock
}
}),
],
providers: [
{ provide: CookieService, useValue: new CookieServiceMock() },
{ provide: AuthService, userValue: authService },
{ provide: RouteService, useValue: routeServiceStub },
{ provide: TranslateService, useValue: translateServiceStub },
{ provide: Document, useValue: document },
]
});
}));

describe('getCurrentLanguageCode', () => {
beforeEach(() => {
spyOn(translateService, 'getLangs').and.returnValue(langList);
});

it('should return the language saved on cookie if it\'s a valid & active language', () => {
spyOnGet.and.returnValue('de');
expect(service.getCurrentLanguageCode()).toBe('de');
});

it('should return the default language if the cookie language is disabled', () => {
spyOnGet.and.returnValue('disabled');
expect(service.getCurrentLanguageCode()).toBe('en');
});

it('should return the default language if the cookie language does not exist', () => {
spyOnGet.and.returnValue('does-not-exist');
expect(service.getCurrentLanguageCode()).toBe('en');
});

it('should return language from browser setting', () => {
spyOn(translateService, 'getBrowserLang').and.returnValue('xx');
expect(service.getCurrentLanguageCode()).toBe('xx');
cookieService = TestBed.inject(CookieService);
translateService = TestBed.inject(TranslateService);
routeService = TestBed.inject(RouteService);
window = new NativeWindowRef();
document = { documentElement: { lang: 'en' } };
service = new LocaleService(window, cookieService, translateService, authService, routeService, document);
serviceAsAny = service;
spyOnGet = spyOn(cookieService, 'get');
spyOnSet = spyOn(cookieService, 'set');
spyOnGetLanguage = spyOn(routeService, 'getQueryParameterValue').withArgs('lang');
});

it('should return default language from config', () => {
spyOn(translateService, 'getBrowserLang').and.returnValue('fr');
expect(service.getCurrentLanguageCode()).toBe('en');
});
});

describe('getLanguageCodeFromCookie', () => {
it('should return language from cookie', () => {
spyOnGet.and.returnValue('de');
expect(service.getLanguageCodeFromCookie()).toBe('de');
describe('getCurrentLanguageCode', () => {
it('should return language saved on cookie', () => {
spyOnGet.and.returnValue('de');
expect(service.getCurrentLanguageCode()).toBe('de');
});

describe('', () => {
beforeEach(() => {
spyOn(translateService, 'getLangs').and.returnValue(langList);
});

it('should return language from browser setting', () => {
spyOn(translateService, 'getBrowserLang').and.returnValue('xx');
expect(service.getCurrentLanguageCode()).toBe('xx');
});

it('should return default language from config', () => {
spyOn(translateService, 'getBrowserLang').and.returnValue('fr');
expect(service.getCurrentLanguageCode()).toBe('en');
});
});
});

});
describe('getLanguageCodeFromCookie', () => {
it('should return language from cookie', () => {
spyOnGet.and.returnValue('de');
expect(service.getLanguageCodeFromCookie()).toBe('de');
});

describe('saveLanguageCodeToCookie', () => {
it('should save language to cookie', () => {
service.saveLanguageCodeToCookie('en');
expect(spyOnSet).toHaveBeenCalledWith(LANG_COOKIE, 'en');
});
});

describe('setCurrentLanguageCode', () => {
beforeEach(() => {
spyOn(service, 'saveLanguageCodeToCookie');
spyOn(translateService, 'use');
describe('saveLanguageCodeToCookie', () => {
it('should save language to cookie', () => {
service.saveLanguageCodeToCookie('en');
expect(spyOnSet).toHaveBeenCalledWith(LANG_COOKIE, 'en');
});
});

it('should set the given language', () => {
service.setCurrentLanguageCode('xx');
expect(translateService.use).toHaveBeenCalledWith('xx');
expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('xx');
});

it('should set the current language', () => {
spyOn(service, 'getCurrentLanguageCode').and.returnValue('es');
service.setCurrentLanguageCode();
expect(translateService.use).toHaveBeenCalledWith('es');
expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('es');
});

it('should set the current language on the html tag', () => {
spyOn(service, 'getCurrentLanguageCode').and.returnValue('es');
service.setCurrentLanguageCode();
expect((service as any).document.documentElement.lang).toEqual('es');
});
});

describe('', () => {
it('should set quality to current language list', () => {
const langListWithQuality = ['en;q=1', 'xx;q=0.9', 'de;q=0.8'];
spyOn(service, 'setQuality').and.returnValue(langListWithQuality);
service.setQuality(langList, LANG_ORIGIN.BROWSER, false);
expect(service.setQuality).toHaveBeenCalledWith(langList, LANG_ORIGIN.BROWSER, false);
describe('setCurrentLanguageCode', () => {
beforeEach(() => {
spyOn(service, 'saveLanguageCodeToCookie');
spyOn(translateService, 'use');
});

it('should set the given language', () => {
service.setCurrentLanguageCode('it');
expect(translateService.use).toHaveBeenCalledWith('it');
expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('it');
});

it('should set the current language', () => {
spyOn(service, 'getCurrentLanguageCode').and.returnValue('es');
service.setCurrentLanguageCode();
expect(translateService.use).toHaveBeenCalledWith('es');
expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('es');
});

it('should set the current language on the html tag', () => {
spyOn(service, 'getCurrentLanguageCode').and.returnValue('es');
service.setCurrentLanguageCode();
expect((service as any).document.documentElement.lang).toEqual('es');
});

describe('should set language on init', () => {
beforeEach(() => {
spyOn(translateService, 'getLangs').and.returnValue(langList);
spyOn(service, 'setCurrentLanguageCode');
});
describe('whith correct lang query param ', () => {
beforeEach(() => {
spyOnGetLanguage.and.returnValue(observableOf('en'));
service.initDefaults();
});
it('should set correct lang', () => {
expect(service.setCurrentLanguageCode).toHaveBeenCalledWith('en');
});
});
describe('whith wrong lang query param ', () => {
beforeEach(() => {
spyOnGetLanguage.and.returnValue(observableOf('abcd'));
service.initDefaults();
});
it('should not set lang', () => {
expect(service.setCurrentLanguageCode).not.toHaveBeenCalled();
});
});
});
});

it('should return the list of language with quality factor', () => {
spyOn(service, 'getLanguageCodeList');
service.getLanguageCodeList();
expect(service.getLanguageCodeList).toHaveBeenCalled();
describe('', () => {
it('should set quality to current language list', () => {
const langListWithQuality = ['en;q=1', 'it;q=0.9', 'de;q=0.8'];
spyOn(service, 'setQuality').and.returnValue(langListWithQuality);
service.setQuality(langList, LANG_ORIGIN.BROWSER, false);
expect(service.setQuality).toHaveBeenCalledWith(langList, LANG_ORIGIN.BROWSER, false);
});

it('should return the list of language with quality factor', () => {
spyOn(service, 'getLanguageCodeList');
service.getLanguageCodeList();
expect(service.getLanguageCodeList).toHaveBeenCalled();
});
});
});
});
12 changes: 12 additions & 0 deletions src/app/core/locale/locale.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ export class LocaleService {
protected routeService: RouteService,
@Inject(DOCUMENT) protected document: any
) {
this.initDefaults();
}

/**
* Initialize the language from query params
*/
initDefaults() {
this.routeService.getQueryParameterValue('lang').subscribe(lang => {
if (lang && this.translate.getLangs().includes(lang)) {
this.setCurrentLanguageCode(lang);
}
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/testing/route-service.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const routeServiceStub: any = {
getQueryParamMap: () => {
return observableOf(new Map());
},
getQueryParameterValue: () => {
getQueryParameterValue: (lang?: string) => {
return observableOf({});
},
getRouteParameterValue: (param) => {
Expand Down

0 comments on commit 234b490

Please sign in to comment.