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

import { VariableDetailComponent } from './variable-detail.component';

describe('Variable Management Detail Component', () => {
  let comp: VariableDetailComponent;
  let fixture: ComponentFixture<VariableDetailComponent>;

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

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

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

      // THEN
      expect(instance.variable()).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();
    });
  });
});
