const getMedia = async (mediaId) => (await makeRequest({
url: `${MEDIA_BASE_URL}/admin/media/${mediaId}`,
method: 'GET'
})).data
If this is the function that is making the request through the Axios, we can make it with some curry function and Axios innate data of config to make it and return it with Promise.resolve.
const createMockImplementation = ({ contentId, mediaId }) => (config = {}) => {
if (config.method?.toLowerCase() === 'get' && config.url?.endsWith(`/admin/content/${contentId}`)) {
return Promise.resolve({
status: 200,
data: {
id: contentId,
...mockContentItem1
}
}) // getContent
} else if (config.method?.toLowerCase() === 'post' && config.url?.endsWith(`/admin/content/${contentId}`)) {
return Promise.resolve({
status: 200
}) // saveContent
} else if (config.method?.toLowerCase() === 'get' && config.url?.endsWith(`/admin/media/${mediaId}`)) {
return Promise.resolve({
status: 200,
data: {
id: mediaId,
...mockMediaItemWithAssets
}
}) // getMedia
} else if (config.method?.toLowerCase() === 'post' && config.url?.endsWith(`/admin/media/${mediaId ? `/${mediaId}` : ''}`)) {
return Promise.resolve({
status: 200
}) // saveMedia
}
}
'Node.js > Jest' 카테고리의 다른 글
[Jest] toBeTruthy() (0) | 2022.04.19 |
---|---|
[Jest] How to set up Debug in Visual Studio Code? (0) | 2022.04.16 |
[Jest] set, clear and reset mock/spy/stub implementation (0) | 2022.04.16 |
[Jest] Jest Mock Functions (0) | 2022.04.15 |
[Node.js] Jest worker encountered 4 child process exceptions, exceeding retry limit (0) | 2022.04.07 |