ensure @Input bindings are not undefined before executing trigger logic
This commit is contained in:
parent
4750d1c5ef
commit
d5e35156e0
6 changed files with 64 additions and 53 deletions
|
@ -1,34 +1,35 @@
|
|||
import { LinearWorkflowSectionComponent } from './linear-workflow-section.component';
|
||||
import { LinearWorkflowComponent } from './linear-workflow.component';
|
||||
import { SimpleChanges } from 'ng-metadata/core';
|
||||
import { Mock } from 'ts-mocks';
|
||||
import Spy = jasmine.Spy;
|
||||
|
||||
|
||||
describe("LinearWorkflowSectionComponent", () => {
|
||||
var component: LinearWorkflowSectionComponent;
|
||||
var parentMock: LinearWorkflowComponent;
|
||||
var parentMock: Mock<LinearWorkflowComponent>;
|
||||
|
||||
beforeEach(() => {
|
||||
parentMock = new LinearWorkflowComponent();
|
||||
component = new LinearWorkflowSectionComponent(parentMock);
|
||||
parentMock = new Mock<LinearWorkflowComponent>();
|
||||
component = new LinearWorkflowSectionComponent(parentMock.Object);
|
||||
component.sectionId = "mysection";
|
||||
});
|
||||
|
||||
describe("ngOnInit", () => {
|
||||
|
||||
it("calls parent component to add itself as a section", () => {
|
||||
var addSectionSpy: Spy = spyOn(parentMock, "addSection").and.returnValue(null);
|
||||
parentMock.setup(mock => mock.addSection).is((section) => null);
|
||||
component.ngOnInit();
|
||||
|
||||
expect(addSectionSpy.calls.argsFor(0)[0]).toBe(component);
|
||||
expect((<Spy>parentMock.Object.addSection).calls.argsFor(0)[0]).toBe(component);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ngOnChanges", () => {
|
||||
var onSectionInvalidSpy: Spy;
|
||||
var changesObj: SimpleChanges;
|
||||
|
||||
beforeEach(() => {
|
||||
onSectionInvalidSpy = spyOn(parentMock, "onSectionInvalid").and.returnValue(null);
|
||||
parentMock.setup(mock => mock.onSectionInvalid).is((section) => null);
|
||||
changesObj = {
|
||||
sectionValid: {
|
||||
currentValue: true,
|
||||
|
@ -46,51 +47,58 @@ describe("LinearWorkflowSectionComponent", () => {
|
|||
it("does nothing if 'sectionValid' input not changed", () => {
|
||||
component.ngOnChanges({});
|
||||
|
||||
expect(onSectionInvalidSpy).not.toHaveBeenCalled();
|
||||
expect((<Spy>parentMock.Object.onSectionInvalid)).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does nothing if 'sectionValid' input's previous value is falsy", () => {
|
||||
changesObj['sectionValid'].previousValue = null;
|
||||
component.ngOnChanges(changesObj);
|
||||
|
||||
expect((<Spy>parentMock.Object.onSectionInvalid)).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does nothing if 'sectionValid' input is true", () => {
|
||||
component.ngOnChanges(changesObj);
|
||||
|
||||
expect(onSectionInvalidSpy).not.toHaveBeenCalled();
|
||||
expect((<Spy>parentMock.Object.onSectionInvalid)).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("calls parent method to inform that section is invalid if 'sectionValid' input changed to false", () => {
|
||||
changesObj['sectionValid'].previousValue = true;
|
||||
changesObj['sectionValid'].currentValue = false;
|
||||
component.ngOnChanges(changesObj);
|
||||
|
||||
expect(onSectionInvalidSpy.calls.argsFor(0)[0]).toEqual(component.sectionId);
|
||||
expect((<Spy>parentMock.Object.onSectionInvalid).calls.argsFor(0)[0]).toEqual(component.sectionId);
|
||||
});
|
||||
|
||||
it("calls parent method to go to next section if 'skipSection' input is true and is current section", () => {
|
||||
delete changesObj['sectionValid'];
|
||||
const onNextSectionSpy: Spy = spyOn(parentMock, 'onNextSection').and.returnValue(null);
|
||||
parentMock.setup(mock => mock.onNextSection).is(() => null);
|
||||
component.isCurrentSection = true;
|
||||
component.ngOnChanges(changesObj);
|
||||
|
||||
expect(onNextSectionSpy).toHaveBeenCalled();
|
||||
expect(<Spy>parentMock.Object.onNextSection).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("onSubmitSection", () => {
|
||||
var onNextSectionSpy: Spy;
|
||||
|
||||
beforeEach(() => {
|
||||
onNextSectionSpy = spyOn(parentMock, "onNextSection").and.returnValue(null);
|
||||
parentMock.setup(mock => mock.onNextSection).is(() => null);
|
||||
});
|
||||
|
||||
it("does nothing if section is invalid", () => {
|
||||
component.sectionValid = false;
|
||||
component.onSubmitSection();
|
||||
|
||||
expect(onNextSectionSpy).not.toHaveBeenCalled();
|
||||
expect(<Spy>parentMock.Object.onNextSection).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("calls parent method to go to next section if section is valid", () => {
|
||||
component.sectionValid = true;
|
||||
component.onSubmitSection();
|
||||
|
||||
expect(onNextSectionSpy).toHaveBeenCalled();
|
||||
expect(<Spy>parentMock.Object.onNextSection).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -24,7 +24,7 @@ export class LinearWorkflowSectionComponent implements OnChanges, OnInit {
|
|||
constructor(@Host() @Inject(LinearWorkflowComponent) private parent: LinearWorkflowComponent) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public ngOnInit(): void {
|
||||
if (!this.skipSection) {
|
||||
this.parent.addSection(this);
|
||||
|
@ -34,7 +34,7 @@ export class LinearWorkflowSectionComponent implements OnChanges, OnInit {
|
|||
public ngOnChanges(changes: SimpleChanges): void {
|
||||
switch (Object.keys(changes)[0]) {
|
||||
case 'sectionValid':
|
||||
if (!changes['sectionValid'].currentValue && this.parent) {
|
||||
if (changes['sectionValid'].previousValue && !changes['sectionValid'].currentValue && this.parent) {
|
||||
this.parent.onSectionInvalid(this.sectionId);
|
||||
}
|
||||
break;
|
||||
|
|
Reference in a new issue