Created
November 30, 2021 13:34
-
-
Save Sidnioulz/5ae2439e683a670ccbdf51dae2da1417 to your computer and use it in GitHub Desktop.
Basic backend API mock functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
jest.mock('axios') | |
export const mockAxiosSuccess = (data, status = 200) => { | |
axios.mockResolvedValueOnce({ data, status }) | |
} | |
export const mockAxiosFailure = (data, status = null) => { | |
axios.mockRejectedValueOnce({ | |
response: { | |
data, | |
status: status || data?.code || 400, | |
}, | |
}) | |
} | |
export const mockAxiosNetworkFailure = () => { | |
const err = new Error('Network Error') | |
err.isAxiosError = true | |
err.config = { url: 'badurl.com/bad/path' } | |
axios.mockRejectedValueOnce(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment