Skip to content

Commit

Permalink
"Cherrypick" formatting update
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjan1999 committed Apr 27, 2019
1 parent 75b1a83 commit 80293c2
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 283 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[*]
end_of_line = lf
insert_final_newline = true
indent_size = 4
indent_style = space
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": false
}
224 changes: 116 additions & 108 deletions lib/mock-axios-types.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,116 @@
import SyncPromise from "jest-mock-promise";

export interface HttpResponse {
data: any;
status?: number;
statusText?: string;
headers?: object;
config?: object;
}

export type AnyFunction = (...args: any[]) => any;

// spy is a function which extends an object (it has static methods and properties)
export type SpyFn = AnyFunction & { mockClear: AnyFunction };

export type AxiosFn = (...args: any[]) => SpyFn;

type Interceptors = {
request: {
use: SpyFn
},
response: {
use: SpyFn
}
};

type AxiosDefaults = {
headers: any
};

export interface AxiosAPI {
// mocking Axios methods
get: jest.Mock<SyncPromise, [string?, any?, any?]>;
post: jest.Mock<SyncPromise, [string?, any?, any?]>;
put: jest.Mock<SyncPromise, [string?, any?, any?]>;
patch: jest.Mock<SyncPromise, [string?, any?, any?]>;
delete: jest.Mock<SyncPromise, [string?, any?, any?]>;
head: jest.Mock<SyncPromise, [string?, any?, any?]>;
options: jest.Mock<SyncPromise, [string?, any?, any?]>;
all: SpyFn;
create: jest.Mock<AxiosMockType, []>;
interceptors: Interceptors;
defaults: AxiosDefaults;
}

export interface AxiosMockAPI {
/**
* Simulate 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.
*/
mockResponse: ((response?: HttpResponse, queueItem?: SyncPromise|AxiosMockQueueItem, silentMode?: boolean) => void);
/**
* Simulate 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 only fail quietly if no matching request is found.
*/
mockError?: (error?: any, queueItem?: SyncPromise|AxiosMockQueueItem, silentMode?: boolean) => void;
/**
* Returns promise of the most recent request
*/
lastPromiseGet?: () => SyncPromise;
/**
* Removes the give promise from the queue
* @param promise
*/

popPromise?: (promise?: SyncPromise) => SyncPromise;
/**
* Returns request item of the most recent request
*/
lastReqGet?: () => 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
* Returns undefined if no matching request could be found
*
* THe result can then be used with @see mockResponse
*
* @param url the url of the request to be found
*/
getReqByUrl: (url: string) => AxiosMockQueueItem;
/**
* Removes the give request from the queue
* @param promise
*/
popRequest?: (promise?: AxiosMockQueueItem) => AxiosMockQueueItem;

/**
* Clears all of the queued requests
*/
reset: () => void;
}

export interface AxiosMockQueueItem {
promise: SyncPromise;
url: string;
data?: any;
config?: any;
}

/**
* Axios object can be called like a function,
* that's why we're defining it as a spy
*/
export type AxiosMockType = AxiosFn & AxiosAPI & AxiosMockAPI;
import SyncPromise from "jest-mock-promise";

export interface HttpResponse {
data: any;
status?: number;
statusText?: string;
headers?: object;
config?: object;
}

export type AnyFunction = (...args: any[]) => any;

// spy is a function which extends an object (it has static methods and properties)
export type SpyFn = AnyFunction & { mockClear: AnyFunction };

export type AxiosFn = (...args: any[]) => SpyFn;

interface Interceptors {
request: {
use: SpyFn;
};
response: {
use: SpyFn;
};
}

interface AxiosDefaults {
headers: any;
}

export interface AxiosAPI {
// mocking Axios methods
get: jest.Mock<SyncPromise, [string?, any?, any?]>;
post: jest.Mock<SyncPromise, [string?, any?, any?]>;
put: jest.Mock<SyncPromise, [string?, any?, any?]>;
patch: jest.Mock<SyncPromise, [string?, any?, any?]>;
delete: jest.Mock<SyncPromise, [string?, any?, any?]>;
head: jest.Mock<SyncPromise, [string?, any?, any?]>;
options: jest.Mock<SyncPromise, [string?, any?, any?]>;
all: SpyFn;
create: jest.Mock<AxiosMockType, []>;
interceptors: Interceptors;
defaults: AxiosDefaults;
}

export interface AxiosMockAPI {
/**
* Simulate 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.
*/
mockResponse: (
response?: HttpResponse,
queueItem?: SyncPromise | AxiosMockQueueItem,
silentMode?: boolean,
) => void;
/**
* Simulate 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 only fail quietly if no matching request is found.
*/
mockError?: (
error?: any,
queueItem?: SyncPromise | AxiosMockQueueItem,
silentMode?: boolean,
) => void;
/**
* Returns promise of the most recent request
*/
lastPromiseGet?: () => SyncPromise;
/**
* Removes the give promise from the queue
* @param promise
*/

popPromise?: (promise?: SyncPromise) => SyncPromise;
/**
* Returns request item of the most recent request
*/
lastReqGet?: () => 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
* Returns undefined if no matching request could be found
*
* THe result can then be used with @see mockResponse
*
* @param url the url of the request to be found
*/
getReqByUrl: (url: string) => AxiosMockQueueItem;
/**
* Removes the give request from the queue
* @param promise
*/
popRequest?: (promise?: AxiosMockQueueItem) => AxiosMockQueueItem;

/**
* Clears all of the queued requests
*/
reset: () => void;
}

export interface AxiosMockQueueItem {
promise: SyncPromise;
url: string;
data?: any;
config?: any;
}

/**
* Axios object can be called like a function,
* that's why we're defining it as a spy
*/
export type AxiosMockType = AxiosFn & AxiosAPI & AxiosMockAPI;
Loading

0 comments on commit 80293c2

Please sign in to comment.