import { LocationService } from './location-service';
import { TestBed } from '@angular/core/testing';
import { Router, RouterEvent } from '@angular/router';
import { ReplaySubject } from 'rxjs';
import { RouterTestingModule } from '@angular/router/testing';
import { first } from 'rxjs/operators';

describe('LocationService', () => {
  const routerEvents = new ReplaySubject<RouterEvent>(1);
  const mockRouter = {
    events: routerEvents.asObservable(),
  };
  let locationService: LocationService;
  let router: jasmine.SpyObj<Router>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        {
          provide: Router,
          useValue: mockRouter,
        },
      ],
    });

    router = TestBed.inject(Router);
    locationService = TestBed.inject(LocationService);
  });

  it('should return the current path', async () => {
    routerEvents.next({
      id: Date.now(),
      url: '/test',
    });
    let result = await locationService.currentPath$.pipe(first()).toPromise();

    expect(result).toBe('/test');

    routerEvents.next({
      id: Date.now(),
      url: '/test/mock',
    });

    result = await locationService.currentPath$.pipe(first()).toPromise();
    expect(result).toBe('/test/mock');
  });

  it('should not return an undefined path', async () => {
    routerEvents.next({
      id: Date.now(),
      url: undefined,
    });
    routerEvents.next({
      id: Date.now(),
      url: '/test',
    });

    const result = await locationService.currentPath$.pipe(first()).toPromise();

    expect(result).toBe('/test');
  });
});
