1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 01:14:36 +00:00

test(generateFileUploadForm): add unit test

This commit is contained in:
Yury Shkoda
2022-02-17 13:34:13 +03:00
parent d3dff44918
commit 9b239abda0
3 changed files with 46 additions and 2 deletions

View File

@@ -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' },

View File

@@ -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.'
)
)
})
})

View File

@@ -3,7 +3,7 @@
* @param data - the array of JSON objects to convert.
*/
export const convertToCSV = (
data: Array<any>,
data: any[],
sasFormats?: { formats: { [key: string]: string } }
) => {
let formats = sasFormats?.formats