Skip to content
This repository has been archived by the owner on Jun 30, 2024. It is now read-only.
/ bs-jest-fetch-mock Public archive

jest-fetch-mock bindings for BuckleScript in Reason

License

Notifications You must be signed in to change notification settings

jihchi/bs-jest-fetch-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bs-jest-fetch-mock Travis (.org) npm Coveralls github npm NPM

jest-fetch-mock bindings for BuckleScript in Reason

Installation

npm install --save-dev bs-jest-fetch-mock jest-fetch-mock

# or yarn...

yarn add -D bs-jest-fetch-mock jest-fetch-mock

Add following code to your Jest's setupFiles:

// jest.setup.js
require('jest-fetch-mock').enableMocks();

Add following code to your Jest's configuration:

// package.json
"jest": {
  "automock": false,
  "setupFiles": [
    "./jest.setup.js"
  ]
}

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.