Home Reference Source
import HttpMock from 'packages/http/src/httpMock.ts'
public class | source

HttpMock

Http Mocker for easy to http testing using Jest

When using this mocking helper you need to call Http.onGet() before Http instance is created

Static Method Summary

Static Public Methods
public static

onDelete(endpoint: string): MockBehavior

Mocks responses for delete methods

public static

onGet(endpoint: string): MockBehavior

Mocks responses for get methods You may pass a specific endpoint as parameter to mock only selected endpoints.

public static

onPost(endpoint: string): MockBehavior

Mocks responses for post methods

public static

onPut(endpoint: string): MockBehavior

Mocks responses for put methods

public static

reset()

Resets all mocked behavior

Static Public Methods

public static onDelete(endpoint: string): MockBehavior source

Mocks responses for delete methods

Params:

NameTypeAttributeDescription
endpoint string

Return:

MockBehavior

public static onGet(endpoint: string): MockBehavior source

Mocks responses for get methods You may pass a specific endpoint as parameter to mock only selected endpoints. This is very useful, when having methods that do several Http requests, so you can mock them one on one.

The following code returns the same content on every get call

  HttpMock.onGet().reply(200, [{login: 'foo'}, {login: 'bar'}]);

The next code returns the different content depending on the passed endpoint

  HttpMock.onGet('/foo').reply(200, {data: 'foo'});
  HttpMock.onGet('/bar').reply(200, {data: 'bar'});

Params:

NameTypeAttributeDescription
endpoint string

Return:

MockBehavior

public static onPost(endpoint: string): MockBehavior source

Mocks responses for post methods

Params:

NameTypeAttributeDescription
endpoint string

Return:

MockBehavior

public static onPut(endpoint: string): MockBehavior source

Mocks responses for put methods

Params:

NameTypeAttributeDescription
endpoint string

Return:

MockBehavior

public static reset() source

Resets all mocked behavior