|
| 1 | +import { Component, DebugElement, Inject, Input, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 3 | +import { FormBuilder, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms'; |
| 4 | +import { MatButtonModule } from '@angular/material/button'; |
| 5 | +import { MatDialogModule } from '@angular/material/dialog'; |
| 6 | +import { MatFormFieldModule } from '@angular/material/form-field'; |
| 7 | +import { MatSnackBarModule } from '@angular/material/snack-bar'; |
| 8 | +import { By } from '@angular/platform-browser'; |
| 9 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 10 | +import { |
| 11 | + CreateIntValue, |
| 12 | + CreateResource, |
| 13 | + CreateTextValueAsString, |
| 14 | + CreateValue, |
| 15 | + MockOntology, |
| 16 | + MockResource, |
| 17 | + ReadResource, |
| 18 | + ResourceClassAndPropertyDefinitions, |
| 19 | + ResourceClassDefinition, |
| 20 | + ResourcePropertyDefinition, |
| 21 | + ResourcesEndpointV2 |
| 22 | +} from '@dasch-swiss/dsp-js'; |
| 23 | +import { OntologyCache } from '@dasch-swiss/dsp-js/src/cache/ontology-cache/OntologyCache'; |
| 24 | +import { TranslateModule } from '@ngx-translate/core'; |
| 25 | +import { of } from 'rxjs'; |
| 26 | +import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens'; |
| 27 | +import { BaseValueDirective } from 'src/app/main/directive/base-value.directive'; |
| 28 | +import { SwitchPropertiesComponent } from '../../resource-instance-form/select-properties/switch-properties/switch-properties.component'; |
| 29 | +import { ValueService } from '../../services/value.service'; |
| 30 | +import { IntValueComponent } from '../../values/int-value/int-value.component'; |
| 31 | +import { TextValueAsStringComponent } from '../../values/text-value/text-value-as-string/text-value-as-string.component'; |
| 32 | + |
| 33 | +import { CreateLinkResourceComponent } from './create-link-resource.component'; |
| 34 | + |
| 35 | +/** |
| 36 | + * test host component to simulate parent component. |
| 37 | + */ |
| 38 | +@Component({ |
| 39 | + template: ` |
| 40 | + <app-create-link-resource [parentResource]="parentResource" [propDef]="propDef" [resourceClassDef]="resourceClassDef" #createLinkResourceComp></app-create-link-resource>` |
| 41 | +}) |
| 42 | +class TestHostComponent implements OnInit { |
| 43 | + |
| 44 | + @ViewChild('createLinkResourceComp') createLinkResourceComponent: CreateLinkResourceComponent; |
| 45 | + |
| 46 | + parentResource: ReadResource; |
| 47 | + propDef: string; |
| 48 | + resourceClassDef: string; |
| 49 | + |
| 50 | + ngOnInit() { |
| 51 | + MockResource.getTestThing().subscribe(res => { |
| 52 | + this.parentResource = res; |
| 53 | + this.propDef = 'http://0.0.0.0:3333/ontology/0001/anything/v2#hasOtherThingValue'; |
| 54 | + this.resourceClassDef = 'http://0.0.0.0:3333/ontology/0001/anything/v2#Thing'; |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * mock select-properties component to use in tests. |
| 62 | + */ |
| 63 | +@Component({ |
| 64 | + selector: 'app-select-properties', |
| 65 | + template: ` |
| 66 | + <app-text-value-as-string #createVal |
| 67 | + [mode]="'create'" |
| 68 | + [commentDisabled]="true" |
| 69 | + [valueRequiredValidator]="true" |
| 70 | + [parentForm]="parentForm" |
| 71 | + [formName]="'label'"> |
| 72 | + </app-text-value-as-string> |
| 73 | + ` |
| 74 | +}) |
| 75 | +class MockSelectPropertiesComponent { |
| 76 | + @ViewChildren('switchProp') switchPropertiesComponent: QueryList<SwitchPropertiesComponent>; |
| 77 | + |
| 78 | + // input for resource's label |
| 79 | + @ViewChild('createVal') createValueComponent: BaseValueDirective; |
| 80 | + |
| 81 | + @Input() properties: ResourcePropertyDefinition[]; |
| 82 | + |
| 83 | + @Input() ontologyInfo: ResourceClassAndPropertyDefinitions; |
| 84 | + |
| 85 | + @Input() resourceClass: ResourceClassDefinition; |
| 86 | + |
| 87 | + @Input() parentForm: FormGroup; |
| 88 | + |
| 89 | + parentResource = new ReadResource(); |
| 90 | + |
| 91 | + constructor(private _valueService: ValueService) { } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * mock switch-properties component to use in tests. |
| 96 | + */ |
| 97 | +@Component({ |
| 98 | + selector: 'app-switch-properties' |
| 99 | +}) |
| 100 | +class MockSwitchPropertiesComponent { |
| 101 | + @ViewChild('createVal') createValueComponent: BaseValueDirective; |
| 102 | + |
| 103 | + @Input() property: ResourcePropertyDefinition; |
| 104 | + |
| 105 | + @Input() parentResource: ReadResource; |
| 106 | + |
| 107 | + @Input() parentForm: FormGroup; |
| 108 | + |
| 109 | + @Input() formName: string; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * mock value component to use in tests. |
| 114 | + */ |
| 115 | +@Component({ |
| 116 | + selector: 'dsp-int-value' |
| 117 | +}) |
| 118 | +class MockCreateIntValueComponent implements OnInit { |
| 119 | + |
| 120 | + @ViewChild('createVal') createValueComponent: IntValueComponent; |
| 121 | + |
| 122 | + @Input() parentForm: FormGroup; |
| 123 | + |
| 124 | + @Input() formName: string; |
| 125 | + |
| 126 | + @Input() mode; |
| 127 | + |
| 128 | + @Input() displayValue; |
| 129 | + |
| 130 | + form: FormGroup; |
| 131 | + |
| 132 | + valueFormControl: FormControl; |
| 133 | + |
| 134 | + constructor(@Inject(FormBuilder) private _fb: FormBuilder) { } |
| 135 | + |
| 136 | + ngOnInit(): void { |
| 137 | + this.valueFormControl = new FormControl(null, [Validators.required]); |
| 138 | + |
| 139 | + this.form = this._fb.group({ |
| 140 | + test: this.valueFormControl |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + getNewValue(): CreateValue { |
| 145 | + const createIntVal = new CreateIntValue(); |
| 146 | + |
| 147 | + createIntVal.int = 123; |
| 148 | + |
| 149 | + return createIntVal; |
| 150 | + } |
| 151 | + |
| 152 | + updateCommentVisibility(): void { } |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * mock value component to use in tests. |
| 157 | + */ |
| 158 | +@Component({ |
| 159 | + selector: 'app-text-value-as-string' |
| 160 | +}) |
| 161 | +class MockCreateTextValueComponent implements OnInit { |
| 162 | + |
| 163 | + @ViewChild('createVal') createValueComponent: TextValueAsStringComponent; |
| 164 | + |
| 165 | + @Input() parentForm: FormGroup; |
| 166 | + |
| 167 | + @Input() formName: string; |
| 168 | + |
| 169 | + @Input() mode; |
| 170 | + |
| 171 | + @Input() displayValue; |
| 172 | + |
| 173 | + @Input() commentDisabled?: boolean; |
| 174 | + |
| 175 | + @Input() valueRequiredValidator: boolean; |
| 176 | + |
| 177 | + form: FormGroup; |
| 178 | + |
| 179 | + valueFormControl: FormControl; |
| 180 | + constructor(@Inject(FormBuilder) private _fb: FormBuilder) { } |
| 181 | + ngOnInit(): void { |
| 182 | + this.valueFormControl = new FormControl(null, [Validators.required]); |
| 183 | + this.form = this._fb.group({ |
| 184 | + label: this.valueFormControl |
| 185 | + }); |
| 186 | + } |
| 187 | + getNewValue(): CreateValue { |
| 188 | + const createTextVal = new CreateTextValueAsString(); |
| 189 | + createTextVal.text = 'My Label'; |
| 190 | + return createTextVal; |
| 191 | + } |
| 192 | + updateCommentVisibility(): void { } |
| 193 | +} |
| 194 | + |
| 195 | +describe('CreateLinkResourceComponent', () => { |
| 196 | + let testHostComponent: TestHostComponent; |
| 197 | + let testHostFixture: ComponentFixture<TestHostComponent>; |
| 198 | + let createLinkResourceComponentDe: DebugElement; |
| 199 | + |
| 200 | + beforeEach(waitForAsync(() => { |
| 201 | + |
| 202 | + const dspConnSpy = { |
| 203 | + v2: { |
| 204 | + ontologyCache: jasmine.createSpyObj('ontologyCache', ['getOntology', 'getResourceClassDefinition']), |
| 205 | + res: jasmine.createSpyObj('res', ['createResource']) |
| 206 | + } |
| 207 | + }; |
| 208 | + |
| 209 | + TestBed.configureTestingModule({ |
| 210 | + declarations: [ |
| 211 | + CreateLinkResourceComponent, |
| 212 | + TestHostComponent, |
| 213 | + MockSelectPropertiesComponent, |
| 214 | + MockCreateTextValueComponent, |
| 215 | + MockSwitchPropertiesComponent, |
| 216 | + MockCreateIntValueComponent |
| 217 | + ], |
| 218 | + imports: [ |
| 219 | + BrowserAnimationsModule, |
| 220 | + MatButtonModule, |
| 221 | + MatDialogModule, |
| 222 | + MatFormFieldModule, |
| 223 | + MatSnackBarModule, |
| 224 | + ReactiveFormsModule, |
| 225 | + TranslateModule.forRoot() |
| 226 | + ], |
| 227 | + providers: [ |
| 228 | + { |
| 229 | + provide: DspApiConnectionToken, |
| 230 | + useValue: dspConnSpy |
| 231 | + }, |
| 232 | + ValueService |
| 233 | + ] |
| 234 | + }) |
| 235 | + .compileComponents(); |
| 236 | + })); |
| 237 | + |
| 238 | + beforeEach(() => { |
| 239 | + const dspConnSpy = TestBed.inject(DspApiConnectionToken); |
| 240 | + |
| 241 | + (dspConnSpy.v2.ontologyCache as jasmine.SpyObj<OntologyCache>).getResourceClassDefinition.and.callFake( |
| 242 | + (resClassIri: string) => of(MockOntology.mockIResourceClassAndPropertyDefinitions('http://0.0.0.0:3333/ontology/0001/anything/v2#Thing')) |
| 243 | + ); |
| 244 | + |
| 245 | + testHostFixture = TestBed.createComponent(TestHostComponent); |
| 246 | + testHostComponent = testHostFixture.componentInstance; |
| 247 | + testHostFixture.detectChanges(); |
| 248 | + expect(testHostComponent).toBeTruthy(); |
| 249 | + |
| 250 | + const hostCompDe = testHostFixture.debugElement; |
| 251 | + |
| 252 | + createLinkResourceComponentDe = hostCompDe.query(By.directive(CreateLinkResourceComponent)); |
| 253 | + |
| 254 | + }); |
| 255 | + |
| 256 | + it('should initialize the properties array', async () => { |
| 257 | + expect(testHostComponent.createLinkResourceComponent.properties.length).toEqual(18); |
| 258 | + }); |
| 259 | + |
| 260 | + it('should submit the form', () => { |
| 261 | + const dspConnSpy = TestBed.inject(DspApiConnectionToken); |
| 262 | + |
| 263 | + (dspConnSpy.v2.res as jasmine.SpyObj<ResourcesEndpointV2>).createResource.and.callFake( |
| 264 | + () => { |
| 265 | + let resource = new ReadResource(); |
| 266 | + |
| 267 | + MockResource.getTestThing().subscribe((res) => { |
| 268 | + resource = res; |
| 269 | + }); |
| 270 | + |
| 271 | + return of(resource); |
| 272 | + } |
| 273 | + ); |
| 274 | + |
| 275 | + const anythingOnto = MockOntology.mockReadOntology('http://0.0.0.0:3333/ontology/0001/anything/v2'); |
| 276 | + |
| 277 | + // get resource class definitions |
| 278 | + const resourceClasses = anythingOnto.getClassDefinitionsByType(ResourceClassDefinition); |
| 279 | + |
| 280 | + testHostComponent.createLinkResourceComponent.properties = new Array<ResourcePropertyDefinition>(); |
| 281 | + |
| 282 | + MockResource.getTestThing().subscribe(res => { |
| 283 | + const resourcePropDef = (res.entityInfo as ResourceClassAndPropertyDefinitions).getAllPropertyDefinitions()[9]; |
| 284 | + testHostComponent.createLinkResourceComponent.properties.push(resourcePropDef as ResourcePropertyDefinition); |
| 285 | + }); |
| 286 | + |
| 287 | + testHostFixture.detectChanges(); |
| 288 | + |
| 289 | + const selectPropertiesComp = createLinkResourceComponentDe.query(By.directive(MockSelectPropertiesComponent)); |
| 290 | + |
| 291 | + expect(selectPropertiesComp).toBeTruthy(); |
| 292 | + |
| 293 | + const label = new CreateTextValueAsString(); |
| 294 | + label.text = 'My Label'; |
| 295 | + |
| 296 | + const props = {}; |
| 297 | + const createVal = new CreateIntValue(); |
| 298 | + createVal.int = 123; |
| 299 | + props['http://0.0.0.0:3333/ontology/0001/anything/v2#hasInteger'] = [createVal]; |
| 300 | + |
| 301 | + const expectedCreateResource = new CreateResource(); |
| 302 | + expectedCreateResource.label = 'My Label'; |
| 303 | + expectedCreateResource.type = 'http://0.0.0.0:3333/ontology/0001/anything/v2#Thing'; |
| 304 | + expectedCreateResource.properties = props; |
| 305 | + |
| 306 | + testHostComponent.createLinkResourceComponent.onSubmit(); |
| 307 | + |
| 308 | + expect(dspConnSpy.v2.res.createResource).toHaveBeenCalledTimes(1); |
| 309 | + }); |
| 310 | +}); |
0 commit comments