import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  let translateService: TranslateService;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
        TranslateModule.forRoot()
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();

    translateService = TestBed.inject(TranslateService);
  });

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });

  it(`should have as title 'ogWebconsole'`, () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app.title).toEqual('ogWebconsole');
  });

  it('should set the language from localStorage on creation', () => {
    spyOn(localStorage, 'getItem').and.returnValue('en'); // Simula que el idioma guardado es "en"
    spyOn(translateService, 'use');

    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();

    expect(localStorage.getItem).toHaveBeenCalledWith('language');
    expect(translateService.use).toHaveBeenCalledWith('en');
  });

  it('should default to Spanish if no language is saved in localStorage', () => {
    spyOn(localStorage, 'getItem').and.returnValue(null); // Simula que no hay idioma guardado
    spyOn(translateService, 'use');

    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();

    expect(localStorage.getItem).toHaveBeenCalledWith('language');
    expect(translateService.use).toHaveBeenCalledWith('es');
  });

  it('should set language to Spanish in sessionStorage on ngOnInit', () => {
    spyOn(sessionStorage, 'setItem');

    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;

    app.ngOnInit();
    expect(sessionStorage.setItem).toHaveBeenCalledWith('language', 'es');
  });
});
