import { LinearWorkflowSectionComponent } from './linear-workflow-section.component';
import { LinearWorkflowComponent } from './linear-workflow.component';
import { SimpleChanges } from 'ng-metadata/core';
import Spy = jasmine.Spy;


describe("LinearWorkflowSectionComponent", () => {
  var component: LinearWorkflowSectionComponent;
  var parentMock: LinearWorkflowComponent;

  beforeEach(() => {
    parentMock = new LinearWorkflowComponent();
    component = new LinearWorkflowSectionComponent(parentMock);
  });

  describe("ngOnInit", () => {

    it("calls parent component to add itself as a section", () => {
      var addSectionSpy: Spy = spyOn(parentMock, "addSection").and.returnValue(null);
      component.ngOnInit();

      expect(addSectionSpy.calls.argsFor(0)[0]).toBe(component);
    });
  });

  describe("ngOnChanges", () => {
    var onSectionInvalidSpy: Spy;
    var changesObj: SimpleChanges;

    beforeEach(() => {
      onSectionInvalidSpy = spyOn(parentMock, "onSectionInvalid").and.returnValue(null);
      changesObj = {
        sectionValid: {
          currentValue: true,
          previousValue: false,
          isFirstChange: () => false,
        },
      };
    });

    it("does nothing if 'sectionValid' input not changed", () => {
      component.ngOnChanges({});

      expect(onSectionInvalidSpy).not.toHaveBeenCalled();
    });

    it("does nothing if 'sectionValid' input is true", () => {
      component.ngOnChanges(changesObj);

      expect(onSectionInvalidSpy).not.toHaveBeenCalled();
    });

    it("calls parent method to inform that section is invalid if 'sectionValid' input changed to false", () => {
      changesObj['sectionValid'].currentValue = false;
      component.ngOnChanges(changesObj);

      expect(onSectionInvalidSpy.calls.argsFor(0)[0]).toEqual(component.sectionId);
    });
  });

  describe("onSubmitSection", () => {
    var onNextSectionSpy: Spy;

    beforeEach(() => {
      onNextSectionSpy = spyOn(parentMock, "onNextSection").and.returnValue(null);
    });

    it("does nothing if section is invalid", () => {
      component.sectionValid = false;
      component.onSubmitSection();

      expect(onNextSectionSpy).not.toHaveBeenCalled();
    });

    it("calls parent method to go to next section if section is valid", () => {
      component.sectionValid = true;
      component.onSubmitSection();

      expect(onNextSectionSpy).toHaveBeenCalled();
    });
  });
});