Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 1 | import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing"; |
| 2 | import {DynamicInputsComponent} from "./dynamic-inputs.component"; |
| 3 | import {async, ComponentFixture, getTestBed, TestBed} from "@angular/core/testing"; |
| 4 | import {CUSTOM_ELEMENTS_SCHEMA} from "@angular/core"; |
| 5 | import {FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, ValidatorFn} from "@angular/forms"; |
| 6 | import {BrowserModule} from "@angular/platform-browser"; |
| 7 | import {CommonModule} from "@angular/common"; |
| 8 | import {RouterTestingModule} from "@angular/router/testing"; |
| 9 | import {DynamicInputLabelPipe} from "../../pipes/dynamicInputLabel/dynamic-input-label.pipe"; |
| 10 | import {DynamicNumber} from "../../models/dynamicInput"; |
| 11 | |
| 12 | |
| 13 | describe('DynamicInputs Component', () => { |
| 14 | let component: DynamicInputsComponent; |
| 15 | let fixture: ComponentFixture<DynamicInputsComponent>; |
| 16 | |
| 17 | beforeAll(done => (async () => { |
| 18 | TestBed.configureTestingModule({ |
| 19 | imports: [BrowserModule, CommonModule, FormsModule, HttpClientTestingModule, RouterTestingModule, ReactiveFormsModule], |
| 20 | declarations: [DynamicInputsComponent, DynamicInputLabelPipe], |
| 21 | providers : [FormBuilder], |
| 22 | schemas: [ CUSTOM_ELEMENTS_SCHEMA ] |
| 23 | }); |
| 24 | await TestBed.compileComponents(); |
| 25 | |
| 26 | fixture = TestBed.createComponent(DynamicInputsComponent); |
| 27 | component = fixture.componentInstance; |
| 28 | fixture.detectChanges(); |
| 29 | })().then(done).catch(done.fail)); |
| 30 | |
| 31 | test('should be defined', () => { |
| 32 | expect(component).toBeDefined(); |
| 33 | }); |
| 34 | |
| 35 | test('isDynamicNumber should return item', () => { |
| 36 | let options = { |
| 37 | minLength : 10, |
| 38 | maxLength : 10 |
| 39 | }; |
| 40 | |
| 41 | let dynamicNumber : DynamicNumber = new DynamicNumber(<any>options); |
| 42 | expect(component.isDynamicNumber(dynamicNumber)).toBeDefined(); |
| 43 | }); |
| 44 | |
| 45 | test('buildValidators should return validator', () => { |
| 46 | let options = { |
| 47 | minLength : 10, |
| 48 | maxLength : 10, |
| 49 | max : 10, |
| 50 | min : 1 |
| 51 | }; |
| 52 | |
| 53 | let dynamicNumber : DynamicNumber = new DynamicNumber(<any>options); |
| 54 | let validator : ValidatorFn = component.buildValidators(dynamicNumber); |
| 55 | expect(validator).toBeDefined(); |
| 56 | }); |
| 57 | |
| 58 | |
| 59 | test('ngOnInit', ()=>{ |
| 60 | component.group = new FormGroup({ |
| 61 | |
| 62 | }); |
| 63 | component.list = [ |
| 64 | { |
| 65 | type : 'select', |
| 66 | value : 'optionName', |
| 67 | name : 'multiSelectControl', |
| 68 | optionList : [{ |
| 69 | isDataLoading : true, |
| 70 | name : 'optionName', |
| 71 | id : 'id' |
| 72 | }] |
| 73 | }, |
| 74 | { |
| 75 | type : 'select', |
| 76 | value : 'optionName', |
| 77 | name : 'multiSelectControl', |
| 78 | optionList : [{ |
| 79 | isDataLoading : true, |
| 80 | name : 'optionName', |
| 81 | id : 'id' |
| 82 | }] |
| 83 | }, |
| 84 | { |
| 85 | type : 'multi_select', |
| 86 | value : 'optionName', |
| 87 | name : 'selectControl', |
| 88 | optionList : [{ |
| 89 | isDataLoading : true, |
| 90 | name : 'optionName', |
| 91 | id : 'id' |
| 92 | }] |
| 93 | }, |
| 94 | { |
| 95 | type : 'multi_select', |
| 96 | value : 'optionName', |
| 97 | name : 'selectControl', |
| 98 | optionList : [{ |
| 99 | isDataLoading : true, |
| 100 | name : 'optionName' |
| 101 | }] |
| 102 | }, |
| 103 | { |
| 104 | type : 'boolean', |
| 105 | value : true, |
| 106 | name : 'booleanControl' |
| 107 | }, |
| 108 | { |
| 109 | type : 'boolean', |
| 110 | name : 'booleanControl2' |
| 111 | }, |
| 112 | { |
| 113 | type : 'number', |
| 114 | value : 100, |
| 115 | name : 'numberControl' |
| 116 | }, |
| 117 | { |
| 118 | type : 'file', |
| 119 | value : 'someValue', |
| 120 | name : 'fileControl' |
| 121 | }, |
| 122 | { |
| 123 | type : 'checkbox', |
| 124 | value : true, |
| 125 | name : 'checkboxControl' |
| 126 | }, |
| 127 | { |
| 128 | type : 'map', |
| 129 | value : true, |
| 130 | name : 'mapControl' |
| 131 | }, |
| 132 | { |
| 133 | type : 'list', |
| 134 | value : true, |
| 135 | name : 'listControl' |
| 136 | }, |
| 137 | { |
| 138 | type : 'default', |
| 139 | value : true, |
| 140 | name : 'defaultControl' |
| 141 | } |
| 142 | ]; |
| 143 | |
| 144 | component.ngOnInit(); |
| 145 | expect(component.group.controls['multiSelectControl'].value).toEqual('id'); |
| 146 | expect(component.group.controls['selectControl'].value).toEqual('optionName'); |
| 147 | expect(component.group.controls['booleanControl'].value).toEqual(true); |
| 148 | expect(component.group.controls['booleanControl2'].value).toEqual(false); |
| 149 | expect(component.group.controls['numberControl'].value).toEqual(100); |
| 150 | expect(component.group.controls['fileControl'].value).toEqual('someValue'); |
| 151 | expect(component.group.controls['checkboxControl'].value).toEqual(true); |
| 152 | expect(component.group.controls['mapControl'].value).toEqual(true); |
| 153 | expect(component.group.controls['listControl'].value).toEqual(true); |
| 154 | expect(component.group.controls['defaultControl'].value).toEqual(true); |
| 155 | }) |
| 156 | |
| 157 | }); |