From 5d408025b04fd8d2ff237dee576141ecc1bfa9b5 Mon Sep 17 00:00:00 2001 From: dhrn <14145706+dhrn@users.noreply.github.com> Date: Mon, 2 Mar 2020 23:27:17 +0530 Subject: [PATCH] [AAE-1949] Form Attach File Widget AAE unit test (#5527) * [AAE-1949] Form Attach File Widget AAE unit test * [AAE-1949] Form Attach File Widget AAE unit test * * fixed type * * fixed second upload * * fixed second upload --- .../components/form-cloud.component.spec.ts | 77 +++++++++++++++---- ...attach-file-cloud-widget.component.spec.ts | 46 +++++++++++ .../src/lib/form/mocks/cloud-form.mock.ts | 74 ++++++++++++++++++ 3 files changed, 181 insertions(+), 16 deletions(-) diff --git a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts index d1abcb6085..6ca16bb076 100644 --- a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts +++ b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts @@ -15,45 +15,64 @@ * limitations under the License. */ -import { SimpleChange, DebugElement, CUSTOM_ELEMENTS_SCHEMA, Component } from '@angular/core'; +import { Component, CUSTOM_ELEMENTS_SCHEMA, DebugElement, SimpleChange } from '@angular/core'; import { By } from '@angular/platform-browser'; -import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; +import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; import { Observable, of, throwError } from 'rxjs'; import { - FormFieldModel, FormFieldTypes, FormService, FormOutcomeEvent, FormOutcomeModel, LogService, WidgetVisibilityService, - setupTestBed, AppConfigService, FormRenderingService, FormModel + AppConfigService, + FormFieldModel, + FormFieldTypes, + FormModel, + FormOutcomeEvent, + FormOutcomeModel, + FormRenderingService, + setupTestBed, + TranslationMock, + TranslationService, + WidgetVisibilityService } from '@alfresco/adf-core'; import { ProcessServiceCloudTestingModule } from '../../testing/process-service-cloud.testing.module'; import { FormCloudService } from '../services/form-cloud.service'; import { FormCloudComponent } from './form-cloud.component'; -import { cloudFormMock, emptyFormRepresentationJSON, fakeCloudForm } from '../mocks/cloud-form.mock'; +import { + cloudFormMock, + conditionalUploadWidgetsMock, + emptyFormRepresentationJSON, + fakeCloudForm +} from '../mocks/cloud-form.mock'; import { FormCloudRepresentation } from '../models/form-cloud-representation.model'; +import { FormCloudModule } from '../form-cloud.module'; describe('FormCloudComponent', () => { let formCloudService: FormCloudService; - let formService: FormService; + let fixture: ComponentFixture; let formComponent: FormCloudComponent; let visibilityService: WidgetVisibilityService; - let logService: LogService; let formRenderingService: FormRenderingService; - let appConfigService: AppConfigService; setupTestBed({ - imports: [ProcessServiceCloudTestingModule] + imports: [ + ProcessServiceCloudTestingModule, + FormCloudModule + ], + providers: [ + { provide: TranslationService, useClass: TranslationMock } + ] }); beforeEach(() => { - logService = new LogService(null); formRenderingService = TestBed.get(FormRenderingService); - visibilityService = new WidgetVisibilityService(null, logService); - spyOn(visibilityService, 'refreshVisibility').and.stub(); - appConfigService = TestBed.get(AppConfigService); + formCloudService = TestBed.get(FormCloudService); + visibilityService = TestBed.get(WidgetVisibilityService); + spyOn(visibilityService, 'refreshVisibility').and.callThrough(); + const appConfigService = TestBed.get(AppConfigService); spyOn(appConfigService, 'get').and.returnValue([]); spyOn(formRenderingService, 'setComponentTypeResolver').and.returnValue(true); - formCloudService = new FormCloudService(null, new AppConfigService(null)); - formService = new FormService(null, null, logService); - formComponent = new FormCloudComponent(formCloudService, formService, null, formRenderingService, visibilityService, appConfigService); + fixture = TestBed.createComponent(FormCloudComponent); + formComponent = fixture.componentInstance; + fixture.detectChanges(); }); it('should check form', () => { @@ -827,6 +846,32 @@ describe('FormCloudComponent', () => { radioFieldById = formFields.find((field) => field.id === 'radiobuttons1'); expect(radioFieldById.value).toBe('option_2'); }); + + describe('form validations', () => { + + it('should be able to set visibility conditions for Attach File widget', async () => { + spyOn(formCloudService, 'getForm').and.returnValue(of(conditionalUploadWidgetsMock)); + const formId = '123'; + const appName = 'test-app'; + formComponent.formId = formId; + formComponent.appVersion = 1; + + formComponent.ngOnChanges({ 'appName': new SimpleChange(null, appName, true) }); + expect(formCloudService.getForm).toHaveBeenCalledWith(appName, formId, 1); + + fixture.detectChanges(); + const inputElement = fixture.debugElement.query(By.css('[id="field-Text0xlk8n-container"] input')); + inputElement.nativeElement.value = 'Attach'; + inputElement.nativeElement.dispatchEvent(new Event('input')); + fixture.detectChanges(); + const container = '[id="field-Attachfile0h9fr1-container"]'; + const uploadElement = fixture.debugElement.query(By.css(container)); + expect(uploadElement).toBeDefined(); + const label = fixture.debugElement.query(By.css(`${container} label`)); + expect(label.nativeElement.innerText).toEqual('Attach file'); + }); + + }); }); @Component({ diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.spec.ts b/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.spec.ts index 7179d8d6d9..2e28c9c1ba 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.spec.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.spec.ts @@ -234,6 +234,52 @@ describe('AttachFileCloudWidgetComponent', () => { }); })); + it('should be able to set label property for Attach File widget', () => { + widget.field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.UPLOAD, + readOnly: true, + id: 'attach-file', + name: 'Label', + params: onlyLocalParams + }); + fixture.detectChanges(); + fixture.whenStable().then(() => { + expect(element.querySelector('label').innerText).toEqual('Label'); + }); + }); + + it('should be able to enable multiple file upload', async(() => { + const files = [fakeLocalPngAnswer, { ...fakeLocalPngAnswer, id: 1166, nodeId: 1166, name: 'second_png_file.png' }]; + widget.field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.UPLOAD, + id: 'attach-file', + name: 'Upload', + value: [], + params: { onlyLocalParams, multiple: true } + }); + spyOn(processCloudContentService, 'createTemporaryRawRelatedContent') + .and.returnValues(of(files[0]), of(files[1])); + fixture.detectChanges(); + fixture.whenStable().then(() => { + const inputDebugElement = fixture.debugElement.query(By.css('#attach-file')); + expect(inputDebugElement.nativeElement.multiple).toBe(true); + inputDebugElement.triggerEventHandler('change', { + target: { files: [ files[0] ] } + }); + fixture.detectChanges(); + expect(element.querySelector('#file-1155-icon')).toBeDefined(); + let name: HTMLElement = element.querySelector('span[id="file-1155"]'); + expect(name.innerText).toEqual('a_png_file.png'); + inputDebugElement.triggerEventHandler('change', { + target: { files: [ files[1] ] } + }); + fixture.detectChanges(); + expect(element.querySelector('#file-1166-icon')).toBeDefined(); + name = element.querySelector('span[id="file-1166"]'); + expect(name.innerText).toEqual('second_png_file.png'); + }); + })); + describe('when is readonly', () => { it('should show empty list message when there are no file', async(() => { diff --git a/lib/process-services-cloud/src/lib/form/mocks/cloud-form.mock.ts b/lib/process-services-cloud/src/lib/form/mocks/cloud-form.mock.ts index 366ec700b9..be0c25fa6e 100644 --- a/lib/process-services-cloud/src/lib/form/mocks/cloud-form.mock.ts +++ b/lib/process-services-cloud/src/lib/form/mocks/cloud-form.mock.ts @@ -758,3 +758,77 @@ export const emptyFormRepresentationJSON = { 'variables': [], 'version': 0 }; + +export const conditionalUploadWidgetsMock = { + 'formRepresentation': { + 'id': 'form-fb7858f7-5cf6-4afe-b462-c15a5dc0c34c', + 'name': 'AttachVisibility', + 'description': '', + 'version': 0, + 'formDefinition': { + 'tabs': [], + 'fields': [ + { + 'id': '1dc63387-aa9d-4f06-adfa-37817e8fd394', + 'name': 'Label', + 'type': 'container', + 'tab': null, + 'numberOfColumns': 2, + 'fields': { + '1': [ + { + 'id': 'Text0xlk8n', + 'name': 'Text', + 'type': 'text', + 'required': false, + 'colspan': 1, + 'placeholder': null, + 'minLength': 0, + 'maxLength': 0, + 'regexPattern': null, + 'visibilityCondition': null, + 'params': { + 'existingColspan': 1, + 'maxColspan': 2 + } + } + ], + '2': [ + { + 'id': 'Attachfile0h9fr1', + 'name': 'Attach file', + 'type': 'upload', + 'required': false, + 'colspan': 1, + 'visibilityCondition': { + 'leftFormFieldId': 'Text0xlk8n', + 'leftRestResponseId': '', + 'operator': '==', + 'rightValue': 'Attach', + 'rightType': null, + 'rightFormFieldId': '', + 'rightRestResponseId': '', + 'nextConditionOperator': '', + 'nextCondition': null + }, + 'params': { + 'existingColspan': 1, + 'maxColspan': 2, + 'fileSource': { + 'serviceId': 'alfresco-content', + 'name': 'Alfresco Content' + }, + 'multiple': false, + 'link': false + } + } + ] + } + } + ], + 'outcomes': [], + 'metadata': {}, + 'variables': [] + } + } +};