import { ComponentFixture, TestBed } from '@angular/core/testing';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { RouterTestingHarness } from '@angular/router/testing';
import { of } from 'rxjs';

import { ResilientActivityProgressComponent } from './resilient-activity-progress.component';

describe('Resilient Activity Progress Component', () => {
  let comp: ResilientActivityProgressComponent;
  let fixture: ComponentFixture<ResilientActivityProgressComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [ResilientActivityProgressComponent],
      providers: [
        provideRouter(
          [
            {
              path: '**',
              component: ResilientActivityProgressComponent,
              resolve: { period: () => of({ id: 123 }) },
            },
          ],
          withComponentInputBinding(),
        ),
      ],
    })
      .overrideTemplate(ResilientActivityProgressComponent, '')
      .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(ResilientActivityProgressComponent);
    comp = fixture.componentInstance;
  });

  describe('OnInit', () => {
    it('Should load period on init', async () => {
      const harness = await RouterTestingHarness.create();
      const instance = await harness.navigateByUrl('/', ResilientActivityProgressComponent);

      // THEN
      expect(instance.period()).toEqual(expect.objectContaining({ id: 123 }));
    });
  });

  describe('PreviousState', () => {
    it('Should navigate to previous state', () => {
      jest.spyOn(window.history, 'back');
      comp.previousState();
      expect(window.history.back).toHaveBeenCalled();
    });
  });
});
