Skip to content

Commit

Permalink
Minor beautification
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjan1999 committed Dec 8, 2019
1 parent 2cc6e22 commit 512918c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/mock-axios-types.ts
Expand Up @@ -59,32 +59,34 @@ interface CancelTokenStatic {

export interface AxiosMockAPI {
/**
* Simulate a server response, (optionally) with the given data
* Simulates a server response, (optionally) with the given data
* @param response (optional) response returned by the server
* @param queueItem (optional) request promise for which response should be resolved
* @param silentMode (optional) specifies whether the call should throw an error or
* only fail quietly if no matching request is found.
* only fail quietly if no matching request is found. Defaults to false.
*/
mockResponse: (
response?: HttpResponse,
queueItem?: Promise<any> | AxiosMockQueueItem,
silentMode?: boolean,
) => void;

/**
* Simulate a server response for a specific request, (optionally) with the given data
* Simulates a server response for a specific request, (optionally) with the given data
* @param criteria specifies which request should be resolved; it can be just the URL
* or an object containing url and/or method
* @param response (optional) response returned by the server
* @param silentMode (optional) specifies whether the call should throw an error or
* only fail quietly if no matching request is found.
* only fail quietly if no matching request is found. Defaults to false.
*/
mockResponseFor: (
criteria: string | AxiosMockRequestCriteria,
response?: HttpResponse,
silentMode?: boolean,
) => void;

/**
* Simulate an error in server request
* Simulates an error in server request
* @param error (optional) error object
* @param queueItem (optional) request promise for which response should be resolved
* @param silentMode (optional) specifies whether the call should throw an error or
Expand All @@ -95,20 +97,24 @@ export interface AxiosMockAPI {
queueItem?: Promise<any> | AxiosMockQueueItem,
silentMode?: boolean,
) => void;

/**
* Returns promise of the most recent request
*/
lastPromiseGet: () => SynchronousPromise<any>;

/**
* Removes the give promise from the queue
* @param promise
*/

popPromise: (promise?: UnresolvedSynchronousPromise<any>) => UnresolvedSynchronousPromise<any>;

/**
* Returns request item of the most recent request
*/
lastReqGet: () => AxiosMockQueueItem;

/**
* Returns request item of the most recent request with the given criteria
* Returns undefined if no matching request could be found
Expand All @@ -119,6 +125,7 @@ export interface AxiosMockAPI {
* @param criteria the criteria by which to find the request
*/
getReqMatching: (criteria: AxiosMockRequestCriteria) => AxiosMockQueueItem;

/**
* Returns request item of the most recent request with the given url
* The url must equal the url given in the 1st parameter when the request was made
Expand All @@ -129,6 +136,7 @@ export interface AxiosMockAPI {
* @param url the url of the request to be found
*/
getReqByUrl: (url: string) => AxiosMockQueueItem;

/**
* Removes the give request from the queue
* @param promise
Expand Down
4 changes: 4 additions & 0 deletions lib/mock-axios.ts
Expand Up @@ -163,11 +163,13 @@ MockAxios.mockResponseFor = (
criteria = {url: criteria};
}
const queueItem = MockAxios.getReqMatching(criteria);

if (!queueItem && !silentMode) {
throw new Error("No request to respond to!");
} else if (!queueItem) {
return;
}

MockAxios.mockResponse(response, queueItem, silentMode);
};

Expand Down Expand Up @@ -201,9 +203,11 @@ const _checkCriteria = (item: AxiosMockQueueItem, criteria: AxiosMockRequestCrit
if (criteria.method !== undefined && criteria.method !== item.method) {
return false;
}

if (criteria.url !== undefined && criteria.url !== item.url) {
return false;
}

return true;
};

Expand Down

0 comments on commit 512918c

Please sign in to comment.