From 9b239abda0638402a5708c7865d345d36f458a12 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Thu, 17 Feb 2022 13:34:13 +0300 Subject: [PATCH] test(generateFileUploadForm): add unit test --- sasjs-tests/src/testSuites/SpecialCases.ts | 2 +- src/file/spec/generateFileUploadForm.spec.ts | 44 ++++++++++++++++++++ src/utils/convertToCsv.ts | 2 +- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/file/spec/generateFileUploadForm.spec.ts diff --git a/sasjs-tests/src/testSuites/SpecialCases.ts b/sasjs-tests/src/testSuites/SpecialCases.ts index 636c9c3..2bf9c88 100644 --- a/sasjs-tests/src/testSuites/SpecialCases.ts +++ b/sasjs-tests/src/testSuites/SpecialCases.ts @@ -80,7 +80,7 @@ const errorAndCsrfData: any = { } const testTable = 'sometable' -const testTableWithNullVars: { [key: string]: any } = { +export const testTableWithNullVars: { [key: string]: any } = { [testTable]: [ { var1: 'string', var2: 232, nullvar: 'A' }, { var1: 'string', var2: 232, nullvar: 'B' }, diff --git a/src/file/spec/generateFileUploadForm.spec.ts b/src/file/spec/generateFileUploadForm.spec.ts new file mode 100644 index 0000000..fa3a806 --- /dev/null +++ b/src/file/spec/generateFileUploadForm.spec.ts @@ -0,0 +1,44 @@ +import { generateFileUploadForm } from '../generateFileUploadForm' +import { testTableWithNullVars } from '../../../sasjs-tests/src/testSuites/SpecialCases' + +describe('generateFileUploadForm', () => { + beforeAll(() => { + function FormDataMock(this: any) { + this.append = () => {} + } + + const BlobMock = jest.fn() + + ;(global as any).FormData = FormDataMock + ;(global as any).Blob = BlobMock + }) + + it('should generate file upload form from data', () => { + const formData = new FormData() + const tableName = Object.keys(testTableWithNullVars).filter((key: string) => + Array.isArray(testTableWithNullVars[key]) + )[0] + + jest.spyOn(formData, 'append').mockImplementation(() => {}) + + generateFileUploadForm(formData, testTableWithNullVars) + + expect(formData.append).toHaveBeenCalledOnce() + expect(formData.append).toHaveBeenCalledWith( + tableName, + {}, + `${tableName}.csv` + ) + }) + + it('should throw an error if too large string was provided', () => { + const formData = new FormData() + const data = { testTable: [{ var1: 'z'.repeat(32765 + 1) }] } + + expect(() => generateFileUploadForm(formData, data)).toThrow( + new Error( + 'The max length of a string value in SASjs is 32765 characters.' + ) + ) + }) +}) diff --git a/src/utils/convertToCsv.ts b/src/utils/convertToCsv.ts index 8ff5f97..73af3ba 100644 --- a/src/utils/convertToCsv.ts +++ b/src/utils/convertToCsv.ts @@ -3,7 +3,7 @@ * @param data - the array of JSON objects to convert. */ export const convertToCSV = ( - data: Array, + data: any[], sasFormats?: { formats: { [key: string]: string } } ) => { let formats = sasFormats?.formats