import { constructWithInjector } from "../../../testing/construct-with-injector"; // Load the implementations that should be tested import { ChangeDetectorRef, SimpleChanges } from "@angular/core"; import { SamInputMaskComponent } from "./input-mask.component"; import { SamFormService } from "../../form-service"; describe("The Sam Input Mask component", () => { let component: SamInputMaskComponent; // provide our implementations or mocks to the dependency injector beforeEach(() => { component = constructWithInjector( [ SamFormService, { provide: ChangeDetectorRef, useValue: { detectChanges: () => {} } as unknown as ChangeDetectorRef, }, ], () => new SamInputMaskComponent() ); component.template = "__/__/____"; }); it("Should compile", () => { expect(true).toBe(true); }); it("Should check for template", () => { component.template = null; try { component.ngOnInit(); fail(); } catch (exception) { expect(exception.toString()).toContain("No template provided"); } }); it("test numberToTemplate", () => { expect(component.numberToTemplate("12122013")).toBe("12/12/2013"); }); it("should test templateToNumber", () => { expect(component.templateToNumber("12/12/2013")).toBe("12122013"); }); it("should test onFocus", () => { component.onHostFocus(); component.writeValue("12/12/2013"); component.onFocus(); expect(component.value).toBe("12122013"); }); it("should test onBlur", () => { component.writeValue("12122013"); component.onBlur(); expect(component.value).toBe("12/12/2013"); }); it("should test modelChange", () => { component.maxlength = 10; component.previousVal = "12/12/201"; component.onModelChange("12/12/2013"); expect(component.value).toBe("12/12/2013"); component.onModelChange(""); expect(component.value).toBe(""); }); it("should test that maxlength is always a number", () => { try { component.maxlength = "10" as unknown as number; component.ngOnChanges({ maxlength: "10", } as unknown as SimpleChanges); fail(); } catch (exception) { expect(exception.toString()).toContain( "Wrong data type passed in for maxlength" ); } }); });