Skip to content

Commit

Permalink
Consistent formatting with prettier / editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjan1999 committed Apr 25, 2019
1 parent 76f5ea3 commit c90d98c
Show file tree
Hide file tree
Showing 10 changed files with 704 additions and 653 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.awcache
dist
dist
.vscode
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
}
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import MockAxios from "./mock-axios";
export default MockAxios;
import MockAxios from "./mock-axios";
export default MockAxios;
210 changes: 109 additions & 101 deletions lib/mock-axios-types.ts
Original file line number Diff line number Diff line change
@@ -1,101 +1,109 @@
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?]>;
request: jest.Mock<SyncPromise, [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 promise of the most recent request
*/
lastReqGet?: () => 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?]>;
request: jest.Mock<SyncPromise, [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 promise of the most recent request
*/
lastReqGet?: () => 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 c90d98c

Please sign in to comment.