Created
September 14, 2021 15:32
-
-
Save sabesansathananthan/7c13abede7608794c4cd2fa397aea76a to your computer and use it in GitHub Desktop.
mock network request in Jest
This file contains hidden or 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 { request } from "./wrap-request"; | |
export const counter = (id: number, number: number): Promise<{ result: number; msg: string }> => { | |
const operate = number > 0 ? 1 : -1; | |
return request({ | |
url: "https://www.example.com/api/setCounter", | |
method: "POST", | |
data: { id, operate }, | |
}) | |
.then(res => { | |
if (res.result === 0) return { result: 0, msg: "success" }; | |
if (res.result === -100) return { result: -100, msg: "need login" }; | |
return { result: -999, msg: "fail" }; | |
}) | |
.catch(err => { | |
return { result: -999, msg: "fail" }; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment