import 'reflect-metadata'; import { FloatingLabelInputComponent, } from './floating-label-input.component'; import { FormControlState, } from 'ngrx-forms'; jest.mock('angular2-uuid', () => ({ UUID: { UUID: jest.fn().mockReturnValue(1), }, })); import { UUID } from 'angular2-uuid'; const initFloatingLabelInputComponent = () => { return new FloatingLabelInputComponent(); }; describe('ngOnInit', () => { test('Calls UUID.UUID', () => { (UUID as any).UUID.mockClear(); const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.ngOnInit(); expect(UUID.UUID).toHaveBeenCalledTimes(1); }); test('Sets inputId to the result of UUID.UUID with a prefix', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.ngOnInit(); expect(floatingLabelInputComponent.inputId) .toBe('floating-label-input-1'); }); }); describe('hasValue', () => { test('Returns false if there is no formControl', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.formControl = null; const result = floatingLabelInputComponent.hasValue; expect(result).toBe(false); }); test('Returns false if the formControl has no value', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.formControl = {} as FormControlState; const result = floatingLabelInputComponent.hasValue; expect(result).toBe(false); }); test('Returns false if the formControl has an empty string value', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.formControl = { value: '', } as FormControlState; const result = floatingLabelInputComponent.hasValue; expect(result).toBe(false); }); test('Returns true if the formControl has a value', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.formControl = { value: 't', } as FormControlState; const result = floatingLabelInputComponent.hasValue; expect(result).toBe(true); }); }); describe('isInvalid', () => { // tslint:disable-next-line test('Returns true if showInvalidMessages is true and formControl is invalid', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.showInvalidMessages = true; floatingLabelInputComponent.formControl = { isInvalid: true, } as FormControlState; const result = floatingLabelInputComponent.isInvalid; expect(result).toBe(true); }); // tslint:disable-next-line test('Returns false if showInvalidMessages is true and formControl is not invalid', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.showInvalidMessages = true; floatingLabelInputComponent.formControl = { isInvalid: false, } as FormControlState; const result = floatingLabelInputComponent.isInvalid; expect(result).toBe(false); }); // tslint:disable-next-line test('Returns false if showInvalidMessages is false and formControl is invalid', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.showInvalidMessages = false; floatingLabelInputComponent.formControl = { isInvalid: true, } as FormControlState; const result = floatingLabelInputComponent.isInvalid; expect(result).toBe(false); }); }); describe('isTextArea', () => { // tslint:disable-next-line test('Returns true if inputType is textarea', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.inputType = 'textarea'; expect(floatingLabelInputComponent.isTextArea).toBe(true); }); // tslint:disable-next-line test('Returns false if inputType is not textarea', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.inputType = 'text'; expect(floatingLabelInputComponent.isTextArea).toBe(false); }); }); describe('labelElementClasses', () => { // tslint:disable-next-line test('Returns c-floating-label-input-component__textarea-label if inputType is textarea', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.inputType = 'textarea'; expect(floatingLabelInputComponent.labelElementClasses).toBe( 'c-floating-label-input-component__textarea-label', ); }); // tslint:disable-next-line test('Returns c-floating-label-input-component__label if inputType is not textarea', () => { const floatingLabelInputComponent = initFloatingLabelInputComponent(); floatingLabelInputComponent.inputType = 'text'; expect(floatingLabelInputComponent.labelElementClasses).toBe( 'c-floating-label-input-component__label', ); }); });