import { TestPage } from './lforms_testpage.po' ; import TestUtil from './util' ; import { element , by , ExpectedConditions , browser , protractor } from 'protractor' ; const tp = new TestPage ( ) ; describe ( 'Attachment support' , ( ) => { describe ( '' , ( ) => { beforeAll ( ( ) => { tp . openBaseTestPage ( ) ; tp . loadFromTestData ( 'attachmentQ.json' ) ; } ) ; /**
         *  Removes the first attachment.
         */ function removeFirstAttachment ( ) { browser . wait ( ExpectedConditions . not ( ExpectedConditions . presenceOf ( element ( by . id ( 'file-upload/1' ) ) ) ) , 5000 ) ; element ( by . css ( '.lf-remove-attachment' ) ) . click ( ) ; expect ( element ( by . id ( 'file-upload/1' ) ) . getAttribute ( 'type' ) ) . toBe ( 'file' ) ; } it ( 'should show file inputs in both vertical and horizontal layouts' , ( ) => { expect ( element ( by . id ( 'file-upload/1' ) ) . getAttribute ( 'type' ) ) . toBe ( 'file' ) ; expect ( element ( by . id ( 'file-upload-in-table/1/1' ) ) . getAttribute ( 'type' ) ) . toBe ( 'file' ) ; } ) ; it ( 'should allow attachment of file data without a URL' , ( ) => { let testFile = tp . getTestDataPathName ( 'attachmentQ.json' ) ; TestUtil . sendKeys ( element ( by . id ( 'file-upload/1' ) ) , testFile ) ; browser . wait ( ExpectedConditions . not ( ExpectedConditions . presenceOf ( element ( by . id ( 'file-upload/1' ) ) ) ) , 5000 ) ; expect ( element ( by . tagName ( 'a' ) ) . getText ( ) ) . toBe ( 'attachmentQ.json' ) ; tp . getQuestionnaireResponse ( null ) . then ( ( qr ) => { let answer = qr . item [ 0 ] . answer [ 0 ] . valueAttachment ; expect ( answer . title ) . toBe ( 'attachmentQ.json' ) ; expect ( typeof answer . data ) . toBe ( 'string' ) ; expect ( typeof answer . creation ) . toBe ( 'string' ) ; expect ( answer . contentType ) . toBe ( 'application/json' ) ; } ) ; } ) ; it ( 'should allow removal of an attachment' , ( ) => { removeFirstAttachment ( ) ; } ) ; it ( 'should not respond to presses of "enter" in unrelated fields' , ( ) => { element ( by . id ( 'upload-desc/1/1' ) ) . sendKeys ( protractor . Key . ENTER ) ; browser . wait ( ExpectedConditions . not ( ExpectedConditions . presenceOf ( element ( by . css ( '.lf-attachment-url' ) ) ) ) , 5000 ) ; } ) ; it ( 'should not allow files over maxSize size' , ( ) => { var testData = '' ; for ( var i = 0 ; i < 5002 ; ++ i ) testData += 'z' ; var pathName = TestUtil . createTempFile ( 'test.txt' , testData ) ; TestUtil . sendKeys ( element ( by . id ( 'file-upload/1' ) ) , pathName ) ; browser . wait ( ExpectedConditions . alertIsPresent ( ) , 5000 ) ; var alert = browser . switchTo ( ) . alert ( ) ; alert . getText ( ) . then ( ( msg ) => { expect ( msg . indexOf ( 'size' ) ) . not . toBe ( - 1 ) ; } ) ; alert . dismiss ( ) ; expect ( element ( by . id ( 'file-upload/1' ) ) . getAttribute ( 'value' ) ) . toBe ( '' ) ; } ) ; it ( 'should only allow permitted mime types' , ( ) => { var testData = '' ; for ( var i = 0 ; i < 2 ; ++ i ) testData += 'z' ; var pathName = TestUtil . createTempFile ( 'test.zip' , testData ) ; TestUtil . sendKeys ( element ( by . id ( 'file-upload/1' ) ) , pathName ) ; browser . wait ( ExpectedConditions . alertIsPresent ( ) , 5000 ) ; var alert = browser . switchTo ( ) . alert ( ) ; alert . getText ( ) . then ( ( msg ) => { expect ( msg . indexOf ( 'type' ) ) . not . toBe ( - 1 ) ; } ) ; alert . dismiss ( ) ; expect ( element ( by . id ( 'file-upload/1' ) ) . getAttribute ( 'value' ) ) . toBe ( '' ) ; pathName = TestUtil . createTempFile ( 'test' , testData ) ; TestUtil . sendKeys ( element ( by . id ( 'file-upload/1' ) ) , pathName ) ; alert = browser . switchTo ( ) . alert ( ) ; alert . getText ( ) . then ( ( msg ) => { expect ( msg . indexOf ( 'type' ) ) . not . toBe ( - 1 ) ; } ) ; alert . dismiss ( ) ; expect ( element ( by . id ( 'file-upload/1' ) ) . getAttribute ( 'value' ) ) . toBe ( '' ) ; } ) ; it ( 'should allow attachment of a URL without file data and without name' , ( ) => { element ( by . css ( '.toggle-attachment-fields' ) ) . click ( ) ; let newFields = element . all ( by . css ( 'input[type=text]' ) ) ; expect ( newFields . count ( ) ) . toBe ( 3 ) ; let urlField = newFields . get ( 0 ) ; let nameField = newFields . get ( 1 ) ; TestUtil . sendKeys ( urlField , 'http://one' ) ; element ( by . css ( '.attach-button' ) ) . click ( ) ; browser . wait ( ExpectedConditions . not ( ExpectedConditions . presenceOf ( element ( by . id ( 'file-upload/1' ) ) ) ) , 5000 ) ; expect ( element ( by . tagName ( 'a' ) ) . getText ( ) ) . toBe ( 'http://one' ) ; expect ( element ( by . tagName ( 'a' ) ) . getAttribute ( 'href' ) ) . toBe ( 'http://one/' ) ; tp . getQuestionnaireResponse ( null ) . then ( ( qr ) => { let answer = qr . item [ 0 ] . answer [ 0 ] . valueAttachment ; expect ( answer . title ) . toBeUndefined ( ) ; expect ( answer . data ) . toBeUndefined ( ) ; expect ( answer . url ) . toBe ( 'http://one' ) ; expect ( answer . creation ) . toBeUndefined ( ) ; expect ( answer . contentType ) . toBeUndefined ( ) ; } ) ; removeFirstAttachment ( ) ; } ) ; it ( 'should allow attachment of a URL without file data' , ( ) => { element ( by . css ( '.toggle-attachment-fields' ) ) . click ( ) ; let newFields = element . all ( by . css ( 'input[type=text]' ) ) ; expect ( newFields . count ( ) ) . toBe ( 3 ) ; let urlField = newFields . get ( 0 ) ; let nameField = newFields . get ( 1 ) ; TestUtil . sendKeys ( urlField , 'http://one' ) ; TestUtil . sendKeys ( nameField , 'two' ) ; element ( by . css ( '.attach-button' ) ) . click ( ) ; browser . wait ( ExpectedConditions . not ( ExpectedConditions . presenceOf ( element ( by . id ( 'file-upload/1' ) ) ) ) , 5000 ) ; expect ( element ( by . tagName ( 'a' ) ) . getText ( ) ) . toBe ( 'two' ) ; expect ( element ( by . tagName ( 'a' ) ) . getAttribute ( 'href' ) ) . toBe ( 'http://one/' ) ; tp . getQuestionnaireResponse ( null ) . then ( ( qr ) => { let answer = qr . item [ 0 ] . answer [ 0 ] . valueAttachment ; expect ( answer . title ) . toBe ( 'two' ) ; expect ( answer . data ) . toBeUndefined ( ) ; expect ( answer . url ) . toBe ( 'http://one' ) ; expect ( answer . creation ) . toBeUndefined ( ) ; expect ( answer . contentType ) . toBeUndefined ( ) ; } ) ; } ) ; it ( 'should allow a second attachment, with both a URL and file data' , ( ) => { element ( by . id ( 'add-upload/1' ) ) . click ( ) ; browser . wait ( ExpectedConditions . presenceOf ( element ( by . css ( '.lf-attachment-button' ) ) ) , 5000 ) ; element ( by . css ( '.toggle-attachment-fields' ) ) . click ( ) ; let newFields = element . all ( by . css ( 'input[type=text]' ) ) ; expect ( newFields . count ( ) ) . toBe ( 3 ) ; let urlField = newFields . get ( 0 ) ; let nameField = newFields . get ( 1 ) ; TestUtil . sendKeys ( urlField , 'http://three' ) ; TestUtil . sendKeys ( nameField , 'four' ) ; let testFile = tp . getTestDataPathName ( 'attachmentQ.json' ) ; TestUtil . sendKeys ( element ( by . id ( 'file-upload/2' ) ) , testFile ) ; element ( by . css ( '.attach-button' ) ) . click ( ) ; browser . wait ( ExpectedConditions . not ( ExpectedConditions . presenceOf ( element ( by . id ( 'file-upload/1' ) ) ) ) , 5000 ) ; let uploadLinks = element . all ( by . tagName ( 'a' ) ) ; expect ( uploadLinks . count ( ) ) . toBe ( 2 ) ; expect ( uploadLinks . get ( 1 ) . getText ( ) ) . toBe ( 'four' ) ; tp . getQuestionnaireResponse ( null ) . then ( ( qr ) => { let answer = qr . item [ 0 ] . answer [ 1 ] . valueAttachment ; expect ( answer . title ) . toBe ( 'four' ) ; expect ( typeof answer . data ) . toBe ( 'string' ) ; expect ( answer . url ) . toBe ( 'http://three' ) ; expect ( typeof answer . creation ) . toBe ( 'string' ) ; expect ( answer . contentType ) . toBe ( 'application/json' ) ; } ) ; } ) ; } ) ; it ( 'should support an attachment in a saved QuestionnaireRepsonse' , ( ) => { tp . openBaseTestPage ( ) ; tp . loadFromTestData ( 'attachmentQ.json' ) ; let testFile = tp . getTestDataPathName ( 'attachmentQ.json' ) ; TestUtil . sendKeys ( element ( by . id ( 'file-upload/1' ) ) , testFile ) ; browser . wait ( ExpectedConditions . not ( ExpectedConditions . presenceOf ( element ( by . id ( 'file-upload/1' ) ) ) ) , 5000 ) ; expect ( element ( by . tagName ( 'a' ) ) . getText ( ) ) . toBe ( 'attachmentQ.json' ) ; tp . getQuestionnaireResponse ( null ) . then ( ( qr ) => { var q = require ( testFile ) ; browser . executeScript ( "var lfData = LForms.Util.convertFHIRQuestionnaireToLForms(arguments[0], 'R4');" + "return LForms.Util.mergeFHIRDataIntoLForms('QuestionnaireResponse', arguments[1], lfData, 'R4')" , q , qr ) . then ( ( lfData ) => { tp . openTestPage ( '/test/addFormToPageTest.html' ) ; TestUtil . waitForElementPresent ( element ( by . id ( 'formContainer2' ) ) ) ; browser . executeScript ( "LForms.Util.addFormToPage(arguments[0], 'formContainer2')" , lfData ) ; expect ( element ( by . tagName ( 'a' ) ) . getText ( ) ) . toBe ( 'attachmentQ.json' ) ; browser . executeScript ( "return LForms.Util.getFormFHIRData('QuestionnaireResponse', 'R4', document.getElementById('formContainer2'))" ) . then ( ( qr2 ) => { let answer = qr2 . item [ 0 ] . answer [ 0 ] . valueAttachment ; expect ( answer . title ) . toBe ( 'attachmentQ.json' ) ; expect ( typeof answer . data ) . toBe ( 'string' ) ; expect ( typeof answer . creation ) . toBe ( 'string' ) ; expect ( answer . contentType ) . toBe ( 'application/json' ) ; } ) ; } ) ; } ) ; } ) ; it ( 'should allow extraction of the attachment item into an Observation' , function ( ) { tp . openBaseTestPage ( ) ; tp . loadFromTestData ( 'attachmentQ.json' ) ; let testFile = tp . getTestDataPathName ( 'attachmentQ.json' ) ; TestUtil . sendKeys ( element ( by . id ( 'file-upload/1' ) ) , testFile ) ; browser . wait ( ExpectedConditions . not ( ExpectedConditions . presenceOf ( element ( by . id ( 'file-upload/1' ) ) ) ) , 5000 ) ; expect ( element ( by . tagName ( 'a' ) ) . getText ( ) ) . toBe ( 'attachmentQ.json' ) ; tp . getQuestionnaireResponse ( { extract : true } ) . then ( ( fhirData ) => { expect ( Array . isArray ( fhirData ) ) . toBe ( true ) ; expect ( fhirData . length ) . toBe ( 2 ) ; let answer = fhirData [ 0 ] . item [ 0 ] . answer [ 0 ] . valueAttachment ; expect ( answer . title ) . toBe ( 'attachmentQ.json' ) ; expect ( typeof answer . data ) . toBe ( 'string' ) ; expect ( typeof answer . creation ) . toBe ( 'string' ) ; expect ( answer . contentType ) . toBe ( 'application/json' ) ; answer = fhirData [ 1 ] . valueAttachment ; expect ( answer . title ) . toBe ( 'attachmentQ.json' ) ; expect ( typeof answer . data ) . toBe ( 'string' ) ; expect ( typeof answer . creation ) . toBe ( 'string' ) ; expect ( answer . contentType ) . toBe ( 'application/json' ) ; } ) ; } ) ; } ) ;