Skip to content

Instantly share code, notes, and snippets.

@sabesansathananthan
Created September 14, 2021 15:32
Show Gist options
  • Save sabesansathananthan/7c13abede7608794c4cd2fa397aea76a to your computer and use it in GitHub Desktop.
Save sabesansathananthan/7c13abede7608794c4cd2fa397aea76a to your computer and use it in GitHub Desktop.
mock network request in Jest
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