bs-jest-fetch-mock
v2.0.0
Published
jest-fetch-mock bindings for BuckleScript in Reason
Downloads
1
Maintainers
Readme
bs-jest-fetch-mock
jest-fetch-mock bindings for BuckleScript in Reason
Installation
Prerequisite: you have installed jest-fetch-mock.
npm install --save-dev bs-jest-fetch-mock
# or ...
yarn add -D bs-jest-fetch-mock
Usage
Add to bsconfig.json
...
"bs-dependencies": [
+ "bs-jest-fetch-mock"
]
...
API
For more example, please refer to JestFetchMock_test.re
BsJestFetchMock
is the root namespace module.
JestFetchMock.mockResponse
string
JestFetchMock.mockResponse(`Str({|{ "body": "ok" }|}), Js.Undefined.empty);
string with init
JestFetchMock.mockResponse(
`Str({|{ "body": "ok" }|}),
Js.Undefined.return(
init(
~status=204,
~statusText="nothing for you",
~headers=Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
),
),
);
function (with string)
JestFetchMock.mockResponse(
`FnStr(
req =>
if (Fetch.Request.url(req) == "http://parsed_url/") {
resolve({|{ "body": "ok" }|});
} else {
resolve("");
},
),
Js.Undefined.empty,
);
function (with response)
JestFetchMock.mockResponse(
`FnResp(
req =>
if (Fetch.Request.url(req) == "http://parsed_url/") {
response(
~body={|{ "body": "ok" }|},
~status=200,
~statusText="OK",
~headers=
Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
)
|> resolve,
} else {
response(
~body="",
~status=418,
~statusText="I'm a teapot",
~headers=
Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
)
|> resolve,
},
),
Js.Undefined.empty,
);
JestFetchMock.mockResponseOnce
Same function signature as mockResponse
.
JestFetchMock.mockResponsesStr
JestFetchMock.mockResponsesStr([|
({|"first body"|}, Js.Undefined.empty),
(
{|"second body"|},
Js.Undefined.return(
init(
~status=200,
~statusText="ok",
~headers=Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
),
),
),
|]);
JestFetchMock.mockResponsesFn
JestFetchMock.mockResponsesFn([|
(_req => Js.Promise.resolve({|"first body"|}), Js.Undefined.empty),
(
_req => Js.Promise.resolve({|"second body"|}),
Js.Undefined.return(
init(
~status=200,
~statusText="ok",
~headers=Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
),
),
),
|]);
JestFetchMock.mockResponsesFnResp
JestFetchMock.mockResponsesFnResp([|
(
_req =>
response(
~body={|"first body"|},
~status=418,
~statusText="I'm a teapot",
(),
)
|> resolve,
Js.Undefined.empty
),
(
_req =>
response(
~body={|"second body"|},
(),
)
|> resolve,
Js.Undefined.return(
init(
~status=200,
~statusText="ok",
~headers=Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
),
),
),
|]);
JestFetchMock.mockReject
string
JestFetchMock.mockReject(`Str({|{ "body": "ok" }|}));
function
JestFetchMock.mockReject(`FnStr(_req => Js.Promise.resolve({|{ "body": "ok" }|})));
JestFetchMock.mockRejectOnce
Same function signature as mockReject
.
JestFetchMock.mockAbort
JestFetchMock.mockReject();
JestFetchMock.mockAbortOnce
Same function signature as mockAbort
.
JestFetchMock.resetMocks
JestFetchMock.resetMocks();
Testing the library
npm test
this will compile and execute tests with bs-jest
Contributions
Don't hesitate to open a PR with a new binding - while bumping up the amount of covered bindings in the README. There are tests, use them and write the most simple test you can think of to make sure that the bindings work correctly.