1- import { test , expect } from './fixtures/import-file.fixture.js' ;
2- import { FormFillerPage } from './models/form-filler-page.js' ;
1+ import fs from 'fs/promises' ;
32import { match } from 'path-to-regexp' ;
3+ import { PDFDocument } from 'pdf-lib' ;
4+ import { test , expect } from './fixtures/add-download.fixture.js' ;
5+ import { FormFillerPage } from './models/form-filler-page.js' ;
46import { Create } from '../../packages/design/src/FormManager/routes' ;
57
68test . describe ( 'Fill a form as an applicant' , ( ) => {
@@ -14,13 +16,13 @@ test.describe('Fill a form as an applicant', () => {
1416 }
1517
1618 return matcher ( pagePath ) ;
17- }
19+ } ;
1820
1921 test ( 'Next button navigation' , async ( { page, formUrl } ) => {
2022 const url = new URL ( formUrl ) ;
2123 const result = getFormParams ( url ) ;
2224
23- if ( result ) {
25+ if ( result ) {
2426 const { formId } = result . params ;
2527 const formPage = new FormFillerPage ( page ) ;
2628 await formPage . navigateTo ( `${ url . protocol } //${ url . host } /forms/${ formId } ` ) ;
@@ -39,7 +41,7 @@ test.describe('Fill a form as an applicant', () => {
3941 params . append ( 'page' , '2' ) ;
4042 const result = getFormParams ( url ) ;
4143
42- if ( result ) {
44+ if ( result ) {
4345 const { formId } = result . params ;
4446 const formPage = new FormFillerPage ( page ) ;
4547 await formPage . navigateTo ( `${ url . protocol } //${ url . host } /forms/${ formId } ?${ params . toString ( ) } ` ) ;
@@ -51,4 +53,81 @@ test.describe('Fill a form as an applicant', () => {
5153 throw new Error ( 'Invalid form URL' ) ;
5254 }
5355 } ) ;
56+
57+ test ( 'Verify download' , async ( { page, formUrl } ) => {
58+ const url = new URL ( formUrl ) ;
59+ const result = getFormParams ( url ) ;
60+ const fields = [
61+ { label : 'First Name' , value : 'John' } ,
62+ { label : 'Middle Name' , value : 'Michael' } ,
63+ { label : 'Last Name' , value : 'Doe' } ,
64+ ] ;
65+
66+ const getPdfFormFromFile = async ( downloadPath : string ) => {
67+ const pdfBuffer = await fs . readFile ( downloadPath ) ;
68+ const pdfDoc = await PDFDocument . load ( pdfBuffer ) ;
69+ return pdfDoc . getForm ( ) ;
70+ }
71+
72+ const isFieldValuePresent = ( pdfForm : PDFForm , fieldNames : string [ ] , value : string ) => {
73+ let fieldValueFound = false ;
74+ for ( const fieldName of fieldNames ) {
75+ const pdfField = pdfForm . getField ( fieldName ) ;
76+ if ( pdfField . getText ( ) === value ) {
77+ fieldValueFound = true ;
78+ break ;
79+ }
80+ }
81+ return fieldValueFound ;
82+ }
83+
84+ if ( result ) {
85+ const { formId } = result . params ;
86+ const formPage = new FormFillerPage ( page ) ;
87+ await formPage . navigateTo ( `${ url . protocol } //${ url . host } /forms/${ formId } ` ) ;
88+
89+ const isDownloadButtonVisible = async ( ) : Promise < boolean > =>
90+ await page . getByRole ( 'button' , { name : 'Download PDF' , exact : true } ) . isVisible ( ) ;
91+
92+ const waitForDownloadButton = async ( ) => {
93+ let isButtonVisible = await isDownloadButtonVisible ( ) ;
94+ while ( ! isButtonVisible ) {
95+ await formPage . clickNextButton ( ) ;
96+ await page . waitForTimeout ( 500 ) ;
97+ isButtonVisible = await isDownloadButtonVisible ( ) ;
98+ }
99+ } ;
100+
101+ for ( const { label, value } of fields ) {
102+ await formPage . updateInputValue ( label , value ) ;
103+ }
104+
105+ await waitForDownloadButton ( ) ;
106+
107+ const downloadPromise = page . waitForEvent ( 'download' ) ;
108+ await page . getByRole ( 'button' , { name : 'Download PDF' , exact : true } ) . click ( ) ;
109+ const download = await downloadPromise ;
110+ expect ( download . suggestedFilename ( ) ) . toBe (
111+ 'demo-application_for_certificate_of_pardon_for_simple_marijuana_possession.pdf' ,
112+ ) ;
113+
114+ const downloadPath = await download . path ( ) ;
115+ const pdfForm = await getPdfFormFromFile ( downloadPath ) ;
116+ const pdfFieldNames = pdfForm . getFields ( ) . map ( ( pdfFormField ) => pdfFormField . getName ( ) ) ;
117+
118+ let fieldValuesFound = [ ] ;
119+ for ( const { value } of fields ) {
120+ fieldValuesFound = [
121+ ...fieldValuesFound ,
122+ isFieldValuePresent ( pdfForm , pdfFieldNames , value )
123+ ] ;
124+ }
125+
126+ expect ( fieldValuesFound . every ( value => value === true ) ) . toBe ( true ) ;
127+ expect ( fieldValuesFound . length ) . toBe ( fields . length ) ;
128+
129+ } else {
130+ throw new Error ( 'Invalid form URL' ) ;
131+ }
132+ } ) ;
54133} ) ;
0 commit comments