From 9da5da70258b20408bb208148356c8998237604a Mon Sep 17 00:00:00 2001 From: Fran Palmero Date: Fri, 15 Nov 2024 16:44:09 +0100 Subject: [PATCH] Fix ESLint errors --- .../desktop-nav/desktop-nav.component.spec.ts | 74 +++++++-------- .../desktop-nav/desktop-nav.component.ts | 20 ++--- .../mobile-nav/mobile-nav.component.spec.ts | 74 +++++++-------- .../header/mobile-nav/mobile-nav.component.ts | 20 ++--- src/app/services/nav.service.spec.ts | 90 ++++++++++--------- src/app/services/nav.service.ts | 32 +++---- 6 files changed, 157 insertions(+), 153 deletions(-) diff --git a/src/app/core/layout/header/desktop-nav/desktop-nav.component.spec.ts b/src/app/core/layout/header/desktop-nav/desktop-nav.component.spec.ts index cc93f237..0c5eb4f3 100644 --- a/src/app/core/layout/header/desktop-nav/desktop-nav.component.spec.ts +++ b/src/app/core/layout/header/desktop-nav/desktop-nav.component.spec.ts @@ -1,17 +1,17 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { DesktopNavComponent } from './desktop-nav.component'; -import { NavService } from 'src/app/services/nav.service'; -import { TranslateModule } from '@ngx-translate/core'; -import { RouterModule, ActivatedRoute } from '@angular/router'; +import { type ComponentFixture, TestBed } from '@angular/core/testing' +import { DesktopNavComponent } from './desktop-nav.component' +import { NavService } from 'src/app/services/nav.service' +import { TranslateModule } from '@ngx-translate/core' +import { RouterModule, ActivatedRoute } from '@angular/router' // Mock de NavService class MockNavService { - public selectWidth = '69px'; + public selectWidth = '69px' - openLoginModal = jest.fn(); // Simula la función openLoginModal + openLoginModal = jest.fn() // Simula la función openLoginModal changeLanguage = jest.fn((language: string) => { - this.selectWidth = language === 'ca' ? '69px' : '57px'; - }); + this.selectWidth = language === 'ca' ? '69px' : '57px' + }) } // Mock de ActivatedRoute para pruebas @@ -21,12 +21,12 @@ const mockActivatedRoute = { get: jest.fn().mockReturnValue(null) } } -}; +} describe('DesktopNavComponent', () => { - let component: DesktopNavComponent; - let fixture: ComponentFixture; - let navService: MockNavService; + let component: DesktopNavComponent + let fixture: ComponentFixture + let navService: MockNavService beforeEach(() => { TestBed.configureTestingModule({ @@ -36,40 +36,40 @@ describe('DesktopNavComponent', () => { { provide: NavService, useClass: MockNavService }, { provide: ActivatedRoute, useValue: mockActivatedRoute } ] - }); + }) - fixture = TestBed.createComponent(DesktopNavComponent); - component = fixture.componentInstance; - navService = TestBed.inject(NavService) as unknown as MockNavService; - fixture.detectChanges(); - }); + fixture = TestBed.createComponent(DesktopNavComponent) + component = fixture.componentInstance + navService = TestBed.inject(NavService) as unknown as MockNavService + fixture.detectChanges() + }) it('should create', () => { - expect(component).toBeTruthy(); - }); + expect(component).toBeTruthy() + }) it('should call openLoginModal when openLoginModal is invoked', () => { - component.openLoginModal(); - expect(navService.openLoginModal).toHaveBeenCalled(); - }); + component.openLoginModal() + expect(navService.openLoginModal).toHaveBeenCalled() + }) it('should change language and update selectWidth', () => { - const event = new Event('change'); - Object.defineProperty(event, 'target', { value: { value: 'es' }, enumerable: true }); + const event = new Event('change') + Object.defineProperty(event, 'target', { value: { value: 'es' }, enumerable: true }) - component.changeLanguage(event); + component.changeLanguage(event) - expect(navService.changeLanguage).toHaveBeenCalledWith('es'); - expect(navService.selectWidth).toBe('57px'); // Verifica que el selectWidth se actualiza correctamente - }); + expect(navService.changeLanguage).toHaveBeenCalledWith('es') + expect(navService.selectWidth).toBe('57px') // Verifica que el selectWidth se actualiza correctamente + }) it('should change language to "ca" and update selectWidth accordingly', () => { - const event = new Event('change'); - Object.defineProperty(event, 'target', { value: { value: 'ca' }, enumerable: true }); + const event = new Event('change') + Object.defineProperty(event, 'target', { value: { value: 'ca' }, enumerable: true }) - component.changeLanguage(event); + component.changeLanguage(event) - expect(navService.changeLanguage).toHaveBeenCalledWith('ca'); - expect(navService.selectWidth).toBe('69px'); // Verifica que el selectWidth se actualiza correctamente - }); -}); + expect(navService.changeLanguage).toHaveBeenCalledWith('ca') + expect(navService.selectWidth).toBe('69px') // Verifica que el selectWidth se actualiza correctamente + }) +}) diff --git a/src/app/core/layout/header/desktop-nav/desktop-nav.component.ts b/src/app/core/layout/header/desktop-nav/desktop-nav.component.ts index 247783f3..b5730e13 100644 --- a/src/app/core/layout/header/desktop-nav/desktop-nav.component.ts +++ b/src/app/core/layout/header/desktop-nav/desktop-nav.component.ts @@ -1,5 +1,5 @@ -import { Component } from '@angular/core'; -import { NavService } from 'src/app/services/nav.service'; +import { Component, Inject } from '@angular/core' +import { NavService } from 'src/app/services/nav.service' @Component({ selector: 'app-desktop-nav', @@ -7,15 +7,15 @@ import { NavService } from 'src/app/services/nav.service'; styleUrl: './desktop-nav.component.scss' }) export class DesktopNavComponent { - constructor(public navService: NavService) {} + constructor (@Inject(NavService) public navService: NavService) {} - openLoginModal(): void { - this.navService.openLoginModal(); + openLoginModal (): void { + this.navService.openLoginModal() } - changeLanguage(event: Event): void { - const selectElement = event.target as HTMLSelectElement; - const language = selectElement.value; - this.navService.changeLanguage(language); + changeLanguage (event: Event): void { + const selectElement = event.target as HTMLSelectElement + const language = selectElement.value + this.navService.changeLanguage(language) } -} \ No newline at end of file +} diff --git a/src/app/core/layout/header/mobile-nav/mobile-nav.component.spec.ts b/src/app/core/layout/header/mobile-nav/mobile-nav.component.spec.ts index 2dd1e7bc..aed653cd 100755 --- a/src/app/core/layout/header/mobile-nav/mobile-nav.component.spec.ts +++ b/src/app/core/layout/header/mobile-nav/mobile-nav.component.spec.ts @@ -1,17 +1,17 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { MobileNavComponent } from './mobile-nav.component'; -import { NavService } from 'src/app/services/nav.service'; -import { TranslateModule } from '@ngx-translate/core'; -import { ActivatedRoute, RouterModule } from '@angular/router'; +import { type ComponentFixture, TestBed } from '@angular/core/testing' +import { MobileNavComponent } from './mobile-nav.component' +import { NavService } from 'src/app/services/nav.service' +import { TranslateModule } from '@ngx-translate/core' +import { ActivatedRoute, RouterModule } from '@angular/router' // Mock de NavService class MockNavService { - public selectWidth = '69px'; + public selectWidth = '69px' - openLoginModal = jest.fn(); // Simula la función openLoginModal + openLoginModal = jest.fn() // Simula la función openLoginModal changeLanguage = jest.fn((language: string) => { - this.selectWidth = language === 'ca' ? '69px' : '57px'; - }); + this.selectWidth = language === 'ca' ? '69px' : '57px' + }) } // Mock de ActivatedRoute para pruebas @@ -21,12 +21,12 @@ const mockActivatedRoute = { get: jest.fn().mockReturnValue(null) } } -}; +} describe('MobileNavComponent', () => { - let component: MobileNavComponent; - let fixture: ComponentFixture; - let navService: MockNavService; + let component: MobileNavComponent + let fixture: ComponentFixture + let navService: MockNavService beforeEach(() => { TestBed.configureTestingModule({ @@ -36,40 +36,40 @@ describe('MobileNavComponent', () => { { provide: NavService, useClass: MockNavService }, { provide: ActivatedRoute, useValue: mockActivatedRoute } ] - }); + }) - fixture = TestBed.createComponent(MobileNavComponent); - component = fixture.componentInstance; - navService = TestBed.inject(NavService) as unknown as MockNavService; - fixture.detectChanges(); - }); + fixture = TestBed.createComponent(MobileNavComponent) + component = fixture.componentInstance + navService = TestBed.inject(NavService) as unknown as MockNavService + fixture.detectChanges() + }) it('should create', () => { - expect(component).toBeTruthy(); - }); + expect(component).toBeTruthy() + }) it('should call openLoginModal when openLoginModal is invoked', () => { - component.openLoginModal(); - expect(navService.openLoginModal).toHaveBeenCalled(); - }); + component.openLoginModal() + expect(navService.openLoginModal).toHaveBeenCalled() + }) it('should change language and update selectWidth', () => { - const event = new Event('change'); - Object.defineProperty(event, 'target', { value: { value: 'es' }, enumerable: true }); + const event = new Event('change') + Object.defineProperty(event, 'target', { value: { value: 'es' }, enumerable: true }) - component.changeLanguage(event); + component.changeLanguage(event) - expect(navService.changeLanguage).toHaveBeenCalledWith('es'); - expect(navService.selectWidth).toBe('57px'); // Verifica que el selectWidth se actualiza correctamente - }); + expect(navService.changeLanguage).toHaveBeenCalledWith('es') + expect(navService.selectWidth).toBe('57px') // Verifica que el selectWidth se actualiza correctamente + }) it('should change language to "ca" and update selectWidth accordingly', () => { - const event = new Event('change'); - Object.defineProperty(event, 'target', { value: { value: 'ca' }, enumerable: true }); + const event = new Event('change') + Object.defineProperty(event, 'target', { value: { value: 'ca' }, enumerable: true }) - component.changeLanguage(event); + component.changeLanguage(event) - expect(navService.changeLanguage).toHaveBeenCalledWith('ca'); - expect(navService.selectWidth).toBe('69px'); // Verifica que el selectWidth se actualiza correctamente - }); -}); + expect(navService.changeLanguage).toHaveBeenCalledWith('ca') + expect(navService.selectWidth).toBe('69px') // Verifica que el selectWidth se actualiza correctamente + }) +}) diff --git a/src/app/core/layout/header/mobile-nav/mobile-nav.component.ts b/src/app/core/layout/header/mobile-nav/mobile-nav.component.ts index 10843f23..8a45e115 100755 --- a/src/app/core/layout/header/mobile-nav/mobile-nav.component.ts +++ b/src/app/core/layout/header/mobile-nav/mobile-nav.component.ts @@ -1,5 +1,5 @@ -import { Component } from '@angular/core'; -import { NavService } from 'src/app/services/nav.service'; +import { Component, Inject } from '@angular/core' +import { NavService } from 'src/app/services/nav.service' @Component({ selector: 'app-mobile-nav', @@ -7,15 +7,15 @@ import { NavService } from 'src/app/services/nav.service'; styleUrl: './mobile-nav.component.scss' }) export class MobileNavComponent { - constructor(public navService: NavService) {} + constructor (@Inject(NavService) public navService: NavService) {} - openLoginModal(): void { - this.navService.openLoginModal(); + openLoginModal (): void { + this.navService.openLoginModal() } - changeLanguage(event: Event): void { - const selectElement = event.target as HTMLSelectElement; - const language = selectElement.value; - this.navService.changeLanguage(language); + changeLanguage (event: Event): void { + const selectElement = event.target as HTMLSelectElement + const language = selectElement.value + this.navService.changeLanguage(language) } -} \ No newline at end of file +} diff --git a/src/app/services/nav.service.spec.ts b/src/app/services/nav.service.spec.ts index f441bdd5..4ffe9b2f 100644 --- a/src/app/services/nav.service.spec.ts +++ b/src/app/services/nav.service.spec.ts @@ -1,43 +1,45 @@ -import { TestBed } from '@angular/core/testing'; -import { NavService } from './nav.service'; -import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; -import { TranslateService } from '@ngx-translate/core'; +import { TestBed } from '@angular/core/testing' +import { NavService } from './nav.service' +import { NgbModal } from '@ng-bootstrap/ng-bootstrap' +import { TranslateService } from '@ngx-translate/core' // Mock de NgbModal class MockNgbModal { - open() { return { result: Promise.resolve('closed') }; } // Simula el método `open` + open (): { result: Promise } { + return { result: Promise.resolve('closed') } // Simula el método `open` + } } // Mock de TranslateService class MockTranslateService { - private currentLang = 'ca'; - private langs: string[] = []; + private currentLang = 'ca' + private langs: string[] = [] - addLangs(langs: string[]) { - this.langs = langs; + addLangs (langs: string[]): void { + this.langs = langs } - - setDefaultLang(lang: string) { - this.currentLang = lang; + + setDefaultLang (lang: string): void { + this.currentLang = lang } - - use(lang: string) { - this.currentLang = lang; + + use (lang: string): void { + this.currentLang = lang } - getLangs() { - return this.langs; + getLangs (): string[] { + return this.langs } - getCurrentLang() { - return this.currentLang; + getCurrentLang (): string { + return this.currentLang } } describe('NavService', () => { - let service: NavService; - let modalService: NgbModal; - let translateService: MockTranslateService; + let service: NavService + let modalService: NgbModal + let translateService: MockTranslateService beforeEach(() => { TestBed.configureTestingModule({ @@ -46,34 +48,34 @@ describe('NavService', () => { { provide: NgbModal, useClass: MockNgbModal }, { provide: TranslateService, useClass: MockTranslateService } ] - }); - service = TestBed.inject(NavService); - modalService = TestBed.inject(NgbModal); - translateService = TestBed.inject(TranslateService) as unknown as MockTranslateService; - }); + }) + service = TestBed.inject(NavService) + modalService = TestBed.inject(NgbModal) + translateService = TestBed.inject(TranslateService) as unknown as MockTranslateService + }) it('should be created', () => { - expect(service).toBeTruthy(); - }); + expect(service).toBeTruthy() + }) it('should open the login modal', () => { - const modalSpy = spyOn(modalService, 'open').and.callThrough(); - service.openLoginModal(); - expect(modalSpy).toHaveBeenCalledWith(jasmine.any(Function), { centered: true, size: 'lg' }); - }); + const modalSpy = spyOn(modalService, 'open').and.callThrough() + service.openLoginModal() + expect(modalSpy).toHaveBeenCalledWith(jasmine.any(Function), { centered: true, size: 'lg' }) + }) it('should change the language and update selectWidth correctly', () => { - service.changeLanguage('es'); - expect(translateService.getCurrentLang()).toBe('es'); - expect(service.selectWidth).toBe('57px'); + service.changeLanguage('es') + expect(translateService.getCurrentLang()).toBe('es') + expect(service.selectWidth).toBe('57px') - service.changeLanguage('ca'); - expect(translateService.getCurrentLang()).toBe('ca'); - expect(service.selectWidth).toBe('69px'); - }); + service.changeLanguage('ca') + expect(translateService.getCurrentLang()).toBe('ca') + expect(service.selectWidth).toBe('69px') + }) it('should set available languages and default language on creation', () => { - expect(translateService.getLangs()).toEqual(['en', 'es', 'ca']); - expect(translateService.getCurrentLang()).toBe('ca'); - }); -}); + expect(translateService.getLangs()).toEqual(['en', 'es', 'ca']) + expect(translateService.getCurrentLang()).toBe('ca') + }) +}) diff --git a/src/app/services/nav.service.ts b/src/app/services/nav.service.ts index eb3bfe60..becad55b 100644 --- a/src/app/services/nav.service.ts +++ b/src/app/services/nav.service.ts @@ -1,27 +1,29 @@ -import { Injectable } from '@angular/core'; -import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; -import { LoginModalComponent } from '../modules/modals/login-modal/login-modal.component'; -import { TranslateService } from '@ngx-translate/core'; +import { Inject, Injectable } from '@angular/core' +import { NgbModal } from '@ng-bootstrap/ng-bootstrap' +import { LoginModalComponent } from '../modules/modals/login-modal/login-modal.component' +import { TranslateService } from '@ngx-translate/core' @Injectable({ providedIn: 'root' }) export class NavService { - public selectWidth = '69px'; + public selectWidth = '69px' - constructor(private readonly modalService: NgbModal, private readonly translate: TranslateService) { - this.translate.addLangs(['en', 'es', 'ca']); - this.translate.setDefaultLang('ca'); - this.translate.use('ca'); + constructor ( + @Inject(NgbModal) private readonly modalService: NgbModal, + @Inject(TranslateService) private readonly translate: TranslateService + ) { + this.translate.addLangs(['en', 'es', 'ca']) + this.translate.setDefaultLang('ca') + this.translate.use('ca') } - openLoginModal(): void { - this.modalService.open(LoginModalComponent, { centered: true, size: 'lg' }); + openLoginModal (): void { + this.modalService.open(LoginModalComponent, { centered: true, size: 'lg' }) } - changeLanguage(language: string): void { - this.translate.use(language); - this.selectWidth = language === 'ca' ? '69px' : '57px'; + changeLanguage (language: string): void { + this.translate.use(language) + this.selectWidth = language === 'ca' ? '69px' : '57px' } } -