import { ComponentFixture, TestBed } from '@angular/core/testing';
import { UsersComponent } from './users.component';
import { MatTableModule } from '@angular/material/table';
import { MatDialogModule } from '@angular/material/dialog';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ToastrService } from 'ngx-toastr';
import { of } from 'rxjs';
import { NO_ERRORS_SCHEMA } from '@angular/core'; 
import { TranslateModule } from '@ngx-translate/core';
import { ConfigService } from '@services/config.service';

class MockToastrService {
  success() {}
  error() {}
}

describe('UsersComponent', () => {
  let component: UsersComponent;
  let fixture: ComponentFixture<UsersComponent>;

  beforeEach(async () => {
    const mockConfigService = {
      apiUrl: 'http://mock-api-url',
      mercureUrl: 'http://mock-mercure-url'
    };

    await TestBed.configureTestingModule({
      declarations: [UsersComponent],
      imports: [
        MatTableModule,
        MatDialogModule,
        HttpClientTestingModule,
        TranslateModule.forRoot(),
      ],
      providers: [
        { provide: ToastrService, useClass: MockToastrService },
        { provide: ConfigService, useValue: mockConfigService }
      ],
      schemas: [NO_ERRORS_SCHEMA], // Ignorar elementos desconocidos
    }).compileComponents();

    fixture = TestBed.createComponent(UsersComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should have default values for pagination', () => {
    expect(component.itemsPerPage).toBe(10);
    expect(component.page).toBe(0);
    expect(component.pageSizeOptions).toEqual([5, 10, 20, 40, 100]);
  });

  it('should call search on init', () => {
    spyOn(component, 'search');
    component.ngOnInit();
    expect(component.search).toHaveBeenCalled();
  });

  it('should initialize the dataSource', () => {
    expect(component.dataSource).toBeDefined();
  });

  it('should define displayedColumns', () => {
    expect(component.displayedColumns).toBeDefined();
    expect(component.displayedColumns).toContain('id');
    expect(component.displayedColumns).toContain('username');
    expect(component.displayedColumns).toContain('roles');
    expect(component.displayedColumns).toContain('actions');
  });
});
