import 'reflect-metadata'; import { NgrxFormCheckboxComponent, } from './ngrx-form-checkbox.component'; jest.mock('angular2-uuid', () => ({ UUID: { UUID: jest.fn().mockReturnValue(1), }, })); import { UUID } from 'angular2-uuid'; import { FormControlState } from 'ngrx-forms'; const initNgrxFormCheckboxComponent = () => { return new NgrxFormCheckboxComponent(); }; describe('ngOnInit', () => { test('Calls UUID.UUID', () => { (UUID as any).UUID.mockClear(); const ngrxFormCheckboxComponent = initNgrxFormCheckboxComponent(); ngrxFormCheckboxComponent.ngOnInit(); expect(UUID.UUID).toHaveBeenCalledTimes(1); }); test('Sets inputId to the result of UUID.UUID with a prefix', () => { const ngrxFormCheckboxComponent = initNgrxFormCheckboxComponent(); ngrxFormCheckboxComponent.ngOnInit(); expect(ngrxFormCheckboxComponent.inputId) .toBe('ngrx-form-checkbox-1'); }); }); describe('hasValue', () => { test('Returns true if the formControl value is true', () => { const ngrxFormCheckboxComponent = initNgrxFormCheckboxComponent(); ngrxFormCheckboxComponent.formControl = { value: true, } as FormControlState; const result = ngrxFormCheckboxComponent.hasValue; expect(result).toBe(true); }); test('Returns false if the formControl value is false', () => { const ngrxFormCheckboxComponent = initNgrxFormCheckboxComponent(); ngrxFormCheckboxComponent.formControl = { value: false, } as FormControlState; const result = ngrxFormCheckboxComponent.hasValue; expect(result).toBe(false); }); }); describe('showInvalid', () => { // tslint:disable-next-line test('Returns true if showInvalidMessages is true and formControl is invalid', () => { const ngrxFormCheckboxComponent = initNgrxFormCheckboxComponent(); ngrxFormCheckboxComponent.showInvalidMessages = true; ngrxFormCheckboxComponent.formControl = { isInvalid: true, } as FormControlState; const result = ngrxFormCheckboxComponent.showInvalid; expect(result).toBe(true); }); // tslint:disable-next-line test('Returns false if showInvalidMessages is true and formControl is not invalid', () => { const ngrxFormCheckboxComponent = initNgrxFormCheckboxComponent(); ngrxFormCheckboxComponent.showInvalidMessages = true; ngrxFormCheckboxComponent.formControl = { isInvalid: false, } as FormControlState; const result = ngrxFormCheckboxComponent.showInvalid; expect(result).toBe(false); }); // tslint:disable-next-line test('Returns false if showInvalidMessages is false and formControl is invalid', () => { const ngrxFormCheckboxComponent = initNgrxFormCheckboxComponent(); ngrxFormCheckboxComponent.showInvalidMessages = false; ngrxFormCheckboxComponent.formControl = { isInvalid: true, } as FormControlState; const result = ngrxFormCheckboxComponent.showInvalid; expect(result).toBe(false); }); });