From 171c78b4d2a9c4d4426aaab51c0dbd7812e6a200 Mon Sep 17 00:00:00 2001 From: Sufiyan Shaikh Date: Tue, 29 Nov 2022 15:11:33 +0100 Subject: [PATCH 1/4] [DSC-758] Need a parameter on Angular Side to force Language via URL --- src/app/core/locale/locale.service.spec.ts | 86 ++++++++++++++------ src/app/core/locale/locale.service.ts | 12 +++ src/app/shared/testing/route-service.stub.ts | 2 +- 3 files changed, 72 insertions(+), 28 deletions(-) diff --git a/src/app/core/locale/locale.service.spec.ts b/src/app/core/locale/locale.service.spec.ts index 39356fdf970..4384d08d91c 100644 --- a/src/app/core/locale/locale.service.spec.ts +++ b/src/app/core/locale/locale.service.spec.ts @@ -10,8 +10,9 @@ 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', () => { +fdescribe('LocaleService test suite', () => { let service: LocaleService; let serviceAsAny: any; let cookieService: CookieService; @@ -22,13 +23,25 @@ describe('LocaleService test suite', () => { let authService; let routeService; let document; + let spyOnGetLanguage; authService = jasmine.createSpyObj('AuthService', { isAuthenticated: jasmine.createSpy('isAuthenticated'), isAuthenticationLoaded: jasmine.createSpy('isAuthenticationLoaded') }); - const langList = ['en', 'xx', 'de']; + const translateServiceStub: any = { + getLangs: () => { + return langList; + }, + getBrowserLang: () => { + return langList; + }, + // eslint-disable-next-line @typescript-eslint/no-empty-function + use: (param: string) => {} + }; + + const langList = ['en', 'it', 'de']; beforeEach(waitForAsync(() => { return TestBed.configureTestingModule({ @@ -44,6 +57,7 @@ describe('LocaleService test suite', () => { { provide: CookieService, useValue: new CookieServiceMock() }, { provide: AuthService, userValue: authService }, { provide: RouteService, useValue: routeServiceStub }, + { provide: TranslateService, useValue: translateServiceStub }, { provide: Document, useValue: document }, ] }); @@ -59,36 +73,29 @@ describe('LocaleService test suite', () => { serviceAsAny = service; spyOnGet = spyOn(cookieService, 'get'); spyOnSet = spyOn(cookieService, 'set'); + spyOnGetLanguage = spyOn(routeService, 'getQueryParameterValue').withArgs('lang'); }); describe('getCurrentLanguageCode', () => { - beforeEach(() => { - spyOn(translateService, 'getLangs').and.returnValue(langList); - }); - - it('should return the language saved on cookie if it\'s a valid & active language', () => { + it('should return language saved on cookie', () => { 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'); - }); + 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 language from browser setting', () => { + spyOn(translateService, 'getBrowserLang').and.returnValue('it'); + expect(service.getCurrentLanguageCode()).toBe('it'); + }); - it('should return default language from config', () => { - spyOn(translateService, 'getBrowserLang').and.returnValue('fr'); - expect(service.getCurrentLanguageCode()).toBe('en'); + it('should return default language from config', () => { + spyOn(translateService, 'getBrowserLang').and.returnValue('fr'); + expect(service.getCurrentLanguageCode()).toBe('en'); + }); }); }); @@ -114,9 +121,9 @@ describe('LocaleService test suite', () => { }); it('should set the given language', () => { - service.setCurrentLanguageCode('xx'); - expect(translateService.use).toHaveBeenCalledWith('xx'); - expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('xx'); + service.setCurrentLanguageCode('it'); + expect(translateService.use).toHaveBeenCalledWith('it'); + expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('it'); }); it('should set the current language', () => { @@ -131,11 +138,36 @@ describe('LocaleService test suite', () => { 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(); + }); + }); + }); }); describe('', () => { it('should set quality to current language list', () => { - const langListWithQuality = ['en;q=1', 'xx;q=0.9', 'de;q=0.8']; + 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); diff --git a/src/app/core/locale/locale.service.ts b/src/app/core/locale/locale.service.ts index 5c080d8c16c..2cb94314256 100644 --- a/src/app/core/locale/locale.service.ts +++ b/src/app/core/locale/locale.service.ts @@ -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().some(language => language === lang)) { + this.setCurrentLanguageCode(lang); + } + }); } /** diff --git a/src/app/shared/testing/route-service.stub.ts b/src/app/shared/testing/route-service.stub.ts index 8384c3efbcc..a31a058dd12 100644 --- a/src/app/shared/testing/route-service.stub.ts +++ b/src/app/shared/testing/route-service.stub.ts @@ -23,7 +23,7 @@ export const routeServiceStub: any = { getQueryParamMap: () => { return observableOf(new Map()); }, - getQueryParameterValue: () => { + getQueryParameterValue: (lang?: string) => { return observableOf({}); }, getRouteParameterValue: (param) => { From 8f1627cf4226db429d4a36a59c2541cfb4cdfb31 Mon Sep 17 00:00:00 2001 From: Andrea Barbasso <´andrea.barbasso@4science.com´> Date: Tue, 12 Dec 2023 15:42:38 +0100 Subject: [PATCH 2/4] [DSC-758] fix broken tests --- src/app/core/locale/locale.service.spec.ts | 232 +++++++++++---------- 1 file changed, 118 insertions(+), 114 deletions(-) diff --git a/src/app/core/locale/locale.service.spec.ts b/src/app/core/locale/locale.service.spec.ts index 4384d08d91c..6195774a51d 100644 --- a/src/app/core/locale/locale.service.spec.ts +++ b/src/app/core/locale/locale.service.spec.ts @@ -25,10 +25,6 @@ fdescribe('LocaleService test suite', () => { let document; let spyOnGetLanguage; - authService = jasmine.createSpyObj('AuthService', { - isAuthenticated: jasmine.createSpy('isAuthenticated'), - isAuthenticationLoaded: jasmine.createSpy('isAuthenticationLoaded') - }); const translateServiceStub: any = { getLangs: () => { @@ -38,145 +34,153 @@ fdescribe('LocaleService test suite', () => { return langList; }, // eslint-disable-next-line @typescript-eslint/no-empty-function - use: (param: string) => {} + use: (param: string) => { + } }; - const langList = ['en', 'it', '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: TranslateService, useValue: translateServiceStub }, - { 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'); - spyOnGetLanguage = spyOn(routeService, 'getQueryParameterValue').withArgs('lang'); + authService = jasmine.createSpyObj('AuthService', { + isAuthenticated: jasmine.createSpy('isAuthenticated'), + isAuthenticationLoaded: jasmine.createSpy('isAuthenticationLoaded') }); + const langList = ['en', 'xx', 'de']; + + 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', () => { - it('should return language saved on cookie', () => { - spyOnGet.and.returnValue('de'); - expect(service.getCurrentLanguageCode()).toBe('de'); + 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'); + spyOnGetLanguage = spyOn(routeService, 'getQueryParameterValue').withArgs('lang'); }); - describe('', () => { - beforeEach(() => { - spyOn(translateService, 'getLangs').and.returnValue(langList); + describe('getCurrentLanguageCode', () => { + it('should return language saved on cookie', () => { + spyOnGet.and.returnValue('de'); + expect(service.getCurrentLanguageCode()).toBe('de'); }); - it('should return language from browser setting', () => { - spyOn(translateService, 'getBrowserLang').and.returnValue('it'); - expect(service.getCurrentLanguageCode()).toBe('it'); - }); + describe('', () => { + beforeEach(() => { + spyOn(translateService, 'getLangs').and.returnValue(langList); + }); - it('should return default language from config', () => { - spyOn(translateService, 'getBrowserLang').and.returnValue('fr'); - expect(service.getCurrentLanguageCode()).toBe('en'); - }); - }); - }); + it('should return language from browser setting', () => { + spyOn(translateService, 'getBrowserLang').and.returnValue('xx'); + expect(service.getCurrentLanguageCode()).toBe('xx'); + }); - describe('getLanguageCodeFromCookie', () => { - it('should return language from cookie', () => { - spyOnGet.and.returnValue('de'); - expect(service.getLanguageCodeFromCookie()).toBe('de'); + 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('it'); - expect(translateService.use).toHaveBeenCalledWith('it'); - expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('it'); - }); + describe('setCurrentLanguageCode', () => { + beforeEach(() => { + spyOn(service, 'saveLanguageCodeToCookie'); + spyOn(translateService, 'use'); + }); - 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 given language', () => { + service.setCurrentLanguageCode('it'); + expect(translateService.use).toHaveBeenCalledWith('it'); + expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('it'); + }); - 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'); - }); + it('should set the current language', () => { + spyOn(service, 'getCurrentLanguageCode').and.returnValue('es'); + service.setCurrentLanguageCode(); + expect(translateService.use).toHaveBeenCalledWith('es'); + expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('es'); + }); - describe('should set language on init', () => { - beforeEach(() => { - spyOn(translateService, 'getLangs').and.returnValue(langList); - spyOn(service, 'setCurrentLanguageCode'); + 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('whith correct lang query param ', () => { + + describe('should set language on init', () => { beforeEach(() => { - spyOnGetLanguage.and.returnValue(observableOf('en')); - service.initDefaults(); - }); - it('should set correct lang', () => { - expect(service.setCurrentLanguageCode).toHaveBeenCalledWith('en'); + spyOn(translateService, 'getLangs').and.returnValue(langList); + spyOn(service, 'setCurrentLanguageCode'); }); - }); - describe('whith wrong lang query param ', () => { - beforeEach(() => { - spyOnGetLanguage.and.returnValue(observableOf('abcd')); - service.initDefaults(); + describe('whith correct lang query param ', () => { + beforeEach(() => { + spyOnGetLanguage.and.returnValue(observableOf('en')); + service.initDefaults(); + }); + it('should set correct lang', () => { + expect(service.setCurrentLanguageCode).toHaveBeenCalledWith('en'); + }); }); - it('should not set lang', () => { - expect(service.setCurrentLanguageCode).not.toHaveBeenCalled(); + describe('whith wrong lang query param ', () => { + beforeEach(() => { + spyOnGetLanguage.and.returnValue(observableOf('abcd')); + service.initDefaults(); + }); + it('should not set lang', () => { + expect(service.setCurrentLanguageCode).not.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); - }); + 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(); + it('should return the list of language with quality factor', () => { + spyOn(service, 'getLanguageCodeList'); + service.getLanguageCodeList(); + expect(service.getLanguageCodeList).toHaveBeenCalled(); + }); }); }); }); From 32ead6af7d3ec1f25eb66bf883110b0c7fcad248 Mon Sep 17 00:00:00 2001 From: Andrea Barbasso <´andrea.barbasso@4science.com´> Date: Tue, 12 Dec 2023 15:51:47 +0100 Subject: [PATCH 3/4] [DSC-758] refactor language detection in locale service --- src/app/core/locale/locale.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/core/locale/locale.service.ts b/src/app/core/locale/locale.service.ts index 2cb94314256..f37803fa3c7 100644 --- a/src/app/core/locale/locale.service.ts +++ b/src/app/core/locale/locale.service.ts @@ -51,7 +51,7 @@ export class LocaleService { */ initDefaults() { this.routeService.getQueryParameterValue('lang').subscribe(lang => { - if (lang && this.translate.getLangs().some(language => language === lang)) { + if (lang && this.translate.getLangs().includes(lang)) { this.setCurrentLanguageCode(lang); } }); From 8f3f390859a1114dfc4983a4684a421f20487e7f Mon Sep 17 00:00:00 2001 From: Vincenzo Mecca Date: Wed, 27 Mar 2024 15:03:53 +0100 Subject: [PATCH 4/4] [DSC-758] fix broken tests --- src/app/core/locale/locale.service.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/core/locale/locale.service.spec.ts b/src/app/core/locale/locale.service.spec.ts index 6195774a51d..2db176c5d16 100644 --- a/src/app/core/locale/locale.service.spec.ts +++ b/src/app/core/locale/locale.service.spec.ts @@ -12,7 +12,7 @@ import { RouteService } from '../services/route.service'; import { routeServiceStub } from '../../shared/testing/route-service.stub'; import { of as observableOf } from 'rxjs'; -fdescribe('LocaleService test suite', () => { +describe('LocaleService test suite', () => { let service: LocaleService; let serviceAsAny: any; let cookieService: CookieService;