mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-09 13:30:04 +00:00
test(generateFileUploadForm): add unit test
This commit is contained in:
@@ -80,7 +80,7 @@ const errorAndCsrfData: any = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const testTable = 'sometable'
|
const testTable = 'sometable'
|
||||||
const testTableWithNullVars: { [key: string]: any } = {
|
export const testTableWithNullVars: { [key: string]: any } = {
|
||||||
[testTable]: [
|
[testTable]: [
|
||||||
{ var1: 'string', var2: 232, nullvar: 'A' },
|
{ var1: 'string', var2: 232, nullvar: 'A' },
|
||||||
{ var1: 'string', var2: 232, nullvar: 'B' },
|
{ var1: 'string', var2: 232, nullvar: 'B' },
|
||||||
|
|||||||
44
src/file/spec/generateFileUploadForm.spec.ts
Normal file
44
src/file/spec/generateFileUploadForm.spec.ts
Normal 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.'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
* @param data - the array of JSON objects to convert.
|
* @param data - the array of JSON objects to convert.
|
||||||
*/
|
*/
|
||||||
export const convertToCSV = (
|
export const convertToCSV = (
|
||||||
data: Array<any>,
|
data: any[],
|
||||||
sasFormats?: { formats: { [key: string]: string } }
|
sasFormats?: { formats: { [key: string]: string } }
|
||||||
) => {
|
) => {
|
||||||
let formats = sasFormats?.formats
|
let formats = sasFormats?.formats
|
||||||
|
|||||||
Reference in New Issue
Block a user