diff --git a/lib/core/src/lib/form/components/widgets/file-viewer/file-viewer.widget.spec.ts b/lib/core/src/lib/form/components/widgets/file-viewer/file-viewer.widget.spec.ts new file mode 100644 index 0000000000..01f77baa19 --- /dev/null +++ b/lib/core/src/lib/form/components/widgets/file-viewer/file-viewer.widget.spec.ts @@ -0,0 +1,83 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { FormModel } from '../core/form.model'; +import { TranslateModule } from '@ngx-translate/core'; +import { FormFieldModel } from '../core/form-field.model'; +import { FormService } from '../../../services/form.service'; +import { FileViewerWidgetComponent } from './file-viewer.widget'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +describe('FileViewerWidgetComponent', () => { + const fakeForm = new FormModel(); + let widget: FileViewerWidgetComponent; + let formServiceStub: Partial; + let fixture: ComponentFixture; + + const fakePngAnswer: any = { + id: '1933', + link: false, + isExternal: false, + relatedContent: false, + contentAvailable: true, + name: 'a_png_file.png', + simpleType: 'image', + mimeType: 'image/png', + previewStatus: 'queued', + thumbnailStatus: 'queued', + created: '2022-10-14T17:17:37.099Z', + createdBy: { id: 1001, firstName: 'Admin', lastName: 'admin', email: 'admin@example.com' } + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + TranslateModule.forRoot() + ], + declarations: [ FileViewerWidgetComponent ], + providers: [ { provide: FormService, useValue: formServiceStub } ] + }); + + formServiceStub = TestBed.inject(FormService); + fixture = TestBed.createComponent(FileViewerWidgetComponent); + widget = fixture.componentInstance; + }); + + it('should set the file id corretly when the field value is an array', (done) => { + const fakeField = new FormFieldModel(fakeForm, { id: 'fakeField', value: [fakePngAnswer] }); + widget.field = fakeField; + + fixture.detectChanges(); + + fixture.whenStable().then(() => { + expect(widget.field.value).toBe('1933'); + done(); + }); + }); + + it('should set the file id corretly when the field value is a string', (done) => { + const fakeField = new FormFieldModel(fakeForm, { id: 'fakeField', value: 'fakeValue' }); + widget.field = fakeField; + + fixture.detectChanges(); + + fixture.whenStable().then(() => { + expect(widget.field.value).toBe('fakeValue'); + done(); + }); + }); +}); diff --git a/lib/core/src/lib/form/components/widgets/file-viewer/file-viewer.widget.ts b/lib/core/src/lib/form/components/widgets/file-viewer/file-viewer.widget.ts index 03a5cf6b79..b0b1b672aa 100644 --- a/lib/core/src/lib/form/components/widgets/file-viewer/file-viewer.widget.ts +++ b/lib/core/src/lib/form/components/widgets/file-viewer/file-viewer.widget.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { Component, ViewEncapsulation } from '@angular/core'; +import { Component, OnInit, ViewEncapsulation } from '@angular/core'; import { FormService } from '../../../services/form.service'; import { WidgetComponent } from '../widget.component'; @@ -38,9 +38,18 @@ import { WidgetComponent } from '../widget.component'; }, encapsulation: ViewEncapsulation.None }) -export class FileViewerWidgetComponent extends WidgetComponent { - +export class FileViewerWidgetComponent extends WidgetComponent implements OnInit { constructor(formService: FormService) { super(formService); } + + ngOnInit(): void { + if (this.field && + this.field.value && + Array.isArray(this.field.value) && + this.field.value.length) { + const file = this.field.value[0]; + this.field.value = file.id; + } + } }