mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-16 00:20:06 +00:00
chore(*): fix failing tests
This commit is contained in:
8
src/__mocks__/axios.ts
Normal file
8
src/__mocks__/axios.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { AxiosStatic } from 'axios'
|
||||||
|
|
||||||
|
const mockAxios = jest.genMockFromModule('axios') as AxiosStatic
|
||||||
|
|
||||||
|
// this is the key to fix the axios.create() undefined error!
|
||||||
|
mockAxios.create = jest.fn(() => mockAxios)
|
||||||
|
|
||||||
|
export default mockAxios
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
import { FileUploader } from '../FileUploader'
|
import { FileUploader } from '../FileUploader'
|
||||||
import { UploadFile } from '../types'
|
import { UploadFile } from '../types'
|
||||||
|
import axios from 'axios'
|
||||||
|
jest.mock('axios')
|
||||||
|
const mockedAxios = axios as jest.Mocked<typeof axios>
|
||||||
|
|
||||||
const sampleResponse = `{
|
const sampleResponse = `{
|
||||||
"SYSUSERID": "cas",
|
"SYSUSERID": "cas",
|
||||||
@@ -24,7 +27,6 @@ const prepareFilesAndParams = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('FileUploader', () => {
|
describe('FileUploader', () => {
|
||||||
let originalFetch: any
|
|
||||||
const fileUploader = new FileUploader(
|
const fileUploader = new FileUploader(
|
||||||
'/sample/apploc',
|
'/sample/apploc',
|
||||||
'https://sample.server.com',
|
'https://sample.server.com',
|
||||||
@@ -33,25 +35,12 @@ describe('FileUploader', () => {
|
|||||||
null
|
null
|
||||||
)
|
)
|
||||||
|
|
||||||
beforeAll(() => {
|
|
||||||
originalFetch = (global as any).fetch
|
|
||||||
})
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
|
||||||
Promise.resolve({
|
|
||||||
text: () => Promise.resolve(sampleResponse)
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
;(global as any).fetch = originalFetch
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should upload successfully', async (done) => {
|
it('should upload successfully', async (done) => {
|
||||||
const sasJob = 'test/upload'
|
const sasJob = 'test/upload'
|
||||||
const { files, params } = prepareFilesAndParams()
|
const { files, params } = prepareFilesAndParams()
|
||||||
|
mockedAxios.post.mockImplementation(() =>
|
||||||
|
Promise.resolve({ data: sampleResponse })
|
||||||
|
)
|
||||||
|
|
||||||
fileUploader.uploadFile(sasJob, files, params).then((res: any) => {
|
fileUploader.uploadFile(sasJob, files, params).then((res: any) => {
|
||||||
expect(JSON.stringify(res)).toEqual(
|
expect(JSON.stringify(res)).toEqual(
|
||||||
@@ -83,10 +72,8 @@ describe('FileUploader', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should throw an error when login is required', async (done) => {
|
it('should throw an error when login is required', async (done) => {
|
||||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
mockedAxios.post.mockImplementation(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({ data: '<form action="Logon">' })
|
||||||
text: () => Promise.resolve('<form action="Logon">')
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const sasJob = 'test'
|
const sasJob = 'test'
|
||||||
@@ -101,10 +88,8 @@ describe('FileUploader', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should throw an error when invalid JSON is returned by the server', async (done) => {
|
it('should throw an error when invalid JSON is returned by the server', async (done) => {
|
||||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
mockedAxios.post.mockImplementation(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({ data: '{invalid: "json"' })
|
||||||
text: () => Promise.resolve('{invalid: "json"')
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const sasJob = 'test'
|
const sasJob = 'test'
|
||||||
@@ -119,10 +104,8 @@ describe('FileUploader', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should throw an error when the server request fails', async (done) => {
|
it('should throw an error when the server request fails', async (done) => {
|
||||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
mockedAxios.post.mockImplementation(() =>
|
||||||
Promise.resolve({
|
Promise.reject({ data: '{message: "Server error"}' })
|
||||||
text: () => Promise.reject('{message: "Server error"}')
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const sasJob = 'test'
|
const sasJob = 'test'
|
||||||
|
|||||||
Reference in New Issue
Block a user