mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-07 12:30:06 +00:00
chore(file-uploader): improve mocking of fetch, add tests for all error scenarios
This commit is contained in:
@@ -1,10 +1,34 @@
|
|||||||
import { FileUploader } from '../FileUploader'
|
import { FileUploader } from '../FileUploader'
|
||||||
import { UploadFile } from '../types'
|
import { UploadFile } from '../types'
|
||||||
|
|
||||||
|
const sampleResponse = `{
|
||||||
|
"SYSUSERID": "cas",
|
||||||
|
"_DEBUG":" ",
|
||||||
|
"SYS_JES_JOB_URI": "/jobExecution/jobs/000-000-000-000",
|
||||||
|
"_PROGRAM" : "/Public/app/editors/loadfile",
|
||||||
|
"SYSCC" : "0",
|
||||||
|
"SYSJOBID" : "117382",
|
||||||
|
"SYSWARNINGTEXT" : ""
|
||||||
|
}`
|
||||||
|
|
||||||
|
describe('FileUploader', () => {
|
||||||
|
let originalFetch: any
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
originalFetch = (global as any).fetch
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
text: () => Promise.resolve(sampleResponse)
|
text: () => Promise.resolve(sampleResponse)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
;(global as any).fetch = originalFetch
|
||||||
|
})
|
||||||
|
|
||||||
it('should upload successfully', async (done) => {
|
it('should upload successfully', async (done) => {
|
||||||
const fileUploader = new FileUploader(
|
const fileUploader = new FileUploader(
|
||||||
@@ -25,12 +49,14 @@ it('should upload successfully', async (done) => {
|
|||||||
const params = { table: 'libtable' }
|
const params = { table: 'libtable' }
|
||||||
|
|
||||||
fileUploader.uploadFile(sasJob, files, params).then((res: any) => {
|
fileUploader.uploadFile(sasJob, files, params).then((res: any) => {
|
||||||
if (JSON.stringify(res) === JSON.stringify(JSON.parse(sampleResponse)))
|
expect(JSON.stringify(res)).toEqual(
|
||||||
|
JSON.stringify(JSON.parse(sampleResponse))
|
||||||
|
)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should throw no files error', async (done) => {
|
it('should an error when no files are provided', async (done) => {
|
||||||
const fileUploader = new FileUploader(
|
const fileUploader = new FileUploader(
|
||||||
'/sample/apploc',
|
'/sample/apploc',
|
||||||
'https://sample.server.com',
|
'https://sample.server.com',
|
||||||
@@ -43,15 +69,13 @@ it('should throw no files error', async (done) => {
|
|||||||
const files: UploadFile[] = []
|
const files: UploadFile[] = []
|
||||||
const params = { table: 'libtable' }
|
const params = { table: 'libtable' }
|
||||||
|
|
||||||
fileUploader.uploadFile(sasJob, files, params).then(
|
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||||
(res: any) => {},
|
expect(err.error.message).toEqual('At least one file must be provided.')
|
||||||
(err: any) => {
|
done()
|
||||||
if (err.error.message === 'At least one file must be provided.') done()
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should throw no sasJob error', async (done) => {
|
it('should throw an error when no sasJob is provided', async (done) => {
|
||||||
const fileUploader = new FileUploader(
|
const fileUploader = new FileUploader(
|
||||||
'/sample/apploc',
|
'/sample/apploc',
|
||||||
'https://sample.server.com',
|
'https://sample.server.com',
|
||||||
@@ -69,20 +93,104 @@ it('should throw no sasJob error', async (done) => {
|
|||||||
]
|
]
|
||||||
const params = { table: 'libtable' }
|
const params = { table: 'libtable' }
|
||||||
|
|
||||||
fileUploader.uploadFile(sasJob, files, params).then(
|
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||||
(res: any) => {},
|
expect(err.error.message).toEqual('sasJob must be provided.')
|
||||||
(err: any) => {
|
done()
|
||||||
if (err.error.message === 'sasJob must be provided.') done()
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const sampleResponse = `{
|
it('should throw an error when login is required', async (done) => {
|
||||||
"SYSUSERID": "cas",
|
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||||
"_DEBUG":" ",
|
Promise.resolve({
|
||||||
"SYS_JES_JOB_URI": "/jobExecution/jobs/000-000-000-000",
|
text: () => Promise.resolve('<form action="Logon">')
|
||||||
"_PROGRAM" : "/Public/app/editors/loadfile",
|
})
|
||||||
"SYSCC" : "0",
|
)
|
||||||
"SYSJOBID" : "117382",
|
|
||||||
"SYSWARNINGTEXT" : ""
|
const fileUploader = new FileUploader(
|
||||||
}`
|
'/sample/apploc',
|
||||||
|
'https://sample.server.com',
|
||||||
|
'/jobs/path',
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
|
||||||
|
const sasJob = 'test'
|
||||||
|
const files: UploadFile[] = [
|
||||||
|
{
|
||||||
|
file: new File([''], 'testfile'),
|
||||||
|
fileName: 'testfile'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
const params = { table: 'libtable' }
|
||||||
|
|
||||||
|
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||||
|
expect(err.error.message).toEqual(
|
||||||
|
'You must be logged in to upload a file.'
|
||||||
|
)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw an error when invalid JSON is returned by the server', async (done) => {
|
||||||
|
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
text: () => Promise.resolve('{invalid: "json"')
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const fileUploader = new FileUploader(
|
||||||
|
'/sample/apploc',
|
||||||
|
'https://sample.server.com',
|
||||||
|
'/jobs/path',
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
|
||||||
|
const sasJob = 'test'
|
||||||
|
const files: UploadFile[] = [
|
||||||
|
{
|
||||||
|
file: new File([''], 'testfile'),
|
||||||
|
fileName: 'testfile'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
const params = { table: 'libtable' }
|
||||||
|
|
||||||
|
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||||
|
expect(err.error.message).toEqual(
|
||||||
|
'Error while parsing json from upload response.'
|
||||||
|
)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw an error when the server request fails', async (done) => {
|
||||||
|
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
text: () => Promise.reject('{message: "Server error"}')
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const fileUploader = new FileUploader(
|
||||||
|
'/sample/apploc',
|
||||||
|
'https://sample.server.com',
|
||||||
|
'/jobs/path',
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
|
||||||
|
const sasJob = 'test'
|
||||||
|
const files: UploadFile[] = [
|
||||||
|
{
|
||||||
|
file: new File([''], 'testfile'),
|
||||||
|
fileName: 'testfile'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
const params = { table: 'libtable' }
|
||||||
|
|
||||||
|
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||||
|
expect(err.error.message).toEqual('Upload request failed.')
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user