import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { ActivatedRoute, convertToParamMap, provideRouter, Router } from '@angular/router';
import { ResetPassword } from './reset-password';
import { environment } from '../../../../environments/environment';

describe('ResetPassword', () => {
  let httpMock: HttpTestingController;
  let router: Router;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [ResetPassword],
      providers: [
        provideHttpClient(),
        provideHttpClientTesting(),
        provideRouter([]),
        {
          provide: ActivatedRoute,
          useValue: { snapshot: { paramMap: convertToParamMap({ hash: 'reset-hash' }) } },
        },
      ],
    }).compileComponents();
    httpMock = TestBed.inject(HttpTestingController);
    router = TestBed.inject(Router);
  });

  afterEach(() => httpMock.verify());

  it('sets the password for the route hash and returns to login', () => {
    const fixture = TestBed.createComponent(ResetPassword);
    const navigateSpy = vi.spyOn(router, 'navigate');
    fixture.componentInstance['form'].setValue({
      password: 'new-secret',
      passwordConfirmation: 'new-secret',
    });
    fixture.componentInstance['onSubmit']();

    const request = httpMock.expectOne(`${environment.apiUrl}auth/reset/password`);
    expect(request.request.body).toEqual({ hash: 'reset-hash', password: 'new-secret' });
    request.flush(null);

    expect(navigateSpy).toHaveBeenCalledWith(['/auth/login'], { replaceUrl: true });
  });
});
