import { UserFacade } from '@app/adapters/users/user.facade';
import { TestBed } from '@angular/core/testing';
import { USER_REPOSITORY_TOKEN } from '@app/infrastructure/users/user-repository.token';
import { FakeUserRepository } from '@app/testing/domain/users/fake-user.repository';
import { ToastrService } from 'ngx-toastr';
import { mockToastR } from '@app/testing/toastr.mock';

describe('UserFacade', () => {
  let facade: UserFacade;
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        UserFacade,
        { provide: USER_REPOSITORY_TOKEN, useClass: FakeUserRepository },
        { provide: ToastrService, useValue: mockToastR },
      ],
    });
    facade = TestBed.inject(UserFacade);
  });

  it('devrait charger un user par son id ', () => {
    const userId = 'user_001';
    facade.loadOne(userId);

    setTimeout(() => {
      const user = facade.user();
      expect(user?.id).toBe(userId);
      expect(facade.error().hasError).toBe(false);
    }, 0);
  });

  it('devrait mettre a jour un user existant ', () => {
    const userId = 'user_001';
    const newData = { email: 'bar@foo.com' };
    facade.update(userId, newData);

    setTimeout(() => {
      const updatedUser = facade.user();
      expect(updatedUser?.email).toBe(newData.email);
      expect(facade.error().hasError).toBe(false);
    }, 0);
  });
});
