-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75b1a83
commit 80293c2
Showing
4 changed files
with
321 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 4, | ||
"semi": true, | ||
"singleQuote": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.